Chris
Chris

Reputation: 31206

Tensorflow: access shape of placeholder after NN layer in code

So, here is what I want to do:

Right now, I have padding = 'SAME' for all of my neural net layers. I would like to make my code more generic, so I can build my nets with arbitrary paddings, and I don't want to have to calculate how big the output tensors of the layers of my net are. I would like to just access the dimension at initialization/run time, the way the tf.nn functions apparently do internally, so I can initialize my weight and bias tensors in the correct dimension...

So,

How do I access the "shape" function/object of the output placeholder of a convolution?

Upvotes: 3

Views: 2413

Answers (1)

Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57913

There are two kinds of shapes -- tensor.get_shape() which gives static shape computed by Python wrappers during Graph construction (whenever possible), and tf.shape(tensor) which is an op that can be executed during runtime to get shape of the tensor (always possible). Both of these work for convolutions.

a = tf.Variable(tf.ones((1, 3, 3, 1)))
b = tf.Variable(tf.ones((3, 3, 1, 1)))
c = tf.nn_ops.conv2d(a, b, [1, 1, 1, 1], padding="VALID")
sess = create_session()
sess.run(tf.initialize_all_variables())
print c.get_shape()
print sess.run(tf.shape(c))

This gives

(1, 1, 1, 1)
[1 1 1 1]

Upvotes: 3

Related Questions