【发布时间】:2019-01-17 07:19:27
【问题描述】:
这是我正在尝试创建的模型:
def build_model(inputs_size):
# create model
model = Sequential()
model.add(LSTM(100,activation="relu"))
model.add(Dense(100, input_dim=inputs_size, init='normal', activation='relu'))
model.add(Dense(200, input_dim=inputs_size, init='normal', activation='relu'))
model.add(Dense(100, input_dim=inputs_size, init='normal', activation='relu'))
model.add(Dense(3, init='normal', activation='relu'))
model.compile(loss=losses.mean_squared_logarithmic_error, optimizer='adam', metrics=['accuracy'])
return model
def save_model(model):
# saving model
json_model = model.to_json()
open('model_architecture.json', 'w').write(json_model)
# saving weights
model.save_weights('model_weights.h5', overwrite=True)
def load_model():
# loading model
model = model_from_json(open('model_architecture.json').read())
model.load_weights('model_weights.h5')
model.compile(loss=losses.mean_squared_logarithmic_error, optimizer='adam',metrics=["accuracy"])
return model
dataframe = pandas.read_csv("training.csv", header=0,index_col=0)
print(dataframe.columns)
dataset = dataframe.values
X = dataset[:,:-1].astype(float)
Y = dataset[:,-1]
X = preprocessing.scale(X)
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
y = np_utils.to_categorical(encoded_Y)
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1)
model = build_model(X.shape[1])
model.fit(X_train, Y_train, epochs=100, batch_size=10, verbose=True)
save_model(model)
错误是:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2
让我知道我错过了什么。我猜是输入形状的问题,但不知道该怎么做。
已编辑:示例数据集:training.csv
【问题讨论】:
-
向我们展示 X_train 的样本
-
当然...我将添加带有我忘记的问题的示例数据集。
-
Sequential模型中的第一层是LSTM。在 keras 中,第一层应该包含输入的形状,这在您的代码中不存在 -
@SreeramTP 我尝试将 th input_dim 与 LSTM 一起使用,但出现此错误:
ValueError: Error when checking input: expected lstm_2_input to have 3 dimensions, but got array with shape (1799, 6) -
您将二维序列输入网络,而 LSTM 需要 3D 序列。将输入更改为 one_hot 编码,然后将其传递给 LSTM 或使用嵌入层。
标签: python python-3.x tensorflow keras lstm