I am new to TensorFlow. I'm not able to understand how to create a dynamic "pythonic" list in TensorFlow. Basically, I perform some computation on a tensor object (train_data[i]
) and append it to a "list" X
, which I want to be a tensor with shape (100,)
I want to do something like this:
X = []
for i in range(100):
q = tf.log(train_data[i])
print(q) #Tensor("Log:0", shape=(), dtype=float32)
X.append(q)
I want X
to be a Tensor with shape (100,)
, basically a column vector which is a tensor object. If I run the code above, I instead get a python list of TensorObjects.
If you want to convert X
to a (100,) tensor you could add X = tf.stack(X)
after your for loop:
X = []
for i in range(100):
q = tf.log(train_data[i])
print(q) #Tensor("Log:0", shape=(), dtype=float32)
X.append(q)
X = tf.stack(X)
This is a useful construct where you may want to tf.unstack
some tensor, loop over the resulting list, and then use tf.stack
to get back to a single tensor.
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