如何解决Keras LSTM网络中的损失值为Nan的问题?

6
我正在使用TensorFlow作为后端,使用Keras来训练一个LSTM网络。该网络用于能量负荷预测,数据集的大小为(32292,24)。但是程序运行时,从第一个时期开始就出现了损失值为Nan的情况。我该如何解决这个问题?
附注:就数据预处理而言,我将每个值除以100000,因为最初每个值都是4或5位数。因此,我的值应该在(0,1)范围内。
def build_model():
    model = Sequential()
    layers = [1, 50, 100, 1]
    model.add(LSTM(input_dim=layers[0],output_dim=layers[1],return_sequenc
    es = True))     
    model.add(Dropout(0.2))
    model.add(LSTM(layers[2],return_sequences = False))
    model.add(Dropout(0.2))
    model.add(Dense(output_dim=layers[3]))
    model.add(Activation("linear"))

    start = time.time()
    model.compile(loss="mse", optimizer="rmsprop")
    print "Compilation Time : ", time.time() - start
return model
def run_network():
    global_start_time = time.time()
    epochs = 5000
    model = build_model()
    try:
        model.fit(x_train, y_train,batch_size = 400, nb_epoch=epochs,validation_split=0.05) 
        predicted = model.predict(x_test)
        predicted = np.reshape(predicted, (predicted.size,))
        except KeyboardInterrupt:
        print 'Training duration (s) : ', time.time() - global_start_time
    try:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(predicted[:100])
        plt.show()
    except Exception as e:
          print str(e)
          print 'Training duration (s) : ' , time.time() -   global_start_time

return model, y_test, predicted

1
你解决这个问题了吗? - Char
1
@Char 是的,我已经解决了这个问题。我没有正确地对数据集进行归一化处理,这就是为什么我会出现这个错误的原因。 - Sukrit
2个回答

2

我将密集层的激活函数改为'softmax'(在我的情况下,这是关于多类分类的),它起作用了。


0

对我来说,我将激活函数改为线性函数,然后它就起作用了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接