当Variable的第一个维度为None时,使用tf.unpack()

Tae*_*gol 14 python machine-learning tensorflow

我用动态形状的Tensor喂食:

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

我需要把它变成一个有shape=[1, vector_size]使用的张量列表x_list = tf.unpack(x, 0)

但它提出了一个ValueError因为第一维的长度是未知的,即它是None.

我一直试图通过使用另一个tf.placeholder动态提供形状来绕过这个,x但参数shape不能是Tensor.

我怎样才能tf.unpack()在这种情况下使用?

或者是否还有另一个函数可以将我输入的变量转换为张量列表?

提前致谢.

Zho*_*ang 18

我不认为你可以用unpack一个num未指明和不可推论的论证来进行张量调整.正如他们的文件说:

如果num未指定且无法推断,则引发ValueError .

它与TensorFlow的内部设计如何操作有关unpack.在另一个方面,雅罗斯拉夫·布拉托夫解释道

操作,如unpack编译成在图施工时间"张量入/张出"欢声笑语.

因此,TensorFlow需要知道num通过编译的具体值.

虽然,我试图通过使用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)
Run Code Online (Sandbox Code Playgroud)

TensorArray用于包装动态大小的Tensors数组的类.初始化TensorArray此应用程序中的对象时TensorArr = tf.TensorArray(tf.int32, 1, dynamic_size=True, infer_shape=False),设置dynamic_size=True并且infer_shape=False因为占位符的形状x仅部分定义.

要访问每个解压缩的元素:

# access the first element
x_elem0 = x_array.read(0)
# access the last element
last_idx = tf.placeholder(tf.int32)
x_last_elem = x_array.read(last_idx)
Run Code Online (Sandbox Code Playgroud)

然后在评估时间:

# generate random numpy array
dim0 = 4
x_np = np.random.randint(0, 25, size=[dim0, 2])
print x_np
# output of print x_np
[[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()
Run Code Online (Sandbox Code Playgroud)

请注意,在尝试访问每个解压缩的元素时,如果index值超出范围,您将能够通过编译,但在运行时建议索引超出范围时会出现错误.另外,未包装的张量的形状将是TensorShape(None),因为在x评估之前仅部分地确定形状.

  • 目前[`解包()`](https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops/slicing_and_joining#unpack)已过时,可使用`拆散()`代替(上述代码仍然有效). (3认同)

dm0*_*m0_ 2

可能tf.dynamic_partition会有帮助,但它需要静态数量的输出张量。如果您可以确定张量的最大数量,那么您就可以使用它。

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.int32, shape=[None, 2])
data = np.random.randint(10, size=(10,2))

parts = range(len(data))
out = tf.dynamic_partition(x, parts, 20)

sess = tf.Session()
print 'out tensors:\n', out
print
print 'input data:\n', data
print
print 'sess.run result:\n', sess.run(out, {x: data})
Run Code Online (Sandbox Code Playgroud)

输出如下:

out tensors:
[<tf.Tensor 'DynamicPartition:0' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:1' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:2' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:3' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:4' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:5' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:6' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:7' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:8' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:9' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:10' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:11' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:12' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:13' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:14' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:15' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:16' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:17' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:18' shape=(?, 2) dtype=int32>,
 <tf.Tensor 'DynamicPartition:19' shape=(?, 2) dtype=int32>]
input data:
[[7 6]
 [5 1]
 [4 6]
 [4 8]
 [4 9]
 [0 9]
 [9 6]
 [7 6]
 [0 5]
 [9 7]]

sess.run result:
[array([[7, 3]], dtype=int32),
 array([[0, 5]], dtype=int32),
 array([[2, 3]], dtype=int32),
 array([[2, 6]], dtype=int32),
 array([[7, 9]], dtype=int32),
 array([[8, 2]], dtype=int32),
 array([[1, 5]], dtype=int32),
 array([[3, 7]], dtype=int32),
 array([[6, 7]], dtype=int32),
 array([[8, 1]], dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32),
 array([], shape=(0, 2), dtype=int32)]
Run Code Online (Sandbox Code Playgroud)