【发布时间】:2019-01-22 16:12:21
【问题描述】:
我有以下代码使用带有 TensorFlow 后端的 Keras 创建 LSTM 网络。 这段代码运行良好。
import numpy as np
import pandas as pd
from sklearn import model_selection
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.utils import np_utils
flights = {
'flight_stage': [1,0,1,1,0,0,1],
'scheduled_hour': [16,16,17,17,17,18,18],
'delay_category': [1,0,2,2,1,0,2]
}
columns = ['flight_stage', 'scheduled_hour', 'delay_category']
df = pd.DataFrame(flights, columns=columns)
X = df.drop('delay_category',1)
y = df['delay_category']
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.25, random_state=42)
nb_features = X_train.shape[1]
nb_classes = y.nunique()
hidden_neurons = 32
timestamps = X_train.shape[0]
# Reshape input data to 3D array
X_train = X_train.values.reshape(1, X_train.shape[0], X_train.shape[1])
X_test = X_test.values.reshape(1, X_test.shape[0], X_test.shape[1])
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(LSTM(
units=hidden_neurons,
return_sequences=True,
input_shape=(timestamps,nb_features)
)
)
model.add(Dropout(0.2))
model.add(Dense(activation='softmax', units=nb_classes))
model.compile(loss="categorical_crossentropy",
optimizer='adadelta')
但是当我开始训练模型时,它失败了:
history = model.fit(X_train, y_train, validation_split=0.25, epochs=500, batch_size=2, shuffle=True, verbose=0)
错误:
ValueError: Error when checking target: expected dense_19 to have 3 dimensions, but got array with shape (5, 3)
这个错误是指最后的 Dense 层。我使用model.summary() 来获得准确的尺寸。密集层的输出形状是(None, 5, 3)。
但是我不明白为什么它有 3 个维度以及 None 代表什么(它是如何出现在最后一层的)?
【问题讨论】:
-
特征和标签数据的形状是什么?
-
@edkeveked:形状如下:
X_train shape: (1, 5, 2)、X_test shape: (1, 2, 2)、y_train shape: (5,3)、y_test shape: (2,3)。
标签: python tensorflow keras lstm