【问题标题】:ValueError with Dense() layer in KerasKeras 中带有 Dense() 层的 ValueError
【发布时间】:2018-06-16 23:51:41
【问题描述】:

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

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

我得到的错误是:

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?

【问题讨论】:

    标签: keras


    【解决方案1】:

    事实证明我需要解决目标,正如 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)
    

    代码运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多