tensorflow1.X自适名字变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import tensorflow as tf 

#函数接口定义

try:tf.Variable(initial_value=None,
trainable=True,
collections=None,
validate_shape=True,
caching_device=None,
name=None,
variable_def=None,
dtype=None,
expected_shape=None,
import_scope=None)
except:pass


#1. 变量创建后会被加入到 GraphKeys.GLOBAL_VARIABLES 中
#2. 默认可被训练,
#3. 实例名字a, b是唯一标志符,重复了会覆盖,但name可以重复
#3. 默认dtype是float32(<-小数),int32(<-整数)
a = tf.Variable(10,name='var1')
b = tf.Variable([[11,12],[21.0,22.0]],name='var1')
c = tf.Variable('田偉業',name='var1')

sess = tf.Session(); sess.run(tf.global_variables_initializer())

print(a.eval(session=sess))
#10
print(b.eval(session=sess))
#[[11 12]
# [21 22]]
print(a.shape, a.dtype, a.trainable)
#() <dtype: 'int32_ref'> True
print(b.shape, b.dtype, b.trainable)
#(2, 2) <dtype: 'float32_ref'> True
print(c.shape, c.dtype, c.trainable)
#() <dtype: 'string_ref'> True
print(a.name,b.name,c.name)
#var1:0 var1_1:0 var1_2:0

转载请注明来源 https://tianweiye.github.io