Tensorflow细节-P54-变量
1、首先复习前面所学知识:
(1)g = tf.Graph()
(2)别忘了初始化时的initializer
(3) with tf.name_scope("generate_constant"): x = tf.constant([[0.7, 0.9]], name="x")
这个东西发现没球用
(4)最好是用with tf.variable_scope("generate_variable"):
与tf.get_variable
相结合,不要用tf.Variable()
出什么幺蛾子
import tensorflow as tf
g = tf.Graph()
with g.as_default():
with tf.variable_scope("generate_variable"):
w1 = tf.get_variable("w1", [2, 3], tf.float32, initializer=tf.random_normal_initializer(stddev=0.1, seed=1))
w2 = tf.get_variable("w2", [3, 1], tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1, seed=1))
with tf.name_scope("generate_constant"):
x = tf.constant([[0.7, 0.9]], name="x")
with tf.name_scope("matmul"):
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
writer = tf.summary.FileWriter("./path", graph=g)
writer.close()
with tf.Session(graph=g) as sess:
tf.global_variables_initializer().run()
print(sess.run(y))
2、所有的变量都会被加入到Graphkeys.VARIABLES集合,在变量声明时可控制trainable是之训练/不训练