Ron Cohen
Ron Cohen

Reputation: 2925

Tensorflow feed_dict adds extra dimension to variables?

In Tensorflow, the final layer of my network looks like:

layer3output = tf.nn.relu(tf.matmul(layer2output, weightsLayer3) + biasesLayer3)

If I check the size of the output:

print(tf.shape(layer4output))

I get:

Tensor("Shape:0", shape=(2,), dtype=int32)

I expected that layer3output is a 1D vector, but the above indicates 2D. Does the extra dimension correspond to examples fed via feed_dict?

If so, then do all Tensorflow variables have one extra dimension when feed_dict is used?

UPDATE: Please see the code below. It prints out the 6x3 weights tensor and 1x3 biases tensors, and shows the tf.shape of these as (2,) and (1,) respectively. These have more than 2 and 1 elements, so do (2,) and (1,) really mean the number of elements? They do however correspond to the number of dimensions. ...?

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())

OUTPUT:

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.]

Upvotes: 0

Views: 384

Answers (1)

Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57913

TLDR; you need to print sess.run(tf.shape rather than print tf.shape

There's a difference between an "abstract" tensor and it's a actual value. For instance, tf.shape(x) is an "abstract" Tensor that represents shape of x. If x has rank two, then tf.shape will be a 1-D Tensor with 2 elements. This information is obtained during graph construction time using static shape inference, which you can see by using .get_shape() method on a Tensor. It seems __str__ method of Tensor also uses information from static shape inference, which is what you see when you print it. To get the actual value of a tensor, you need to put it through sess.run

Upvotes: 1

Related Questions