【发布时间】:2016-11-21 08:58:55
【问题描述】:
我正在尝试构建一个在 Tensorflow 中处理 3D 数据的 LSTM RNN。在this 论文中,Grid LSTM RNN 可以是 n 维的。我的网络的想法是有一个 3D 体积 [depth, x, y] 并且网络应该是 [depth, x, y, n_hidden] 其中 n_hidden 是 LSTM 单元递归调用的数量。这个想法是每个像素都有自己的 LSTM 递归调用“字符串”。
输出应该是[depth, x, y, n_classes]。我正在做一个二进制分割——考虑前景和背景,所以类的数量只有 2 个。
# Network Parameters
n_depth = 5
n_input_x = 200 # MNIST data input (img shape: 28*28)
n_input_y = 200
n_hidden = 128 # hidden layer num of features
n_classes = 2
# tf Graph input
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])
# Define weights
weights = {}
biases = {}
# Initialize weights
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):
# Prepare data shape to match `rnn` function requirements
# Current data input shape: (batch_size, n_input_y, n_input_x)
# Permuting batch_size and n_input_y
x = tf.reshape(x, [-1, n_input_y, n_depth * n_input_x])
x = tf.transpose(x, [1, 0, 2])
# Reshaping to (n_input_y*batch_size, n_input_x)
x = tf.reshape(x, [-1, n_input_x * n_depth])
# Split to get a list of 'n_input_y' tensors of shape (batch_size, n_hidden)
# This input shape is required by `rnn` function
x = tf.split(0, n_depth * n_input_x * n_input_y, x)
# Define a lstm cell with tensorflow
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)
# Linear activation, using rnn inner loop last output
# pdb.set_trace()
output = []
for i in xrange(n_depth * n_input_x * n_input_y):
#I'll need to do some sort of reshape here on 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
【问题讨论】:
-
我也只是在浏览这个界面。我不知道你的问题的答案,但 input_dims 不应该是维度索引而不是大小吗?
-
@jstaker7 正确,input_dims 不正确。虽然我必须说我不知道什么是正确的。你明白这个参数吗?
-
还不确定。我已要求对原始拉取请求进行澄清:github.com/tensorflow/tensorflow/pull/1665
-
@jstaker7 你成功实现了吗?
-
我还没有实现任何东西,但确实得到了一些澄清。请参阅上面链接的拉取请求。
标签: python machine-learning tensorflow neural-network lstm