I have the following simple code in TensorFlow
:
a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = tf.shape(a)
t1, t2 = size
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(size, feed_dict = {a: np.random.randn(3, 4)})
t1
but it does not work. I want to have the shape of the tensor, a.shape
works fine but the point is that it gets None
for the second dimension. I searched and got that for knowing its value I have to use tf.shape(a)
, but now the problem is that I searched and figured out that python does have any idea what is in the tensor object. I just want to retrieve the values in two variables. The point is that I have to use this code in a more complicated code and these sizes are the marginal part of a larger computation. Is there anyway to get those numbers as integers without running the session for them separately?
I have to say that I know the following variant of my code wroks:
a = tf.placeholder(dtype = tf.float64, shape = (3, None))
b = tf.Variable(dtype = tf.float64, initial_value = np.random.randn(5, 3))
c = tf.matmul(b, a)
size = a.shape.as_list()
print(type(size))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
res = sess.run(c, feed_dict = {a: np.random.randn(3, 4)})
print(res)
print(size)
but It gets None
as the second element of shape. Consequently I have to use tf.shape
. Those folks who insist that my question is duplicate, I ran the suggestion here and get the following result which still consists of None
:
(3, ?)