【问题标题】:I cant calculate "Loss" in keras [closed]我无法在 keras 中计算“损失”[关闭]
【发布时间】:2021-03-14 20:28:26
【问题描述】:

这是我的代码:

# Library Imports
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
plt.style.use("ggplot")

from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout

# Loading/Reading in the Data
df = pd.read_csv("BTC-USD.csv")


# Data Preprocessing
### Setting the datetime index as the date, only selecting the 'Close' column, then only the last 1000 closing prices.
df = df.set_index("Date")[['Close']].tail(3000)
df = df.set_index(pd.to_datetime(df.index))

# Normalizing/Scaling the Data
scaler = MinMaxScaler()
df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns, index=df.index)


def visualize_training_results(results):
    """
    Plots the loss and accuracy for the training and testing data
    """
    history = results.history
    plt.figure(figsize=(12,4))
    plt.plot(history['val_loss'])
    plt.plot(history['loss'])
    plt.legend(['val_loss', 'loss'])
    plt.title('Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.show()
    
    plt.figure(figsize=(12,4))
    plt.plot(history['val_accuracy'])
    plt.plot(history['accuracy'])
    plt.legend(['val_accuracy', 'accuracy'])
    plt.title('Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.show()
    
    
def split_sequence(seq, n_steps_in, n_steps_out):
    """
    Splits the univariate time sequence
    """
    X, y = [], []
    
    for i in range(len(seq)):
        end = i + n_steps_in
        out_end = end + n_steps_out
        
        if out_end > len(seq):
            break
        
        seq_x, seq_y = seq[i:end], seq[end:out_end]
        
        X.append(seq_x)
        y.append(seq_y)
    
    return np.array(X), np.array(y)


def layer_maker(n_layers, n_nodes, activation, drop=None, d_rate=.5):
    """
    Create a specified number of hidden layers for an RNN
    Optional: Adds regularization option, dropout layer to prevent potential overfitting if necessary
    """
    
    # Creating the specified number of hidden layers with the specified number of nodes
    for x in range(1,n_layers+1):
        model.add(LSTM(n_nodes, activation=activation, return_sequences=True))

        # Adds a Dropout layer after every Nth hidden layer (the 'drop' variable)
        try:
            if x % drop == 0:
                model.add(Dropout(d_rate))
        except:
            pass

# How many periods looking back to train
n_per_in  = 30

# How many periods ahead to predict
n_per_out = 10

# Features (in this case it's 1 because there is only one feature: price)
n_features = 1

# Splitting the data into appropriate sequences
X, y = split_sequence(list(df.Close), n_per_in, n_per_out)

# Reshaping the X variable from 2D to 3D
X = X.reshape((X.shape[0], X.shape[1], n_features))

# Instantiating the model
model = Sequential()

# Activation
activ = "softsign"

# Input layer
model.add(LSTM(30, activation=activ, return_sequences=True, input_shape=(n_per_in, n_features)))

# Hidden layers
layer_maker(n_layers=6, n_nodes=12, activation=activ)

# Final Hidden layer
model.add(LSTM(10, activation=activ))

# Output layer
model.add(Dense(n_per_out))

# Model summary
model.summary()

plt.figure(figsize=(12,5))


#Im compiling the code here before training
model.compile()
#visualize_training_results(res)
res = model.fit(X, y, epochs=800, batch_size=32, validation_split=0.1)
#visualize_training_results(res)

我需要在这里做什么才能开始训练我的模型?

我也试过这段代码编译:

model.compile(
    optimizer='adam',
    loss='mean_squared_error',
    metrics=[
        metrics.MeanSquaredError(),
        metrics.AUC(),
    ]
)

但是当我运行这个时,我得到未定义的指标错误。 即使我在编译函数中定义了它?

我也这样做了

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

开始训练,但我得到了这个输出

Epoch 6/800
10/10 [==============================] - 1s 53ms/step - loss: nan - val_loss: nan
Epoch 7/800
10/10 [==============================] - 0s 43ms/step - loss: nan - val_loss: nan

当我这样做时:

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

我得到这个输出

0/10 [==============================] - 0s 49ms/step - loss: nan - accuracy: 0.1128 - val_loss: nan - val_accuracy: 0.0606
Epoch 121/800
10/10 [==============================] - 0s 48ms/step - loss: nan - accuracy: 0.1086 - val_loss: nan - val_accuracy: 0.0606

它训练并且准确度有效,但我仍然得到 nan 的 loss 和 val_loss

不管我用什么编译函数参数,这个东西都不计算损失

现在我试过了

model.compile(
    optimizer='adam',
    loss='mean_squared_error',
    metrics=["accuracy"],
)

这就是我得到的

10/10 [==============================] - 1s 74ms/step - loss: nan - accuracy: 0.0953 - val_loss: nan - val_accuracy: 0.0606
Epoch 5/800
10/10 [==============================] - 1s 74ms/step - loss: nan - accuracy: 0.1097 - val_loss: nan - val_accuracy: 0.0606
Epoch 6/800
10/10 [==============================] - 1s 72ms/step - loss: nan - accuracy: 0.0929 - val_loss: nan - val_accuracy: 0.0606

【问题讨论】:

  • 尝试只在顶部导入 keras,例如 import keras
  • TF 和 Keras 用户文档中涵盖了大部分(如果不是全部)您正在做的事情。 “出了点问题”表明您尚未仔细阅读这些用户指南——您应该有一个请提供预期的MRE - Minimal, Reproducible Example,并清楚地说明您哪里出了问题。

标签: python tensorflow keras data-science


【解决方案1】:

在训练模型时可以获得 NaN 的原因有很多。

  • 数据
  • 损失函数返回大于/小于 +infinity/-infinity

尝试获取 20 行,确保您自己用非常笨拙的眼睛查看了每一行,然后运行代码以查看您是否仍然获得 NAN。如果你不这样做,那么可能有 1-2 个损坏的条目。否则,看看split_sequence 仅使用这 20 列时返回的内容。

然后是无穷大问题,这导致了 NAN。尝试使用bounded 的损失函数。

Here is a list of Keras loss functions

虽然均方根偏差返回一个“与输入数据相比以自然方式表示误差”的值i.e. RMSE 30'000 <=> error in estimating house price by 30'000$,但它仍然计算平方,这肯定会很快上升到无穷大,所以它是如果这是这里的问题,那不是解决方案。您可以使用 MAE 将其保持在较低水平。

更新:

它对我有用。我使用了以下内容,以及美国另一个加密男孩硬币的类似数据集:

# The good old SGD and MAE. 
model.compile(optimizer="sgd", loss="mae") # optional: metrics=['accuracy']

在使用 Adams/Huber 或其他优化器/损失函数时要小心。您应该了解它们的运作方式,至少了解我们使用它们的方式和原因。现在,使用 SGD 并不是最好的,但从那里你有一个健康的基础。

祝你工作顺利!不要过拟合,用小数据集制作小型模型,以便在运行 800 个 epoch 之前快速训练和原型化,结果发现一个参数不可行! 原型!未来你会感谢你的! :)

【讨论】:

  • 你用过这个编译函数model.compile(optimizer="sgd", loss="mae")吗?
  • 确切地说:"model.compile(optimizer="sgd", loss="mae", metrics=['accuracy'])" 但是是的,就是这样。
  • 日期开盘高点...关闭调整收盘量 0 2020-03-14 5573.077637 5625.226563 ... 5200.366211 5200.366211 3.615451e+10
  • 您可能不喜欢它,但唯一的选择是:pandas.pydata.org/pandas-docs/stable/user_guide/10min.html 10 分钟。您将能够调查您的数据。我得到了上一份工作,因为我知道如何使用熊猫。所以也许你也可以通过在Linkedin上找到一份好工作。 ;)
  • 我不知道您的数据来自雅虎财经。 Stackoverflow 不鼓励帮助者获取整个代码只是为了在他们的计算机上运行它们。我已经为您提供的帮助远远超出了本网站上允许的任何人所做的事情。 ;)
猜你喜欢
  • 2018-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 2020-12-19
  • 1970-01-01
  • 2018-07-21
相关资源
最近更新 更多