简体   繁体   English

Keras LSTM 输入尺寸

[英]Keras LSTM input dimension

def train_model_lstm(train_x, train_y, classes): train_x, train_y = np.array(train_x), np.array(train_y) train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1)) def train_model_lstm(train_x, train_y, classes): train_x, train_y = np.array(train_x), np.array(train_y) train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))

data_dim = 1
timesteps = train_x.shape[1]

regressor = Sequential()

regressor.add(LSTM(units=50, return_sequences=True, input_shape=(timesteps, data_dim)))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units=50))
regressor.add(Dropout(0.2))

regressor.add(Dense(len(classes), activation='softmax'))
regressor.compile(optimizer='adam', loss='mean_squared_error')

regressor.fit(train_x, train_y, epochs=2, batch_size=32)

I'm trying to feed my network and using LSTM algorithm to my chatbot我正在尝试为我的网络提供数据并将 LSTM 算法用于我的聊天机器人
I got an error:我收到一个错误:

exception: Error when checking, expected lstm_1_input to have 3 dimensions, but got array with shape (367, 1)异常:检查时出错,预期 lstm_1_input 有 3 个维度,但得到了形状为 (367, 1) 的数组

Someone can advise how can i fix it?有人可以建议我该如何解决?

You may want to provide the shapes of your final train_x and train_y next time.下次您可能需要提供最终 train_x 和 train_y 的形状。

I think that's where the error is.我认为这就是错误所在。

Can you confirm that the shape of train_x is: (length of the dataset, timesteps, data_dim) and the train_y is (length of dataset, len(classes)) ?你能确认 train_x 的形状是:(数据集的长度,时间步长,data_dim) ,而 train_y 是(数据集的长度,len(类))

Also, I don't think that's the best choice for the loss metric since you're using a softmax for a classification problem.另外,我认为这不是损失度量的最佳选择,因为您使用 softmax 来解决分类问题。

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

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