【发布时间】:2020-11-08 08:02:26
【问题描述】:
我正在尝试使用 Keras 中的 seq2seq 模型构建一个聊天机器人。我使用了 Keras 博客中指定的标准 seq2seq 模型。我使用 Word2vec 进行词嵌入。我的问题是我在训练时得到了负值损失。 为什么会发生这种情况,我该如何解决?谢谢。
from keras.models import Model
from keras.layers import Input, LSTM, Dense
# Define an input sequence and process it.
encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
# We discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = Input(shape=(None, num_decoder_tokens))
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the
# return states in the training model, but we will use them in inference.
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model that will turn
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
【问题讨论】:
-
找到原因了吗?
-
@Kamal - 不幸的是,还没有。
-
我找到了负值的原因。主要是因为 word2vec 的向量表示包含负值,所以损失函数不能正确计算损失我所做的是将 decoder_target_data 更改为 word2vec 模型词汇表中所有单词的一个热门编码器
标签: keras loss-function seq2seq