Keras LSTM输入ValueError:输入0与lstm_1层不兼容:预期ndim = 3,找到的ndim = 4

时间:2019-03-27 21:16:16

标签: python input keras lstm

运行代码时,出现此错误: ValueError:输入0与lstm_1层不兼容:预期ndim = 3,找到ndim = 4

def keras_experiment(true_examples,true_labels):
    # print len(true_examples[1])
    x_train = np.array(true_examples[:700])
    y_train = np.array(true_labels[:700])
    x_test = np.array(true_examples[700:])
    y_test = np.array(true_labels[700:])

    from keras.models import Sequential
    from keras.layers import LSTM, Dense
    print x_train[0]
    data_dim = 378
    timesteps = 7
    num_classes = 2

    # expected input data shape: (batch_size, timesteps, data_dim)
    model = Sequential()
    model.add(LSTM(32, return_sequences=False,
                   input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
    model.add(Dense(10, activation='softmax'))

    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])

    model.fit(x_train[0], y_train[0],
              batch_size=64, epochs=5,
              validation_data=(x_test, y_test))

    score = model.evaluate(x_test, y_test, batch_size=16)
    print score

这是打印x_train [0]的结果:

[[0.82183125 0.45548045 0.86122581 ... 3.16044199 1.43419966 0.45379718]
 [0.84371381 0.47813553 0.83602898 ... 2.64684385 1.58629507 0.5993157 ]
 [0.72253171 0.42504681 0.88999478 ... 2.09510967 1.87146875 0.89325575]
 ...
 [0.79126543 0.45734966 0.85694022 ... 2.68172079 1.54250728 0.57519309]
 [0.79846062 0.41452213 0.72903777 ... 2.492895   1.53964412 0.6176129 ]
 [0.8246961  0.39966809 0.52778689 ... 2.02451504 1.42316496 0.70296586]]

所以看起来像我想的那样,列出了700个示例,列出了7个时间步长,列出了378个功能

0 个答案:

没有答案