nak*_*nis 5 python theano tensorflow rnn
我正在尝试将张量从[A, B, C, D]into重塑[A, B, C * D]并将其输入到dynamic_rnn. 假设我事先不知道 B、C 和 D(它们是卷积网络的结果)。
我认为在 Theano 中,这种重塑看起来像这样:
x = x.flatten(ndim=3)
Run Code Online (Sandbox Code Playgroud)
似乎在 TensorFlow 中没有简单的方法可以做到这一点,到目前为止,这是我想出的:
x_shape = tf.shape(x)
x = tf.reshape(x, [batch_size, x_shape[1], tf.reduce_prod(x_shape[2:])]
Run Code Online (Sandbox Code Playgroud)
即使当的形状x曲线建筑物中是已知的(即,print(x.get_shape())打印出的绝对值,如[10, 20, 30, 40]整形后get_shape()变得[10, None, None]再次,仍然假定初始形状是未知的,所以我不能与绝对值操作。
当我传递x给 a 时,dynamic_rnn它失败了:
ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None.
Run Code Online (Sandbox Code Playgroud)
为什么reshape无法处理这个案子?flatten(ndim=n)在 TensorFlow 中使用 4 级或更高级别的张量复制 Theano 的正确方法是什么?
我根据你的要求尝试了一个简单的代码。由于您正在尝试重塑 CNN 输出,因此 X 的形状与 Tensorflow 中 CNN 的输出相同。
HEIGHT = 100
WIDTH = 200
N_CHANELS =3
N_HIDDEN =64
X = tf.placeholder(tf.float32, shape=[None,HEIGHT,WIDTH,N_CHANELS],name='input') # output of CNN
shape = X.get_shape().as_list() # get the shape of each dimention shape[0] =BATCH_SIZE , shape[1] = HEIGHT , shape[2] = HEIGHT = WIDTH , shape[3] = N_CHANELS
input = tf.reshape(X, [-1, shape[1] , shape[2] * shape[3]])
print(input.shape) # prints (?, 100, 600)
#Input for tf.nn.dynamic_rnn should be in the shape of [BATCH_SIZE, N_TIMESTEPS, INPUT_SIZE]
#Therefore, according to the reshape N_TIMESTEPS = 100 and INPUT_SIZE= 600
#create the RNN here
lstm_layers = tf.contrib.rnn.BasicLSTMCell(N_HIDDEN, forget_bias=1.0)
outputs, _ = tf.nn.dynamic_rnn(lstm_layers, input, dtype=tf.float32)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
7105 次 |
| 最近记录: |