2

I have a placeholder with the shape that depends on another placeholder. How can I connect them during the placeholder initialization?

nUsers = tf.placeholder(tf.float32, [None, 1])
p = tf.placeholder(tf.float32, [None, 10 , ???] )

...in ??? I need to put the size given by the number present in nUsers for that item of the batch.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
D.Giunchi
  • 1,900
  • 3
  • 19
  • 23

1 Answers1

0

You can easily get the shape of the tensor with x.get_shape() (check the difference between tf.shape(x)). So you will need something like this:

import tensorflow as tf
import numpy as np
nUsers = tf.placeholder(tf.float32, [None, 1])
p = tf.placeholder(tf.float32, [None, 10 , nUsers.get_shape()[0]])

with tf.Session() as sess:
    print sess.run(p, {nUsers: np.ones((2, 1)), p: np.ones((3, 10, 2))})
Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 1
    in this code the shape of p is determined by the shape of nUsers. But what if the shape of p is determined by the value of nUsers? – D.Giunchi May 14 '17 at 17:16
  • @D.Giunchi this would be a completely different question with a different answer. Also in that question you will need to specify how do you want the value of a tensor to be used there. – Salvador Dali May 14 '17 at 21:18
  • 1
    but if you check my original question, it was mentioned that I needed the value from the nUsers and not the shape for determining another shape. nUsers is basically a number, and in each batch I got a vector of them. For each entry of the batch I have a number of users that did a particular task, and I use this number for determining the size of another placeholder. I went ahead and I try with None (since it varies) but I got other issues that make me think that I should consider that placeholder with a constant size. So my solution is to pad the second array and consider it of constant size. – D.Giunchi May 14 '17 at 22:23
  • @D.Giunchi I'm looking for the same as you. Did you solve this problem? Can you share the solution? – Diego Orellana May 27 '18 at 04:25