machine-learning - 如何解决Keras LSTM网络中的loss = Nan问题?

标签 machine-learning tensorflow deep-learning keras lstm

我正在使用 Keras 训练 LSTM 网络,并以 tensorflow 作为后端。该网络用于能源负荷预测,数据集大小为(32292,24)。但随着程序运行,我从第一个纪元开始就得到了损失的 Nan 值。我该如何解决这个问题?

PS:就数据预处理而言,我将每个值除以 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

最佳答案

我将密集层的激活函数更改为“softmax”(在我的例子中,它是关于多类分类),并且它有效。

关于machine-learning - 如何解决Keras LSTM网络中的loss = Nan问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811464/

相关文章:

python - TypeError : Expected float32 passed to parameter 'y' of op 'Equal' , 得到了类型为 'auto' 的 'str'

deep-learning - 在对自定义数据集进行 yolo 训练期间重写框是什么意思?

python - 在 Python 中从头开始计算雅可比矩阵

python - 构建特征时内存不足(将图像转换为派生特征 [numpy 数组])?

python - Face Plus Plus Python API更改参数

TensorFlow Keras SavedModel 在保存和加载两次后抛出 TypeError

tensorflow - 如何在 Google Colab 中降级 Cuda 和 cuDNN 版本?

ios - 机器学习算法的训练强度如何?

python - AttributeError: 'Tensor' 对象没有属性 'numpy'

python - tensorflow 值错误: Failed to find data adapter that can handle input