Tensor定義
tensor張量可以理解為n維數(shù)組:
- 0維張量是一個(gè)數(shù)(Scalar/number),
- 1維張量是向量(Vector),
- 2維張量是矩陣(Martrix),
- 以此類(lèi)推...
基礎(chǔ)運(yùn)算
import tensorflow as tf
a=tf.add(3,5)
print(a)
Tensor("Add:0", shape=(), dtype=int32)
TF的加法方法树叽,但是通常的賦值并不是正常運(yùn)行加法探孝。
需要將被賦值的變量a放入session運(yùn)行才能看到運(yùn)算結(jié)果。
a=tf.add(3,5)
sess=tf.Session()
print(sess.run(a))
sess.close()
8
將運(yùn)算結(jié)果存入sess稍后再用的寫(xiě)法
a=tf.add(3,5)
with tf.Session() as sess:
print(sess.run(a))
8
tf.Session()封裝了一個(gè)執(zhí)行運(yùn)算的環(huán)境指攒,用tensor對(duì)象內(nèi)的賦值進(jìn)行運(yùn)算
混合運(yùn)算
x=2
y=3
op1 =tf.add(x,y)
op2=tf.multiply(x,y)
op3=tf.pow(op2,op1)
with tf.Session() as sess:
op3=sess.run(op3)
print(op3)
7776
Subgraphs
x=2
y=3
add_op=tf.add(x,y)
mul_op=tf.multiply(x,y)
useless=tf.multiply(x,add_op)
pow_op=tf.pow(add_op,mul_op)
with tf.Session() as sess:
z=sess.run(pow_op)
print(z)
15625
由于求Z值并不需要計(jì)算useless部分,所以session并沒(méi)有計(jì)算它
x=2
y=3
add_op=tf.add(x,y)
mul_op=tf.multiply(x,y)
useless=tf.multiply(x,add_op)
pow_op=tf.pow(add_op,mul_op)
with tf.Session() as sess:
z,not_useless=sess.run([pow_op,useless])
print(z)
print(not_useless)
15625
10
同時(shí)進(jìn)行兩個(gè)計(jì)算
Graph
g=tf.Graph()
with g.as_default():
x=tf.add(3,5)
sess=tf.Session(graph=g)
with tf.Session() as sess: #此處兩行的打包方式已經(jīng)過(guò)時(shí),如果報(bào)錯(cuò)需要改成下面的格式
sess.run(g)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches, contraction_fn)
270 self._unique_fetches.append(ops.get_default_graph().as_graph_element(
--> 271 fetch, allow_tensor=True, allow_operation=True))
272 except TypeError as e:
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
3034 with self._lock:
-> 3035 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
3036
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
3123 raise TypeError("Can not convert a %s into a %s." % (type(obj).__name__,
-> 3124 types_str))
3125
TypeError: Can not convert a Graph into a Tensor or Operation.
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-20-5c5906e5d961> in <module>()
5 sess=tf.Session(graph=g)
6 with tf.Session() as sess:
----> 7 sess.run(g)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
887 try:
888 result = self._run(None, fetches, feed_dict, options_ptr,
--> 889 run_metadata_ptr)
890 if run_metadata:
891 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1103 # Create a fetch handler to take care of the structure of fetches.
1104 fetch_handler = _FetchHandler(
-> 1105 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1106
1107 # Run request and get response.
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds, feed_handles)
412 """
413 with graph.as_default():
--> 414 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
415 self._fetches = []
416 self._targets = []
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
240 if isinstance(fetch, tensor_type):
241 fetches, contraction_fn = fetch_fn(fetch)
--> 242 return _ElementFetchMapper(fetches, contraction_fn)
243 # Did not find anything.
244 raise TypeError('Fetch argument %r has invalid type %r' %
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches, contraction_fn)
273 raise TypeError('Fetch argument %r has invalid type %r, '
274 'must be a string or Tensor. (%s)'
--> 275 % (fetch, type(fetch), str(e)))
276 except ValueError as e:
277 raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument <tensorflow.python.framework.ops.Graph object at 0x0000018E70371A20> has invalid type <class 'tensorflow.python.framework.ops.Graph'>, must be a string or Tensor. (Can not convert a Graph into a Tensor or Operation.)
教程的例子有報(bào)錯(cuò),需要改成下面的格式
g=tf.Graph()
with g.as_default():
x=tf.add(3,5)
with tf.Session(graph=g) as sess: #將上面的兩行改成一行
sess.run(x) #不能直接運(yùn)行g(shù)raph
g=tf.Graph()
with g.as_default():
a=3
b=5
x=tf.add(a,b)
sess = tf.Session(graph=g)
sess.close()
向graph內(nèi)添加加法運(yùn)算锭碳,并且設(shè)為默認(rèn)graph
g1=tf.get_default_graph()
g2=tf.graph()
#將運(yùn)算加入到默認(rèn)graph
with g1.as_default():
a=tf.Constant(3) #不會(huì)報(bào)錯(cuò),但推薦添加到自己創(chuàng)建的graph里
#將運(yùn)算加入到用戶創(chuàng)建的graph
with g2.as_default():
b=tf.Constant(5)
建議不要修改默認(rèn)graph
** Graph 的優(yōu)點(diǎn) **
- 節(jié)省運(yùn)算資源勿璃,只計(jì)算需要的部分
- 將計(jì)算分解為更小的部分
- 讓分布式運(yùn)算更方便擒抛,向多個(gè)CPU推汽,GPU或其它設(shè)備分配任務(wù)
- 適合那些使用directed graph的機(jī)器學(xué)習(xí)算法
Graph 與 Session 的區(qū)別
- Graph定義運(yùn)算,但不計(jì)算任何東西歧沪,不保存任何數(shù)值歹撒,只存儲(chǔ)你在各個(gè)節(jié)點(diǎn)定義的運(yùn)算。
- Session可運(yùn)行Graph或一部分Graph诊胞,它負(fù)責(zé)在一臺(tái)或多臺(tái)機(jī)器上分配資源暖夭,保存實(shí)際數(shù)值,中間結(jié)果和變量撵孤。
下面通過(guò)以下例子具體闡明二者的區(qū)別:
graph=tf.Graph()
with graph.as_default():#每次TF都會(huì)生產(chǎn)默認(rèn)graph,所以前兩行其實(shí)并不需要
variable=tf.Variable(42,name='foo')
initialize=tf.global_variables_initializer()
assign=variable.assign(13)
創(chuàng)建變量迈着,初始化值42,之后賦值13
graph=tf.Graph()
with graph.as_default():#每次TF都會(huì)生產(chǎn)默認(rèn)graph,所以前兩行其實(shí)并不需要
variable=tf.Variable(42,name='foo')
initialize=tf.global_variables_initializer()
assign=variable.assign(13)
with tf.Session(graph=graph) as sess:
sess.run(initialize) #記得將計(jì)算步驟在此處列出來(lái)
sess.run(assign)
print(sess.run(variable))
13
定義的計(jì)算數(shù)量達(dá)到三個(gè)時(shí)就要使用graph邪码。但是variable每次運(yùn)算都要在session內(nèi)run一遍裕菠,如果跳過(guò)此步驟,就無(wú)法獲取運(yùn)算后變量數(shù)值霞扬。(也就相當(dāng)于沒(méi)計(jì)算過(guò))
graph=tf.Graph()
with graph.as_default():#每次TF都會(huì)生產(chǎn)默認(rèn)graph,所以前兩行其實(shí)并不需要
variable=tf.Variable(42,name='foo')
initialize=tf.global_variables_initializer()
assign=variable.assign(13)
with tf.Session(graph=graph) as sess:
print(sess.run(variable)) #未列出計(jì)算步驟所以報(bào)錯(cuò)
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1322 try:
-> 1323 return fn(*args)
1324 except errors.OpError as e:
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1301 feed_dict, fetch_list, target_list,
-> 1302 status, run_metadata)
1303
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
472 compat.as_text(c_api.TF_Message(self.status.status)),
--> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive
FailedPreconditionError: Attempting to use uninitialized value foo
[[Node: _retval_foo_0_0 = _Retval[T=DT_INT32, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](foo)]]
During handling of the above exception, another exception occurred:
FailedPreconditionError Traceback (most recent call last)
<ipython-input-25-cb7c04ce65af> in <module>()
6
7 with tf.Session(graph=graph) as sess:
----> 8 print(sess.run(variable))
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
887 try:
888 result = self._run(None, fetches, feed_dict, options_ptr,
--> 889 run_metadata_ptr)
890 if run_metadata:
891 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1118 if final_fetches or final_targets or (handle and feed_dict_tensor):
1119 results = self._do_run(handle, final_targets, final_fetches,
-> 1120 feed_dict_tensor, options, run_metadata)
1121 else:
1122 results = []
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1315 if handle is None:
1316 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1317 options, run_metadata)
1318 else:
1319 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1334 except KeyError:
1335 pass
-> 1336 raise type(e)(node_def, op, message)
1337
1338 def _extend_graph(self):
FailedPreconditionError: Attempting to use uninitialized value foo
[[Node: _retval_foo_0_0 = _Retval[T=DT_INT32, index=0, _device="/job:localhost/replica:0/task:0/device:CPU:0"](foo)]]
graph=tf.Graph()
with graph.as_default():#每次TF都會(huì)生產(chǎn)默認(rèn)graph,所以前兩行其實(shí)并不需要
variable=tf.Variable(42,name='foo')
initialize=tf.global_variables_initializer()
assign=variable.assign(13)
with tf.Session(graph=graph) as sess:
sess.run(initialize) #計(jì)算步驟糕韧,列到第幾步就計(jì)算到第幾步
print(sess.run(variable))
42