python - 如何在 Tensorflow 中结合 CNN 和 LSTM?
问题描述
如何正确结合 CNN 和 LSTM?
在这里,每个数据都是一个可变长度的列表,每个项目都有一个固定的形状 - (1, 9, 9, 1) 在下面的代码中。如果单个数据的长度为 N,则长度为 N 的列表应由单个 CNN返回(N 项以相同的权重(3*3 单个过滤器)和偏置变量通过 CNN),它应该是 LSTM 的输入, 迭代 N 次。
我想获得形状为 2 的 LSTM 的最后结果,用于分类为两个类。
# tf Graph input
# Can I set x as a (variable-length) list of vector with shape (1, 9, 9, 1)
x = tf.placeholder("float", [None, None, None, None]) # ?
y = tf.placeholder("float", [None, 2])
w_conv = tf.Variable(tf.truncated_normal([3, 3, 1, 1], stddev = 0.1, dtype = tf.float32), name = "w")
b_conv = tf.Variable(tf.constant(0.1, dtype = tf.float32, shape = [1]), name = "b")
# To be simple, let batch_size = 1
# data1 - x[0].shape = x[1].shape = (1, 9, 9, 1)
# data2 - x[0].shape = x[1].shape = x[2].shape = (1, 9, 9, 1)
# conv+relu
x_2 = tf.nn.relu(tf.nn.conv2d(x, w_conv, [1, 1, 1, 1], "SAME") + b_conv)
# data1 - x_2[0].shape = x_2[1].shape = (1, 9, 9, 1)
# data2 - x_2[0].shape = x_2[1].shape = x_2[2].shape = (1, 9, 9, 1)
# maxpool
x_3 = tf.nn.max_pool(x_2, [1, 3, 3, 1], [1, 3, 3, 1], "SAME")
# data1 - x_3[0].shape = x_3[1].shape = (1, 3, 3, 1)
# data2 - x_3[0].shape = x_3[1].shape = x_3[2].shape = (1, 3, 3, 1)
# lstm
x_4 = tf.reshape(x, [-1, 9]) # 9?
lstm_cell = rnn.BasicLSTMCell(2)
stacked = rnn.MultiRNNCell(lstm_cell)
outputs, states = tf.nn.dynamic_rnn(lstm_cell, x_4, dtype = tf.float32) # x_4?
y_ = tf.matmul(outputs[-1], tf.Variable(tf.random_normal([2, 2]))) + tf.Variable(tf.random_normal([2]))
# loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_, labels=y))
optimizer = tf.train.RMSPropOptimizer(learning_rate=1e-4).minimize(cost)
# Model evaluation
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
with tf.Session() as session:
sess.run([optimizer], feed_dict = {x:???, y:[0, 1]})
解决方案
推荐阅读
- java - 如何在 selenium 中为名为 onclick 的标签编写 xpath
- java - 使用 Single 过滤
- c# - ExecuteScalar 需要一个开放且可用的连接。连接的当前状态为关闭
- r - 创建 ggplot2 图表,其 y 刻度从零和美元格式开始
- php - 如何使用 PHP 和 MySQLI 按相关性排序结果?
- javascript - 地理位置地图 api v3 setinterval
- java - 如何在 JToolbar 中将一个按钮向右对齐?
- android - 在 mapbox 导航 NavigationView 中禁用音频语音指令
- android - 从 nativescript 调用 android 方法(sdk、aar 或 jar)
- python - nltk 包返回 TypeError:“LazyCorpusLoader”对象不可调用