我正在尝试使用 Tensorflow 构建一个能够处理 3D 数据的 LSTM RNN。根据这篇论文,网格 LSTM RNN 可以是 n 维的。我的网络设计思路是有一个 3D 体数据 [depth, x, y]
,网络结构应为 [depth, x, y, n_hidden]
,其中 n_hidden
是 LSTM 单元的递归调用次数。想法是每个像素点都有自己的 LSTM 递归调用“字符串”。
输出的形状应为 [depth, x, y, n_classes]
。我正在进行二元分割——可以理解为前景和背景,所以类别的数量只有 2 个。
# 网络参数
n_depth = 5
n_input_x = 200 # MNIST 数据输入(图像形状:28*28)
n_input_y = 200
n_hidden = 128 # 隐藏层特征数量
n_classes = 2
# tf 图输入
x = tf.placeholder("float", [None, n_depth, n_input_x, n_input_y])
y = tf.placeholder("float", [None, n_depth, n_input_x, n_input_y, n_classes])
# 定义权重
weights = {}
biases = {}
# 初始化权重
for i in xrange(n_depth * n_input_x * n_input_y):
weights[i] = tf.Variable(tf.random_normal([n_hidden, n_classes]))
biases[i] = tf.Variable(tf.random_normal([n_classes]))
def RNN(x, weights, biases):
# 准备数据形状以匹配 `rnn` 函数要求
# 当前数据输入形状:(batch_size, n_input_y, n_input_x)
# 置换 batch_size 和 n_input_y
x = tf.reshape(x, [-1, n_input_y, n_depth * n_input_x])
x = tf.transpose(x, [1, 0, 2])
# 重塑为 (n_input_y*batch_size, n_input_x)
x = tf.reshape(x, [-1, n_input_x * n_depth])
# 分割以获得形状为 (batch_size, n_hidden) 的 'n_input_y' 个张量列表
# 这是 `rnn` 函数所需的输入形状
x = tf.split(0, n_depth * n_input_x * n_input_y, x)
# 使用 tensorflow 定义一个 lstm 单元
lstm_cell = grid_rnn_cell.GridRNNCell(n_hidden, input_dims=[n_depth, n_input_x, n_input_y])
# lstm_cell = rnn_cell.MultiRNNCell([lstm_cell] * 12, state_is_tuple=True)
# lstm_cell = rnn_cell.DropoutWrapper(lstm_cell, output_keep_prob=0.8)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
# 使用 rnn 内部循环的最后输出进行线性激活
# pdb.set_trace()
output = []
for i in xrange(n_depth * n_input_x * n_input_y):
# 这里我需要对 outputs[i] 进行某种重塑
output.append(tf.matmul(outputs[i], weights[i]) + biases[i])
return output
pred = RNN(x, weights, biases)
pred = tf.transpose(tf.pack(pred),[1,0,2])
pred = tf.reshape(pred, [-1, n_depth, n_input_x, n_input_y, n_classes])
# pdb.set_trace()
temp_pred = tf.reshape(pred, [-1, n_classes])
n_input_y = tf.reshape(y, [-1, n_classes])
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(temp_pred, n_input_y))
目前我遇到了错误:TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
这个错误发生在 RNN 初始化之后:outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
x
的类型当然是 float32
我无法确定 GridRNNCell
返回的类型是什么,能在这里提供帮助吗?这可能是问题所在。我是否应该为它定义更多的参数?input_dims
是有意义的,但 output_dims
应该是什么?
这是 contrib
代码中的一个错误吗?
GridRNNCell 位于 contrib/grid_rnn/python/ops/grid_rnn_cell.py
回答:
您使用的是哪种版本的网格 LSTM 单元?
如果您使用的是 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/rnn/python/ops/rnn_cell.py
我认为您可以尝试初始化 ‘feature_size’ 和 ‘frequency_skip’。另外,我认为可能存在另一个错误。将动态形状输入到这个版本中可能会导致 TypeError