我正在尝试构建一个LSTM RNN来处理Tensorflow中的3D数据。从this纸,网格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
代码中的错误吗?grid rnn cell位于contrib/grid_nrnn/python/ops/grid_n_cell.py
最佳答案
您正在使用哪个版本的网格LSTM单元?
如果您使用https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/rnn/python/ops/rnn_cell.py
我想你可以尝试初始化“功能大小”和“频率跳过”。
此外,我认为可能存在另一种错误。将动态形状馈送到此版本中可能会导致类型错误