How to multiply tensor sizes?

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 and fourth dimension (14*14*1). How can i do this?

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]
+4
source share
1 answer

sort of

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

must do the job.

Or, if you want it to be inside the tensor graph, something like:

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

Source: https://habr.com/ru/post/1678214/


All Articles