0

我已经构建了一个基于聊天机器人的 seq2seq。我使用的 coupus 是来自https://github.com/Conchylicultor/DeepQA/tree/master/data/cornell的电影对话 我用来训练我的模型的大约 20000 个语料库。在 300 个 epoch 之后,损失约为 0.02。但最后当我输入一个随机问题时,比如“你要去哪里?” 或“你叫什么名字”或其他什么,我得到了相同的答案“它”。如您所见,无论我输入什么,我总是得到一个单词“It”。我发现当我使用 np.argmax 计算预测的概率分布时,每次我得到相同的索引“4”,这意味着接下来的单词' 指数。

我还发现来自 encoder_model 预测的 state_h 和 state_c 有一些非正规数据。例如。来自状态 c 的最大概率 > 16。

embed_layer = Embedding(input_dim=vocab_size, output_dim=50, trainable=False)
embed_layer.build((None,))
embed_layer.set_weights([embedding_matrix])

LSTM_cell = LSTM(300, return_state=True)
LSTM_decoder = LSTM(300, return_sequences=True, return_state=True)

dense = TimeDistributed(Dense(vocab_size, activation='softmax'))

#encoder输入 与 decoder输入
input_context = Input(shape=(maxLen, ), dtype='int32', name='input_context')
input_target = Input(shape=(maxLen, ), dtype='int32', name='input_target')

input_context_embed = embed_layer(input_context)
input_target_embed = embed_layer(input_target)

_, context_h, context_c = LSTM_cell(input_context_embed)
decoder_lstm, _, _ = LSTM_decoder(input_target_embed, 
                                  initial_state=[context_h, context_c])

output = dense(decoder_lstm)

model = Model([input_context, input_target], output)

model.compile(optimizer='adam', loss='categorical_crossentropy', 
              metrics=['accuracy'])
model.summary()

model.fit([context_, final_target_], outs, epochs=1, batch_size=128, validation_split=0.2)

input_context = Input(shape=(maxLen,), dtype='int32', name='input_context')
input_target = Input(shape=(maxLen,), dtype='int32', name='input_target')

input_ctx_embed = embed_layer(input_context)
input_tar_embed = embed_layer(input_target)

_, context_h, context_c = LSTM_cell(input_ctx_embed)
decoder_lstm, _, _ = LSTM_decoder(input_tar_embed, 
                                  initial_state=[context_h, context_c])
output = dense(decoder_lstm)

context_model = Model(input_context, [context_h, context_c])

target_h = Input(shape=(300,))
target_c = Input(shape=(300,))

target, h, c = LSTM_decoder(input_tar_embed, initial_state=[target_h, target_c])
output = dense(target)

target_model = Model([input_target, target_h, target_c], [output, h, c])


maxlen = 12
with open('reverse_dictionary.pkl', 'rb') as f:
    index_to_word = pickle.load(f)

question = "what is your name?"
# question = "where are you going?"
print(question)
a = question.split()
for pos, i in enumerate(a):
    a[pos] = re.sub('[^a-zA-Z0-9 .,?!]', '', i)
    a[pos]= re.sub(' +', ' ', i)
    a[pos] = re.sub('([\w]+)([,;.?!#&\'\"-]+)([\w]+)?', r'\1 \2 \3', i)
    if len(i.split()) > maxlen:
            a[pos] = (' ').join(a[pos].split()[:maxlen])
            if '.' in a[pos]:
                ind = a[pos].index('.')
                a[pos] = a[pos][:ind+1]
            if '?' in a[pos]:
                ind = a[pos].index('?')
                a[pos] = a[pos][:ind+1]
            if '!' in a[pos]:
                ind = a[pos].index('!')
                a[pos] = a[pos][:ind+1]

question = ' '.join(a).split()
print(question)

question = np.array([word_to_index[w] for w in question])
question = sequence.pad_sequences([question], maxlen=maxLen)
#                                   padding='post', truncating='post')
print(question)

question_h, question_c = context_model.predict(question)

answer = np.zeros([1, maxLen])
answer[0, -1] = word_to_index['BOS']
'''
i keeps track of the length of the generated answer. 
This won't allow the model to genrate sequences with more than 20 words.
'''
i=1

answer_ = []
flag = 0

while flag != 1:
    prediction, prediction_h, prediction_c = target_model.predict([
        answer, question_h, question_c
    ])
#     print(prediction[0,-1,4])
    word_arg = np.argmax(prediction[0, -1, :]) #
#     print(word_arg)
    answer_.append(index_to_word[word_arg])

    if word_arg == word_to_index['EOS'] or i > 20:
        flag = 1
    answer = np.zeros([1, maxLen])
    answer[0, -1] = word_arg
    question_h = prediction_h
    question_c = prediction_c
    i += 1

print(' '.join(answer_))

我的输入:你叫什么名字?['什么','是','你的','名字','?'] [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 218 85 20 206 22]]

我得到了什么:它

4

1 回答 1

1

2天后,我知道我的word2idx与idx2word的反面不一样的原因,谢谢大家。

于 2019-04-04T12:26:30.443 回答