tf.get_variable()一般和tf.variable_scope()聯用
Gets an existing variable with these parameters or create a new one.
單獨用tf.get_variable()只能建立新的變數,並不能得到已定義的變數。
import numpy as np import tensorflow as tf Y,X = np.mgrid[1:10,60:69] Z = X + 1j*Y xs = tf.constant(Z.astype('complex64')) zs = tf.Variable(xs,name='zs') haha = tf.get_variable("zs",[9,9]) print('xs name:') print(xs.name) print('zs name:') print(zs.name) print('haha name') print(haha.name) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print('zs:') print(sess.run(zs)) print('haha:') print(sess.run(haha))
xs name:
Const:0
zs name:
zs:0
haha name
zs_1:0
zs:
[[ 60.+1.j 61.+1.j 62.+1.j 63.+1.j 64.+1.j 65.+1.j 66.+1.j 67.+1.j
68.+1.j]
[ 60.+2.j 61.+2.j 62.+2.j 63.+2.j 64.+2.j 65.+2.j 66.+2.j 67.+2.j
68.+2.j]
[ 60.+3.j 61.+3.j 62.+3.j 63.+3.j 64.+3.j 65.+3.j 66.+3.j 67.+3.j
68.+3.j]
[ 60.+4.j 61.+4.j 62.+4.j 63.+4.j 64.+4.j 65.+4.j 66.+4.j 67.+4.j
68.+4.j]
[ 60.+5.j 61.+5.j 62.+5.j 63.+5.j 64.+5.j 65.+5.j 66.+5.j 67.+5.j
68.+5.j]
[ 60.+6.j 61.+6.j 62.+6.j 63.+6.j 64.+6.j 65.+6.j 66.+6.j 67.+6.j
68.+6.j]
[ 60.+7.j 61.+7.j 62.+7.j 63.+7.j 64.+7.j 65.+7.j 66.+7.j 67.+7.j
68.+7.j]
[ 60.+8.j 61.+8.j 62.+8.j 63.+8.j 64.+8.j 65.+8.j 66.+8.j 67.+8.j
68.+8.j]
[ 60.+9.j 61.+9.j 62.+9.j 63.+9.j 64.+9.j 65.+9.j 66.+9.j 67.+9.j
68.+9.j]]
haha:
[[-0.14533952 -0.14523381 0.14145613 -0.22598141 0.1617533 0.19500619
-0.3357884 0.37150401 -0.37969798]
[ 0.0509094 -0.0470084 -0.49762926 -0.51874632 -0.29320604 -0.16176665
-0.5291841 -0.29577848 -0.45970225]
[-0.25813478 -0.54057223 0.05018246 0.03825295 0.57038844 0.48767948
-0.4359569 0.1867137 0.310839 ]
[-0.52079606 -0.01381797 -0.47396937 -0.49792162 -0.3253502 0.50621891
-0.233078 0.32882768 0.09004182]
[-0.16246468 0.50816798 0.4791249 -0.36204988 0.13710362 0.01671994
0.47486234 -0.23209643 0.19463843]
[-0.12628189 -0.08157283 0.05564255 -0.51489758 0.05933517 0.11145258
-0.30569348 -0.25055888 -0.11803272]
[ 0.20007056 -0.2579872 0.36695904 -0.23011178 -0.08050397 0.41955733
0.42810726 -0.53095597 0.08598411]
[-0.4530935 -0.30864996 0.56224608 0.5119139 0.33313245 0.09980142
-0.22323224 0.36909598 0.05093253]
[-0.42787349 0.26984555 -0.28011185 -0.09457535 0.23706919 0.5402844
-0.16451114 0.14855558 0.09051406]]