[英]ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (7942, 1)
[英]ValueError: Error when checking target: expected dense_19 to have 3 dimensions, but got array with shape (5, 3)
我有以下代码使用带有 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
代表什么(它是如何出现在最后一层的)?
3 是最后一层返回的单元数。 它是 softmax 激活的类数
5 是 lstm 返回的单元数,表示返回的序列的大小
None 是最后一层的批次元素数。 它只是意味着最后一层可以接受每批形状 [5, 3] 的张量的不同大小
X_train shape: (1, 5, 2),
X_test shape: (1, 2, 2),
y_train shape: (5,3),
y_test shape: (2,3)
查看数据形状,特征的批量大小与标签的批量大小之间显然存在不匹配。 最左边的数字在特征形状 X 和标签形状 y 之间应该相等。 它是批量大小。
'1', 5, 2 => batch size of 1
'2', 3 => batch size of 2
这里有一个不匹配。 同样为了解决 lstm 层的输出和最后一层的输入之间的问题,可以使用layer.flatten
nb_classes = 3
hidden_neurons = 32
model = Sequential()
model.add(LSTM(
units=hidden_neurons,
return_sequences=True,
input_shape=(5, 2)
)
)
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(activation='softmax', units=nb_classes))
model.compile(loss="categorical_crossentropy",
optimizer='adadelta')
model.compile(loss='categorical_crossentropy',
optimizer='adam')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.