user5793877
user5793877

Reputation:

Tensorflow. Converting unknown dimension size of a tensor to int

Assume we have

a = tf.placeholder(tf.float32, shape=(None, 3072))
b = a.get_shape()[0]

How can I convert b such that I can use it in further calculations, e.g. for given tensor T I will be able to create a new one, sth like

newT = T / b

Upvotes: 21

Views: 10367

Answers (2)

fabmilo
fabmilo

Reputation: 48310

You have to use a Graph operation:

a = tf.placeholder(tf.float32, shape=(None, 3072))
b = tf.shape(a)[0]

returns

<tf.Tensor 'strided_slice:0' shape=() dtype=int32>

while b = a.get_shape()[0] returns

Dimension(None)

Upvotes: 31

David Wong
David Wong

Reputation: 748

Your current way works already. I tried it with the following code and it works fine:

x = [[1,2,3],[4,5,6], [7,8,9]]
x = tf.constant(x)
size = x.get_shape()[0]
x /= size

with googlelog.Capture():
  p_op = tf.Print(x, [x], "output: ", summarize=10)
  sess.run(p_op)

With output:

output: [0 0 1 1 1 2 2 2 3]

Upvotes: -2

Related Questions