ValueError: This model has not yet been built. Build the model first by calling `build()` or calling

在使用keras构造lstm模型时遇到的报错,原代码:

def build_model():

    model = Sequential()

    model.add(Dropout(dropout_rate))

    model.add(LSTM(units=100,input_shape=(90,7)))

    model.add(Dropout(dropout_rate))

    model.add(Dense(units=1, activation='sigmoid'))

    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    # 输出模型的概要信息

    model.summary()

    return model

np.random.seed(seed=seed)

# 生产模型并训练模型
model = build_model()
model.fit(X_train, y_train, batch_size=batch_size, epochs=6, verbose=2)
scores = model.evaluate(X_test, y_test, verbose=2)
print('Accuracy: %.2f%%' % (scores[1] * 100))

 发生报错,原因是没有告诉模型输入的格式,需要在model.summary()上面写一句:

  model.build((None,90, 7))

 其中90是lstm的time_step ,7是input_dim,none这里个人认为代表样本数,本来输入的是batch size 但是也跑不通,参考stackoverflow回答改成了none,问题解决

你可能感兴趣的:(深度学习,keras,lstm)