Tensorflow重用变量

时间:2016-06-15 15:50:31

标签: python machine-learning neural-network artificial-intelligence tensorflow

我尝试使用tensorflow构建像素级分类LSTM RNN。我的模型显示在下图中。我遇到的问题是构建3D LSTM RNN。我已经构建了一个2D LSTM RNN的代码,所以我将代码放在一个循环中,但现在我收到以下错误:

ValueError: Variable RNN/BasicLSTMCell/Linear/Matrix does not exist, disallowed. Did you mean to set reuse=None in VarScope?

所以这就是网络:

enter image description here

这个想法是这样的......大小(200,200)的输入图像是大小为(200,200,200)的LSTM RNN的输入。从LSTM张量向量(LSTM RNN中的粉红色框)输出的每个序列被馈送到MLP,然后MLP进行单个输出预测 - 像素逐像素预测(您可以看到一个输入像素如何生成一个输出"像素"

所以这是我的代码:

...
n_input_x = 200
n_input_y = 200

x = tf.placeholder("float", [None, n_input_x, n_input_y])
y = tf.placeholder("float", [None, n_input_x, n_input_y])

def RNN(x):
    x = tf.transpose(x, [1, 0, 2])
    x = tf.reshape(x, [-1, n_input_x])
    x = tf.split(0, n_steps, x)

    output_matrix = []
    for i in xrange(200):
        temp_vector = []
        lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
        outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
        for j in xrange(200):
            lstm_vector = outputs[j]
            pixel_pred = multilayer_perceptron(lstm_vector, mlp_weights, mlp_biases)
            temp_vector.append(pixel_pred)
        output_matrix.append(temp_vector)
        print i

    return output_matrix

temp = RNN(x)
pred = tf.placeholder(temp, [None, n_input_x, n_input_y])
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
...

您可以看到我将调用放在第一个循环中的RNN。这样,我每次都会生成一个新的RNN。我知道Tensorflow自动增加其他Tensors。

调试我有

(Pdb) lstm_cell
<tensorflow.python.ops.rnn_cell.BasicLSTMCell object at 0x7f9d26956850>

然后对于输出我有200个BasicLSTMCells的向量

(Pdb) len(outputs)
200
...
<tf.Tensor 'RNN_2/BasicLSTMCell_199/mul_2:0' shape=(?, 200) dtype=float32>]

理想情况下,我希望第二次调用RNN来生成索引为200-399的LSTM张量

我尝试了这个,但它不会构建一个RNN,因为40000和x(分割后)的尺寸没有排成一行。

x = tf.reshape(x, [-1, n_input_x])
# Split to get a list of 'n_steps' tensors of shape (batch_size, n_hidden)
# This input shape is required by `rnn` function
x = tf.split(0, n_input_y, x)
lstm_cell = rnn_cell.BasicLSTMCell(40000, forget_bias=1.0, state_is_tuple=True)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)
output_matrix = []
for i in xrange(200):
    temp_vector = []
    for j in xrange(200):
        lstm_vector = outputs[i*j]

那么我也试图摆脱分裂,但随后它抱怨它必须是一个列表。所以我尝试重塑x = tf.reshape(x, [n_input_x * n_input_y]),但后来仍然说it must be a list

0 个答案:

没有答案