编码器-解码器LSTM模型给出了'nan'损失和预测

时间:2020-05-16 08:04:49

标签: python keras lstm chatbot recurrent-neural-network

我正在尝试创建用于训练聊天机器人的基本编码器-解码器模型。 X包含问题或人类对话,Y包含漫游器答案。我将序列填充到输入和输出句子的最大大小。 X.shape =(2363,242,1)和Y.shape =(2363,144,1)。但是在训练过程中,损失对于所有时期都具有值“ nan”,并且预测给出的阵列的所有值都为“ nan” 。我尝试使用“ rmsprop”优化器而不是“ adam”。我不能使用损失函数'categorical_crossentropy',因为输出不是一次编码,而是一个序列。我的代码到底有什么问题?

模型

model = Sequential()
model.add(LSTM(units=64, activation='relu', input_shape=(X.shape[1], 1)))
model.add(RepeatVector(Y.shape[1]))
model.add(LSTM(units=64, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(units=1)))
print(model.summary())

model.compile(optimizer='adam', loss='mean_squared_error')

hist = model.fit(X, Y, epochs=20, batch_size=64, verbose=2)
model.save('encoder_decoder_model_epochs20.h5')

数据准备

def remove_punctuation(s):
    s = s.translate(str.maketrans('','',string.punctuation))
    s = s.encode('ascii', 'ignore').decode('ascii')
    return s

def prepare_data(fname):
    word2idx = {'PAD': 0}
    curr_idx = 1
    sents = list()
    for line in open(fname):
        line = line.strip()
        if line:
            tokens = remove_punctuation(line.lower()).split()
            tmp = []
            for t in tokens:
                if t not in word2idx:
                    word2idx[t] = curr_idx
                    curr_idx += 1
                tmp.append(word2idx[t])
            sents.append(tmp)
    sents = np.array(pad_sequences(sents, padding='post'))
    return sents, word2idx

human = 'rdany-conversations/human_text.txt'
robot = 'rdany-conversations/robot_text.txt'

X, input_vocab = prepare_data(human)
Y, output_vocab = prepare_data(robot)

X = X.reshape((X.shape[0], X.shape[1], 1))
Y = Y.reshape((Y.shape[0], Y.shape[1], 1))

1 个答案:

答案 0 :(得分:1)

首先请检查您的输入中没有任何NaN。如果不是这种情况,可能是爆炸梯度。标准化您的输入(MinMax或Z缩放),尝试使用较小的学习率,剪切渐变,尝试不同的权重初始化。