R.ali
R.ali

Reputation: 49

How to multiply dimensions of a tensor?

In the following code when I print conv_out.get_shape() it gives me output (1,14,14,1). I want to multiply the second third and fourth dimension (14*14*1). How I can do that?

input = tf.Variable(tf.random_normal([1,28,28,1]))
filter = tf.Variable(tf.random_normal([5,5,1,1]))

def conv2d(input,filter):
    return tf.nn.conv2d(input,filter,strides=[1,2,2,1],padding='SAME')

conv_out = conv2d(input,filter)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())

print conv_out.get_shape()
print conv_out.get_shape().as_list()[2]

Upvotes: 5

Views: 814

Answers (1)

Pietro Tortella
Pietro Tortella

Reputation: 1114

something like

import numpy as np
np.asarray(conv_out.get_shape().as_list()[1:]).prod()

should do the work.

Or, if you want it internally to the tensorflow graph, something like:

tf_shape = tf.shape(conv_out)
tf_shape_prod = tf.reduce_prod(tf_shape[1:])

Upvotes: 4

Related Questions