Tensorflow feed_dict 为变量增加了额外的维度?
Tensorflow feed_dict adds extra dimension to variables?
在 Tensorflow 中,我的网络的最后一层看起来像:
layer3output = tf.nn.relu(tf.matmul(layer2output, weightsLayer3) + biasesLayer3)
如果我检查输出的大小:
print(tf.shape(layer4output))
我得到:
Tensor("Shape:0", shape=(2,), dtype=int32)
我以为layer3output
是一维向量,但上面的表示是二维的。额外维度是否对应于通过 feed_dict
提供的示例?
如果是这样,那么当使用feed_dict时,所有的Tensorflow变量是否都多了一维?
更新:请看下面的代码。它打印出 6x3 weights
张量和 1x3 biases
张量,并将其中的 tf.shape
分别显示为 (2,)
和 (1,)
。这些多了2个和1个元素,那么(2,)
和(1,)
真的是指元素个数吗?然而,它们确实对应于维数。 ...?
import tensorflow as tf
nInputs = 6
nUnitsLayer1 = 3
weights = tf.Variable(tf.truncated_normal([nInputs, nUnitsLayer1]))
biases= tf.Variable(tf.zeros(nUnitsLayer1))
print 'nInputs', nInputs
print 'nUnitsLayer1', nUnitsLayer1
print 'weights shape', tf.shape(weights)
print 'biases shape', tf.shape(biases)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
print 'weights'
print (weights.eval())
print 'biases'
print (biases.eval())
输出:
nInputs 6
nUnitsLayer1 3
weights shape Tensor("Shape:0", shape=(2,), dtype=int32)
biases shape Tensor("Shape_1:0", shape=(1,), dtype=int32)
weights
[[ 0.16274141 -0.20140187 0.05342311]
[ 0.06554962 0.97961253 0.48603743]
[-0.49525094 -0.85534018 -0.49244919]
[ 0.09299681 -1.76659465 -0.64823383]
[ 0.98095983 1.53840697 0.55010611]
[-0.99781513 -1.18230617 0.22286507]]
biases
[ 0. 0. 0.]
TLDR;你需要 print sess.run(tf.shape
而不是 print tf.shape
"abstract" 张量与实际值之间存在差异。例如,tf.shape(x)
是一个 "abstract" 张量,表示 x
的形状。如果 x
具有二阶,则 tf.shape
将是具有 2 个元素的一维张量。此信息是在图形构建期间使用静态形状推断获得的,您可以通过在张量上使用 .get_shape()
方法来查看。 Tensor 的 __str__
方法似乎也使用了来自静态形状推断的信息,这就是您在打印时看到的信息。要得到一个张量的实际值,需要通过sess.run
在 Tensorflow 中,我的网络的最后一层看起来像:
layer3output = tf.nn.relu(tf.matmul(layer2output, weightsLayer3) + biasesLayer3)
如果我检查输出的大小:
print(tf.shape(layer4output))
我得到:
Tensor("Shape:0", shape=(2,), dtype=int32)
我以为layer3output
是一维向量,但上面的表示是二维的。额外维度是否对应于通过 feed_dict
提供的示例?
如果是这样,那么当使用feed_dict时,所有的Tensorflow变量是否都多了一维?
更新:请看下面的代码。它打印出 6x3 weights
张量和 1x3 biases
张量,并将其中的 tf.shape
分别显示为 (2,)
和 (1,)
。这些多了2个和1个元素,那么(2,)
和(1,)
真的是指元素个数吗?然而,它们确实对应于维数。 ...?
import tensorflow as tf
nInputs = 6
nUnitsLayer1 = 3
weights = tf.Variable(tf.truncated_normal([nInputs, nUnitsLayer1]))
biases= tf.Variable(tf.zeros(nUnitsLayer1))
print 'nInputs', nInputs
print 'nUnitsLayer1', nUnitsLayer1
print 'weights shape', tf.shape(weights)
print 'biases shape', tf.shape(biases)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
print 'weights'
print (weights.eval())
print 'biases'
print (biases.eval())
输出:
nInputs 6
nUnitsLayer1 3
weights shape Tensor("Shape:0", shape=(2,), dtype=int32)
biases shape Tensor("Shape_1:0", shape=(1,), dtype=int32)
weights
[[ 0.16274141 -0.20140187 0.05342311]
[ 0.06554962 0.97961253 0.48603743]
[-0.49525094 -0.85534018 -0.49244919]
[ 0.09299681 -1.76659465 -0.64823383]
[ 0.98095983 1.53840697 0.55010611]
[-0.99781513 -1.18230617 0.22286507]]
biases
[ 0. 0. 0.]
TLDR;你需要 print sess.run(tf.shape
而不是 print tf.shape
"abstract" 张量与实际值之间存在差异。例如,tf.shape(x)
是一个 "abstract" 张量,表示 x
的形状。如果 x
具有二阶,则 tf.shape
将是具有 2 个元素的一维张量。此信息是在图形构建期间使用静态形状推断获得的,您可以通过在张量上使用 .get_shape()
方法来查看。 Tensor 的 __str__
方法似乎也使用了来自静态形状推断的信息,这就是您在打印时看到的信息。要得到一个张量的实际值,需要通过sess.run