【问题标题】:Keras dense layer shape mismatchKeras 密集层形状不匹配
【发布时间】:2017-08-07 04:30:13
【问题描述】:

我正在尝试在 Keras 中创建一个多类分类器,但我在 Dense 层中发现尺寸不匹配。

MAX_SENT_LENGTH = 100
MAX_SENTS = 15
EMBEDDING_DIM = 100

x_train = data[:-nb_validation_samples]
y_train = labels[:-nb_validation_samples]
x_val = data[-nb_validation_samples:]
y_val = labels[-nb_validation_samples:]

embedding_layer = Embedding(len(word_index) + 1,
                            EMBEDDING_DIM,
                            weights=[embedding_matrix],
                            input_length=MAX_SENT_LENGTH,
                            trainable=True)

sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sentence_input)
l_lstm = Bidirectional(LSTM(100))(embedded_sequences)
sentEncoder = Model(sentence_input, l_lstm)

review_input = Input(shape=(MAX_SENTS,MAX_SENT_LENGTH), dtype='int32')
review_encoder = TimeDistributed(sentEncoder)(review_input)
l_lstm_sent = Bidirectional(LSTM(100))(review_encoder)
preds = Dense(7, activation='softmax')(l_lstm_sent)
model = Model(review_input, preds)

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])

model.fit(x_train, y_train, validation_data=(x_val, y_val), 
            epochs=10, batch_size=50)

类标签已正确转换为 1-hot 向量,但在尝试拟合模型时,出现此不匹配错误:

('Shape of data tensor:', (5327, 15, 100))
('Shape of label tensor:', (5327, 7))
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 15, 100)           0         
_________________________________________________________________
time_distributed_1 (TimeDist (None, 15, 200)           351500    
_________________________________________________________________
bidirectional_2 (Bidirection (None, 200)               240800    
_________________________________________________________________
dense_1 (Dense)              (None, 7)                 1407       
=================================================================
Total params: 592,501
Trainable params: 592,501
Non-trainable params: 0
_________________________________________________________________
None
ValueError: Error when checking target: expected dense_1 to have 
              shape (None, 1) but got array with shape (4262, 7)

这个 (None, 1) 维度是从哪里来的,我该如何解决这个错误?

【问题讨论】:

    标签: python keras


    【解决方案1】:

    如果您的标签是一次性编码的,则应使用 loss='categorical_crossentropy' 而不是 loss='sparse_categorical_crossentropy''sparse_categorical_crossentropy' 采用整数标签,这就是为什么需要 (None,1) 维度。

    【讨论】:

    • 我落入了同样的“陷阱”。伙计,这并不明显,甚至没有记录在 keras.io/losses 。无论如何谢谢!
    猜你喜欢
    • 2017-12-19
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2020-05-22
    相关资源
    最近更新 更多