在 tensorflow 中解包(unstack)一个具有一个 None 维度的输入(占位符) [英] unpack(unstack) an input (placeholder) with one None dimension in tensorflow

查看:31
本文介绍了在 tensorflow 中解包(unstack)一个具有一个 None 维度的输入(占位符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 LSTM 与具有不同时间步长(不同帧数)的输入一起使用.rnn.static_rnn 的输入应该是一个 tf 序列(不是 tf!).所以,我应该将我的输入转换为序列.我尝试使用 tf.unstack 和 tf.split,但它们都需要知道输入的确切大小,而我的输入(时间步长)的一个维度因不同的输入而变化.以下是我的代码的一部分:

I am trying to use LSTM with inputs with different time steps (different number of frames). The input to the rnn.static_rnn should be a sequence of tf (not a tf!). So, I should convert my input to sequence. I tried to use tf.unstack and tf.split, but both of them need to know exact size of inputs, while one dimension of my inputs (time steps) is changing by different inputs. following is part of my code:

n_input = 256*256 # data input (img shape: 256*256)
n_steps = None # timesteps
batch_size = 1
# tf Graph input
x = tf.placeholder("float", [ batch_size , n_input,n_steps])
y = tf.placeholder("float", [batch_size, n_classes])
# Permuting batch_size and n_steps
x1 = tf.transpose(x, [2, 1, 0])
x1 = tf.transpose(x1, [0, 2, 1])
x3=tf.unstack(x1,axis=0)
#or x3 = tf.split(x2, ?, 0)
# Define a lstm cell with tensorflow
lstm_cell = rnn.BasicLSTMCell(num_units=n_hidden, forget_bias=1.0)

# Get lstm cell output
outputs, states = rnn.static_rnn(lstm_cell, x3, dtype=tf.float32,sequence_length=None)

我在使用 tf.unstack 时遇到以下错误:

I got following error when I am using tf.unstack:

ValueError: 无法从形状推断 num (?, 1, 65536)

ValueError: Cannot infer num from shape (?, 1, 65536)

此外,还有一些讨论此处此处,但它们都没有用于我.任何帮助表示赞赏.

Also, there are some discussions here and here, but none of them were useful for me. Any help is appreciated.

推荐答案

此处,如果参数未指定且不可推断,则 tf.unstack 不起作用.

As explained in here, tf.unstack does not work if the argument is unspecified and non-inferrable.

在您的代码中,经过转置后,x1 具有 [ n_steps, batch_size, n_input] 的形状及其在 axis=0 处的值设置为 None.

In your code, after transpositions, x1 has the shape of [ n_steps, batch_size, n_input] and its value at axis=0 is set to None.

这篇关于在 tensorflow 中解包(unstack)一个具有一个 None 维度的输入(占位符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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