【问题标题】:ValueError in `categorical_crossentropy` loss function: shape issue`categorical_crossentropy` 损失函数中的 ValueError:形状问题
【发布时间】:2021-02-26 10:17:03
【问题描述】:

我正在尝试开发一个生物标记名称实体识别(多类)模型。我有 9 个类并将其转换为 one-hot 编码。在培训期间,我收到以下错误:

ValueError: 形状为 (2014, 120, 9) 的目标数组被传递为形状 (None, 9) 的输出,同时用作损失 categorical_crossentropy。这种损失期望目标具有与输出相同的形状。

我的代码sn-p:

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

【问题讨论】:

    标签: python python-3.x tensorflow keras named-entity-recognition


    【解决方案1】:

    不要使用一种热编码和categorical_crossentropy。而是保持 y 向量不变并使用sparse_categorical_crossentropy。看看这是否有效。

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

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 2020-10-08
      • 1970-01-01
      • 2023-04-03
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多