当Variable的第一维为None时使用tf.unpack() [英] Using tf.unpack() when first dimension of Variable is None

查看:164
本文介绍了当Variable的第一维为None时使用tf.unpack()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法填充动态形状的张量:

I'm feeding in a dynamic shaped Tensor using:

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

我需要将其转换为使用x_list = tf.unpack(x, 0)的具有shape=[1, 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)

但是它会引发ValueError,因为第一维的长度是未知的,即它是None.

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

我一直在尝试通过使用另一个tf.placeholder动态提供x的形状来解决此问题,但是参数shape不能为张量.

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.

在这种情况下如何使用tf.unpack()?

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?

先谢谢了.

推荐答案

我认为您不能unpack使用张数num未指定且不可推断的张量.正如他们的文档所述:

I don't think you can unpack a tensor with the argument num unspecified and non-inferrable. As their documentation says:

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

Raises ValueError if num is unspecified and cannot be inferred.

它与TensorFlow的内部设计如unpack的操作有关. Yaroslav Bulatov解释了其他胎面. >

It has something to do with how TensorFlow's internal design for operations like unpack. In this other tread, Yaroslav Bulatov explained

在图形构建期间,像unpack这样的操作会编译为"tensor-in/tensor-out"操作.

Operations like unpack compile into "tensor-in/tensor-out" ops during graph construction time.

因此TensorFlow需要知道num的特定值才能通过编译.

Hence TensorFlow needs to know the specific value of num to pass compiling.

尽管如此,我会尝试通过使用TensorArray来解决此问题. (有关说明,请参见下面的代码.)

Although, I'd try to get around this by using TensorArray. (see the following code for illustration).

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是用于包装动态大小的数组的类张量.在此应用程序TensorArr = tf.TensorArray(tf.int32, 1, dynamic_size=True, infer_shape=False)中初始化TensorArray对象时,请设置dynamic_size=Trueinfer_shape=False,因为占位符x的形状仅部分定义.

TensorArray is a class for wrapping dynamically sized arrays of Tensors. When initialize a TensorArray object in this application, 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.

要访问每个未打包的元素,请执行以下操作:

To access each unpacked element:

# 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)

然后在评估时:

# 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()

请注意,当尝试访问每个未打包的元素时,如果index值超出范围,则可以通过编译,但是在运行时会提示错误,提示索引超出范围.此外,解压缩后的张量的形状为TensorShape(None),因为x的形状仅在确定之前才被部分确定.

Note that when trying to access each unpacked element, if the index value is out of bound, you'd be able to pass the compiling but you'll get an error during runtime suggesting index out of bound. Additionally, the shape of the unpacked tensor would be TensorShape(None), since the shape of x is only partially determined until being evaluated.

这篇关于当Variable的第一维为None时使用tf.unpack()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆