2 views

I'm feeding in a dynamic shaped Tensor using:

x = tf.placeholder(tf.int32, shape=[None, vector_size])

I need to turn this into a list of Tensors that have shape=[1, vector_size] using x_list = tf.unpack(x, 0)

But it raises a ValueError because the length of the first dimension is not known i.e. it's None.

I've been trying to get around this by using another tf.placeholder to dynamically supply the shape of x but the parameter shape cannot be a Tensor.

How can I use tf.unpack() in this situation?

Or is there another function that can also turn the variable that I feed in into a list of Tensors?

by (33.1k points)
edited by

Simply use TensorArray:

import tensorflow as tf

import numpy as np

sess = tf.InteractiveSession()

# assume vector_size=2 for simplicity

x = tf.placeholder(tf.int32, shape=[None, 2])

TensorArr = tf.TensorArray(tf.int32, 1, dynamic_size=True, infer_shape=False)

x_array = TensorArr.unpack(x)

TensorArray is a class for wrapping dynamically sized arrays of Tensors. I initialize a

TensorArr = tf.TensorArray(tf.int32, 1, dynamic_size=True, infer_shape=False)

set dynamic_size=True and infer_shape=False since the shape of placeholder x is only partly defined.

# access the first element

# access the last element

last_idx = tf.placeholder(tf.int32)

Then at evaluation time:

# generate random numpy array

dim0 = 4

x_np = np.random.randint(0, 25, size=[dim0, 2])

print x_np

Output:

[[17 15]

[17 19]

[ 3  0]

[ 4 13]]

feed_dict = {x : x_np, last_idx : dim0-1} #python 0 based indexing

x_elem0.eval(feed_dict=feed_dict)

array([17, 15], dtype=int32) #output of x_elem0.eval(feed_dict)

x_last_elem.eval(feed_dict=feed_dict)

array([ 4, 13], dtype=int32) #output of x_last_elem.eval(feed_dict)

sess.close()

Hope this answer helps you! Machine Learning Tutorial is also required for a better understanding of the topic.

Learn TensorFlow with the help of this comprehensive video tutorial: