【问题标题】:Error when checking target: expected dense_21 to have 2 dimensions, but got array with shape (2300, 4, 54)检查目标时出错:预期 dense_21 有 2 个维度,但得到了形状为 (2300, 4, 54) 的数组
【发布时间】:2020-07-02 05:51:31
【问题描述】:

当给定一个预测变量向量时,我遇到了一个处理预测四个输出的问题。它在 LSTM 层输入处引发错误。

我有

X.shape,Y.shape = ((2300, 36, 768), (2300, 4, 54))


# Core Part

checkpoint = ModelCheckpoint('model-{epoch:03d}-{acc:03f}-{val_acc:03f}.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')  

data_dim = 768
timesteps = X.shape[1]
num_classes = 10


# # expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()

model.add(LSTM(256, return_sequences=True, input_shape=(timesteps, data_dim))) 
model.add(LSTM(64, return_sequences=True))  
model.add(LSTM(32)) 

model.add(Dense(54, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='rmsprop',  metrics=['accuracy'])

model.fit(X, Y, batch_size=64, epochs=10, validation_split=0.1)

当我有X.shape,Y.shape = ((2300, 36, 768), (2300, 15))时,上面的代码工作正常

我该如何克服这个问题,如果我有超过一个、四个或十个预测输出,我该如何设置 LSTM 和 DENSE 层?

提前致谢。

【问题讨论】:

    标签: python keras deep-learning neural-network lstm


    【解决方案1】:

    如果你的输出不是一个序列,你可以使用Reshape层如下:

    from keras.models import Sequential
    from keras.layers import LSTM, Reshape, Dense, Activation
    model = Sequential()
    
    model.add(LSTM(256, return_sequences=True, input_shape=(100, 32))) 
    model.add(LSTM(64, return_sequences=True))  
    model.add(LSTM(32))
    
    # now we have a vector of length 32 and
    # we want the dense layer to have 4*54=216 neurons so that
    # we will be able to reshape it to a matrix of shape (4, 54) in
    # the next (Reshape) layer.
    model.add(Dense(4*54))
    model.add(Reshape((4, 54)))
    
    model.add(Activation('softmax'))
    
    model.summary()
    

    将打印:

    Model: "sequential_1"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    lstm_1 (LSTM)                (None, 100, 256)          295936    
    _________________________________________________________________
    lstm_2 (LSTM)                (None, 100, 64)           82176     
    _________________________________________________________________
    lstm_3 (LSTM)                (None, 32)                12416     
    _________________________________________________________________
    dense_1 (Dense)              (None, 216)               7128      
    _________________________________________________________________
    reshape_1 (Reshape)          (None, 4, 54)             0         
    _________________________________________________________________
    activation_1 (Activation)    (None, 4, 54)             0         
    =================================================================
    Total params: 397,656
    Trainable params: 397,656
    Non-trainable params: 0
    

    【讨论】:

    • 感谢@Bashir 的解决方案。您能否详细解释一下,为什么要使用 model.add(Dense(4*54)) model.add(Reshape((4, 54))) ??
    • 我添加了关于密集层和重塑层的 cmets
    【解决方案2】:

    如果您希望获得 4 个输出,那么您的最终 Dense 层应该有 4 个神经元,而不是 54,即,

    model.add(Dense(54, activation='softmax'))

    应该是:

    model.add(Dense(4, activation='softmax'))

    另外,您的 Y 的形状应该是 (number_of_examples, 4),在您的情况下应该是

    (2300, 4) 代替 (2300, 4, 54)

    如果您实际上有一个形状为(2300, 4, 54)Y,那么您不能在输出中使用Dense 层,因为Dense 层仅适用于一维示例每个向量。

    【讨论】:

    • 那么有什么替代方法吗??
    • 单独添加了一个答案
    • 坦克巴希尔,你能解释一下为什么你添加了 model.add(Dense(4*54)) model.add(Reshape((4, 54))),我需要知道。正如你所提到的,它工作得很好。
    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 2019-07-31
    • 2019-03-25
    • 1970-01-01
    • 2019-11-13
    • 2019-03-02
    • 1970-01-01
    • 2018-04-11
    相关资源
    最近更新 更多