我创建变量的方式如下:
x = tf.placeholder(tf.float32, shape=[None, D], name='x-input') # M x D# Variables Layer1#std = 1.5*np.pistd = 0.1W1 = tf.Variable( tf.truncated_normal([D,D1], mean=0.0, stddev=std, name='W1') ) # (D x D1)S1 = tf.Variable(tf.constant(100.0, shape=[1], name='S1')) # (1 x 1)C1 = tf.Variable( tf.truncated_normal([D1,1], mean=0.0, stddev=0.1, name='C1') ) # (D1 x 1)
但不知为何,TensorFlow在我的可视化中添加了额外的变量块:
为什么会这样?如何阻止这种情况的发生?
回答:
你在TensorFlow中错误地使用了名称
W1 = tf.Variable( tf.truncated_normal([D,D1], mean=0.0, stddev=std, name='W1') ) \----------------------------------------------------------/ initializer \-------------------------------------------------------------------------/ actual variable
因此,你的代码创建了未命名的变量,并为初始化操作命名为W1
。这就是为什么你在图中看到的名为W1
的并不是你的W1
变量,而是重命名的初始化操作,而你的W1
变量被称为Variable
(这是TensorFlow为未命名操作分配的默认名称)。正确的做法应该是
W1 = tf.Variable( tf.truncated_normal([D,D1], mean=0.0, stddev=std), name='W1' )
这样会为实际的变量创建一个名为W1
的节点,并且会附带一个小的初始化节点(用于为其设定随机值)。