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

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

问题描述

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

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

我需要使用 x_list = tf.unpack(x, 0)

将其转换为具有 shape=[1, vector_size] 的张量列表>

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

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

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

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

提前致谢.

解决方案

我不认为你可以 unpack 带有参数 num 未指定和不可推断的张量.正如他们的文档所说:

<块引用>

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

这与 TensorFlow 的内部设计如何进行 unpack 等操作有关.在这个其他步骤中,Yaroslav Bulatov 解释了><块引用>

unpack 等操作在图构建期间编译为张量输入/张量输出"操作.

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

虽然,我会尝试通过使用 TensorArray 来解决这个问题.(请参阅以下代码进行说明).

 将 tensorflow 导入为 tf将 numpy 导入为 npsess = tf.InteractiveSession()# 为简单起见,假设 vector_size=2x = 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 是一个类包装动态大小的张量数组.在本应用中初始化TensorArray对象时,TensorArr = tf.TensorArray(tf.int32, 1, dynamic_size=True, infer_shape=False),设置dynamic_size=Trueinfer_shape=False 因为占位符 x 的形状只是部分定义.

访问每个解压的元素:

# 访问第一个元素x_elem0 = x_array.read(0)# 访问最后一个元素last_idx = tf.placeholder(tf.int32)x_last_elem = x_array.read(last_idx)

然后在评估时间:

#生成随机的numpy数组昏暗0 = 4x_np = np.random.randint(0, 25, size=[dim0, 2])打印 x_np# 打印 x_np 的输出[[17 15][17 19][ 3 0][ 4 13]]feed_dict = {x : x_np, last_idx : dim0-1} #python 0 基于索引x_elem0.eval(feed_dict=feed_dict)array([17, 15], dtype=int32) #x_elem0.eval(feed_dict)的输出x_last_elem.eval(feed_dict=feed_dict)array([ 4, 13], dtype=int32) #x_last_elem.eval(feed_dict)的输出sess.close()

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

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?

Thanks in advance.

解决方案

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

Raises ValueError if num is unspecified and cannot be inferred.

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

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

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

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

Then at evaluation time:

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

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.

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

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