ValueError with Dense() layer in Keras



>我正在尝试训练一个模型,该模型接受两个输入,将它们连接起来,并将结果输入LSTM。最后一层是 Dense(( 调用,目标是二进制向量(具有多个 1(。任务是分类。

我的输入序列是 50 行 23 个时间步长,具有 5625 个特征 (x_train (,我的补充输入(不是真正的序列(是 50 行长度为 23 (total_hours ( 的 onehot 行

我得到的错误是:

ValueError: Error when checking target: expected dense_1 to have shape (1, 5625) 
but got array with shape (5625, 1)

我的代码是:

import numpy as np
from keras.layers import LSTM, Dense, Input, Concatenate
from keras.models import Model
#CREATING DUMMY INPUT
hours_input_1 = np.eye(23)
hours_input_2 = np.eye(23)
hours_input_3 = np.pad(np.eye(4), pad_width=((0, 19), (0, 19)), mode='constant')
hours_input_3 = hours_input_3[:4,]
total_hours = np.vstack((hours_input_1, hours_input_2, hours_input_3))
seq_input = np.random.normal(size=(50, 24, 5625))
y_train = np.array([seq_input[i, -1, :] for i in range(50)])
x_train = np.array([seq_input[i, :-1, :] for i in range(50)])
#print 'total_hours', total_hours.shape #(50, 23)
#print 'x_train', x_train.shape #(50, 23, 5625)
#print 'y_train shape', y_train.shape #(50, 5625)
#MODEL DEFINITION
seq_model_in = Input(shape=(1,), batch_shape=(1, 1, 5625))
hours_model_in = Input(shape=(1,), batch_shape=(1, 1, 1))
merged = Concatenate(axis=-1)([seq_model_in, hours_model_in])
#print merged.shape #(1, 1, 5626) = added the 'hour' on as an extra feature
merged_lstm = LSTM(10, batch_input_shape=(1, 1, 5625), return_sequences=False, stateful=True)(merged)
merged_dense = Dense(5625, activation='sigmoid')(merged_lstm)
model = Model(inputs=[seq_model_in, hours_model_in], outputs=merged_dense)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#TRAINING
for epoch in range(10):
        for i in range(50):
                y_true = y_train[i,:]
                for j in range(23):
                        input_1 = np.expand_dims(np.expand_dims(x_train[i][j], axis=1), axis=1)
                        input_1 = np.reshape(input_1, (1, 1, x_train.shape[2]))
                        input_2 = np.expand_dims(np.expand_dims(np.array([total_hours[i][j]]), axis=1), axis=1)
                        tr_loss, tr_acc = model.train_on_batch([input_1, input_2], y_true)#np.array([y_true]))
                model.reset_states()

我的model.summary()如下所示:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (1, 1, 5625)         0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            (1, 1, 1)            0                                            
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (1, 1, 5626)         0           input_1[0][0]                    
                                                                 input_2[0][0]                    
__________________________________________________________________________________________________
lstm_1 (LSTM)                   (1, 10)              225480      concatenate_1[0][0]              
__________________________________________________________________________________________________
dense_1 (Dense)                 (1, 5625)            61875       lstm_1[0][0]                     
==================================================================================================
Total params: 287,355
Trainable params: 287,355
Non-trainable params: 0
__________________________________________________________________________________________________

我正在使用带有TensorFlow后端的Keras版本2.1.2(TensorFlow版本1.4.0。如何解决值错误?

事实证明,我需要解决目标,正如 ValueError 所暗示的那样。

如果替换:

y_true = y_train[i,:]

跟:

y_true_1 = np.expand_dims(y_train[i,:], axis=1)
y_true = np.swapaxes(y_true_1, 0, 1)

代码将运行。

最新更新