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)
Found the answer in another post tensorflow-constant-with-variable-size
zero = tf.fill(tf.shape(Y), 0.0)
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)>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With