tflearn错误:“ strided_slice”的形状必须为等级1,但等级为2。我应该如何更改为正确的等级?

时间:2018-12-02 12:41:21

标签: python tensorflow rank tensor tflearn

我正在尝试使用tflearn创建CRNN(卷积递归神经网络),该输入17张28乘28的灰度图像。

网络是:

def rnn_conv_lstm_model(width, height, sequence_length):
   network = tflearn.input_data(shape=[None, sequence_length, height, width, 1])
   network = tflearn.reshape(network, [-1, height, width, 1])
   network = tflearn.conv_2d(network, 32, 3, activation='relu')
   network = tflearn.max_pool_2d(network, 2)
   network = tflearn.conv_2d(network, 64, 3, activation='relu')
   network = tflearn.conv_2d(network, 64, 3, activation='relu')
   network = tflearn.max_pool_2d(network, 2)
   network = tflearn.reshape(network, [-1, sequence_length, int((height/2**2)*(width/2**2)*64)])
   network = tflearn.lstm(network, 256)
   network = tflearn.fully_connected(network, 2, activation='sigmoid')
   network = tflearn.regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001, name="output1")
   return network

然后将此模型输入:

get_input_data()
# train the model
model = tflearn.DNN(rnn_conv_lstm_model(28, 28, 17), tensorboard_verbose=0)
model.fit(X_input_data, Y_labels, show_metric=True, batch_size=None, snapshot_step=None, n_epoch=1)

我得到的2个错误是“线程Thread-8中的异常”,如下所示:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [1,2], [1,17], [1,17], [1].
During handling of the above exception, another exception occurred:
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [1,2], [1,17], [1,17], [1].

从环顾四周和研究的角度来看,我相当确定等级代表维度的数量。结果,由于上述误差中的每个输入形状都以1开头,因此我认为应该可以相对容易地将它们重新成形为等级1的形状。话虽如此,我不确定该怎么做,甚至不确定这个想法是否正确。

在将X_input和Y_label馈入网络之前,我已经查看它们的形状,它们如下:

X_inputs shape:
(17, 1)
X_inputs:
[[<tf.Tensor 'Squeeze_35:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_26:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_30:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_27:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_31:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_28:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_29:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_34:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_36:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze_37:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]
 [<tf.Tensor 'Squeeze:0' shape=(28, 28, 1) dtype=int32>]]


Y_label shape:
(1, 2)
Y_label:
Tensor("one_hot_2:0", shape=(1, 2), dtype=float32)

通过此功能获取数据:

def get_input_data():
   global X_input_data
   X_input_data = generate_images()
   X_input_data = np.array(X_input_data)
   X_input_data = X_input_data.reshape(17, 1)
   #X_input_data = X_input_data.reshape(-1, 17, 1)  -- if using this, error declares input shapes to be [1,2], [1,1], [1,1], [1]

   global Y_labels
   Y_labels = y

我已经坚持了一段时间,现在需要尽快完成CRNN。我希望它可以运行的时间比1个多,但是我只是想让它现在完全运行。

如果有人可以帮助我解决这个问题,将不胜感激。如果您需要更多信息/代码,那么我可以提供。

0 个答案:

没有答案