ValueError:检查目标时出错:预期density_19具有3个维度,但数组的形状为(5,3)

时间:2019-01-22 16:12:21

标签: python tensorflow keras lstm

我有以下代码,使用带有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代表什么(它如何出现在最后一层)?

1 个答案:

答案 0 :(得分:1)

3是最后一层返回的单位数。这是softmax激活的类数

5是lstm返回的单位数,表示返回的序列的大小

无是最后一层的批处理元素数。这仅表示最后一层可以接受每批形状为[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')

live code