简体   繁体   English

如何将CNN输出作为RNN中的输入序列进行馈送(使用Tensorflow)

[英]How to feed CNN output as input series in a RNN (using Tensorflow)

I have implemented a simple CNN using tensorflow. 我已经使用tensorflow实现了一个简单的CNN。 Up until now I used to put a fully connected layer of size 2 at the last convolutional layer to distinct between my 2 classes. 到目前为止,我以前在最后一个卷积层上放置了一个大小为2的完全连接层,以区分我的2个类。 Now I want to feed that last convolutional layer to an RNN and then perform the classification. 现在,我要将最后一个卷积层馈送到RNN,然后执行分类。

The output of the last convolutional layer is called "cnn_output" and is a tensor of that form 最后一个卷积层的输出称为“ cnn_output”,并且是该形式的张量
<tf.Tensor 'Sigmoid_1:0' shape=(?, 168, 32) dtype=float32>

before feeding it to the rnn I split it using this command: 在将其喂入rnn之前,我使用以下命令将其拆分:
input_series = tf.split(axis=2, num_or_size_splits= 32, value = cnn_output)

the result of the split is 32 tensor of that type: 分割的结果是该类型的32张量:
<tf.Tensor 'split:0' shape=(?, 168, 1) dtype=float32>

the variables for the rnn are defined as below: rnn的变量定义如下:
cell = tf.contrib.rnn.BasesLSTMCell(32, state_is_tuple=True)
cell_state = tf.placeholder(tf.float32, [168,32])
hidden_state = tf.placeholder(tf.float32, [168,32])
init_state = tf.contrib.rnn.LSTMStateTuple( cell_state, hidden_state)

to feed the output of the cnn to the rnn I use this command 喂cnn的输出到rnn我使用此命令
states_series, current_state = tf.nn.static_rnn(cell, input_series, init_state)

At this line I get the following error 在这一行,我得到以下错误

ValueError: linear is expecting 2D arguments: [TensorShape([Dimension(None), Dimension(168), Dimension(1)]), TensorShape([Dimension(168), Dimension(32)])]
To me it seems that there is something wrong with that first unknown dimension of the tensors but I am not sure how to deal with it. 在我看来,张量的第一个未知维度似乎有问题,但我不确定如何处理。
I tried change the shape of the cell_state and hidden_state to [None,168,32] but didn't seem to help. 我尝试将cell_state和hidden_​​state的形状更改为[None,168,32],但似乎没有帮助。
I also tried to use the dynamic_rnn (not sure about the difference with static_rnn) but didn't work either 我也尝试使用dynamic_rnn(不确定与static_rnn的区别),但也没有用

EDIT UPDATE: I manage to run the code without error by reshaping the input_series of the rnn with this command: 编辑更新:我设法通过使用以下命令重塑rnn的input_series来运行代码而不会出现错误:

input_series = [tf.reshape(ipt,[-1,168 ])for ipt in input_series]

which resulted in tensors like this 导致像这样的张量

<tf.Tensor 'Reshape_21:0' shape=(?, 168) dtype=float32>

Although I am not sure yet if the rnn unit works properly. 虽然我不确定rnn单元是否正常工作。 I will check it out and update 我将检查并更新

I think your error comes from the fact that your placeholders are 2D and not 3D. 我认为您的错误来自您的占位符是2D而非3D的事实。 You forgot to add a batch dimension. 您忘记添加批次尺寸。

Try to define them like this: 尝试这样定义它们:

cell_state = tf.placeholder(tf.float32, [None,168,32])
hidden_state = tf.placeholder(tf.float32, [None,168,32])

(You might have to change the 32 to 1, given that your inputs have 1 channel each) (鉴于您的输入各有1个通道,您可能必须将32更改为1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM