简体   繁体   English

Keras LSTM 模型

[英]Keras LSTM model

I cannot find a hands on tutorial on how to structure the data for use with keras LSTM.我找不到有关如何构建与 keras LSTM 一起使用的数据的动手教程。

Data数据

x_train = 7300 rows where each vector is length 64. x_train = 7300 行,其中每个向量的长度为 64。

y_train = array of 7300 items either 0's or 1's (the class). y_train = 7300 个项目的数组,0 或 1(类)。

Model模型

model = Sequential()
model.add(LSTM(200, dropout=0.2, recurrent_dropout=0.2, input_shape = (1, 64)))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x_train, y_train,
               epochs = 5,
               batch_size = 32,
               validation_split = 0.1,
               callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])

My question is simply, why doesn't this work?我的问题很简单,为什么这不起作用? Why isn't is as simple as giving an 2d array of vectors and similar length y values to fit.为什么不像给出一个二维向量数组和相似长度的 y 值来拟合那么简单。

Keras LSTM expects input of shape [batch_size, timesteps, features] . Keras LSTM 需要输入形状[batch_size, timesteps, features] Your data is of shape [batch_size, features] .您的数据的形状为[batch_size, features]

To add the timestep dimension (where number of timesteps is 1), do the following:要添加timestep维度(其中时间步长数为 1),请执行以下操作:

x_train = np.expand_dims(x_train, axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM