[英]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.