Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a list and appending to it in TensorFlow

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.

like image 974
Abhinandan Dubey Avatar asked Feb 28 '17 21:02

Abhinandan Dubey


1 Answers

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.

like image 78
RobR Avatar answered Nov 13 '22 11:11

RobR