Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a list of lists into tensorflow

I wanted to know how to pass in a list of lists into a tensor in tensorflow. I was able to pass in arrays of [batch_size, seq_len] in where each item is just a number. But now I have a list of lists also of shape (batch_size, seq_len] but how do I pass this in?

I tried this:

self.inputs_X = [tf.placeholder(tf.int32, shape=[None, None],
                 name='inputs_X{0}'.format(i)) for i in xrange(SEQ_LEN)]

(* any way I can do this without explicitly passing in SEQ_LEN ?)

First of all the tf.int32, doesn't make sense because i'm not longer passing in an int, im passsing in a list. And so, I'm getting this error:

ValueError: setting an array element with a sequence.

Appreciate the help!

like image 729
Ashton Butcher Avatar asked Jan 27 '26 13:01

Ashton Butcher


1 Answers

TensorFlow's equivalent to a "list of x" is to increase the dimension of an existing tensor.

e.g. If you were passing in a list of [1, 2, 3, 4, 5] and you now want to pass in a list of these, you increase the dimensionality by 1 and end up with a matrix (aka a tensor of dimension 2).

tf.int32 still makes sense as your tensor still contains entirely ints.

It's hard to tell but I think you can simply add an extra element to your shape parameter, e.g. tf.placeholder(tf.int32, shape=[None, None, SEQ_LEN]) (replacing SEQ_LEN with None if appropriate). I haven't seen the data, so SEQ_LEN may need to be in a different position too.

Another strategy is to flatten your list-of-lists into a single list and then use tf.reshape to change it into the matrix or higher-dimensional shape you need.

like image 131
Mark McDonald Avatar answered Jan 30 '26 03:01

Mark McDonald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!