简体   繁体   English

`categorical_crossentropy` 损失中的 ValueError function:形状问题

[英]ValueError in `categorical_crossentropy` loss function: shape issue

I am trying to develop a bio-tagging name entity recognition (multi-class) model. I have 9 classes and converted it to one-hot encoding.我正在尝试开发生物标记名称实体识别(多类)model。我有 9 个类并将其转换为单热编码。 During the training I got following error:在培训期间,我收到以下错误:

ValueError: A target array with shape (2014, 120, 9) was passed for an output of shape (None, 9) while using as loss categorical_crossentropy . ValueError:形状为 (2014, 120, 9) 的目标数组被传递给形状为 (None, 9) 的 output,同时用作损失categorical_crossentropy This loss expects targets to have the same shape as the output.此损失预计目标具有与 output 相同的形状。

My code snippet:我的代码片段:

from keras.utils import to_categorical
y = [to_categorical(i, num_classes=n_tags) for i in y]  ### One hot encoding

input = Input(shape=(max_len,))
embed = Embedding(input_dim=n_words + 1, output_dim=50,
                  input_length=max_len, mask_zero=True)(input)  # 50-dim embedding
lstm = Bidirectional(LSTM(units=130, return_sequences=True,
                           recurrent_dropout=0.2))(embed)  # variational biLSTM
(lstm, forward_h, forward_c, backward_h, backward_c) = Bidirectional(LSTM(units=130, return_sequences=True, return_state=True, recurrent_dropout=0.2))(lstm)  # variational biLSTM
state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
context_vector, attention_weights = Attention(10)(lstm, state_h)   ### Attention mechanism  
output = Dense(9, activation="softmax")(context_vector)
model = Model(input, output)
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['categorical_accuracy'])
model.summary()

在此处输入图像描述

history = model.fit(X,np.array(y), batch_size=32, epochs=15,verbose=1)  
#### Got error message during training

Don't use one hot encoding and categorical_crossentropy .不要使用一种热编码和categorical_crossentropy Instead keep the y vector as it is and use sparse_categorical_crossentropy .而是保持 y 向量不变并使用sparse_categorical_crossentropy See if this works.看看这是否有效。

Refer https://stackoverflow.com/a/50135466/14337775参考https://stackoverflow.com/a/50135466/14337775

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow 'categorical_crossentropy' 中的 ValueError - ValueError in TensorFlow 'categorical_crossentropy' 损失为 NaN,使用激活 softmax 和损失 function categorical_crossentropy - Loss is NaN using activation softmax and loss function categorical_crossentropy ValueError: 形状为 (72148, 23) 的目标数组被传递给形状为 (None, 826, 23) 的 output,同时用作损失`categorical_crossentropy` - ValueError: A target array with shape (72148, 23) was passed for an output of shape (None, 826, 23) while using as loss `categorical_crossentropy` keras自定义生成器categorical_crossentropy修复输出形状问题 - keras custom generator categorical_crossentropy fix output shape issue 为什么model.fit()使用categorical_crossentropy损失函数通过tf.train.AdamOptimizer引发ValueError? - Why does model.fit() raise ValueError with tf.train.AdamOptimizer using categorical_crossentropy loss function? Keras Categorical_crossentropy 损失实现 - Keras Categorical_crossentropy loss implementation 形状为 (32, 3) 的目标数组被传递给形状为 (None, 15, 15, 3) 的 output,同时用作损失`categorical_crossentropy` - A target array with shape (32, 3) was passed for an output of shape (None, 15, 15, 3) while using as loss `categorical_crossentropy` 一个形状为 (11203, 25) 的目标数组被传递给形状为 (None, 3) 的输出,同时用作损失`categorical_crossentropy` - A target array with shape (11203, 25) was passed for an output of shape (None, 3) while using as loss `categorical_crossentropy` CNN 值错误输入形状,同时用作损失“categorical_crossentropy”。 此损失期望目标与输出具有相同的形状 - CNN value error input shape while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output python 神经网络损失 = 'categorical_crossentropy' vs 'binary_crossentropy' isse - python neural network loss = 'categorical_crossentropy' vs 'binary_crossentropy' isse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM