Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tensorflow: how create an const tensor the same shape as a placeholder

I have a place holder for input:

Y = tf.placeholder(dtype=tf.float32, shape=(None, n_outputs))

Now I want to create a constant the same shape the Y:

w = Y.get_shape()
zero = tf.constant(np.zeros(w), dtype=tf.float32)

Error return:

__index__ returned non-int (type NoneType)
like image 728
Yan Zhao Avatar asked Dec 13 '22 19:12

Yan Zhao


2 Answers

Found the answer in another post tensorflow-constant-with-variable-size

zero = tf.fill(tf.shape(Y), 0.0)
like image 181
Yan Zhao Avatar answered Feb 22 '23 23:02

Yan Zhao


If you want to fill your tensor with zeros or ones, you can use the tf.zeros_like and tf.ones_like methods as shorthand for tf.fill.

a = tf.constant([0, 1, 2, 3, 4])

a_zeros = tf.zeros_like(a)
a_zeros
>>> <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 0, 0, 0, 0], dtype=int32)>
like image 28
ericburdett Avatar answered Feb 23 '23 00:02

ericburdett