【问题标题】:Evaluating Regression Neural Network model's accuracy评估回归神经网络模型的准确性
【发布时间】:2018-08-24 09:35:26
【问题描述】:

我是机器学习的新手,我为回归输出创建了一个神经网络。我有大约 95000 个训练示例和大约 24000 个测试示例。我想知道如何评估我的模型并获得训练和测试错误?如何知道这个回归模型的准确性?我的 Y 变量值范围在 100-200 之间,X 在数据集中有 9 个输入特征。

这是我的代码:

import pandas as pd
from keras.layers import Dense, Activation,Dropout
from keras.models import Sequential
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from matplotlib import pyplot

# Importing the dataset
# Importing the dataset
dataset = pd.read_csv('data2csv.csv')

X = dataset.iloc[:,1:10].values
y = dataset.iloc[:, :1].values

# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Initialising the ANN
model = Sequential()

# Adding the input layer and the first hidden layer

model.add(Dense(10, activation = 'relu', input_dim = 9))

# Adding the second hidden layer
model.add(Dense(units = 5, activation = 'sigmoid'))
model.add(Dropout(0.2))

# Adding the third hidden layer
model.add(Dense(units = 5, activation = 'relu'))
model.add(Dropout(0.2))

model.add(Dense(units = 5, activation = 'relu'))
model.add(Dense(units = 5, activation = 'relu'))

# Adding the output layer

model.add(Dense(units = 1))

#model.add(Dense(1))
# Compiling the ANN
model.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics=['mae','mse','mape','cosine'])

# Fitting the ANN to the Training set
history=model.fit(X_train, y_train,validation_data=(X_val, y_val) ,batch_size = 1000, epochs = 100)
test_loss = model.evaluate(X_test,y_test)

loss = history.history['loss']
acc = history.history['mean_absolute_error']
val_loss = history.history['val_loss']
val_acc = history.history['val_mean_absolute_error']
mape_loss=history.history['mean_absolute_percentage_error']
cosine_los=history.history['cosine_proximity']
pyplot.plot(history.history['mean_squared_error'])
pyplot.plot(history.history['mean_absolute_error'])
pyplot.plot(history.history['mean_absolute_percentage_error'])
pyplot.plot(history.history['cosine_proximity'])
pyplot.show()
epochs = range(1, len(loss)+1)
plt.plot(epochs, loss, 'ro', label='Training loss')
plt.legend()
plt.show()

y_pred = model.predict(X_test)

plt.plot(y_test, color = 'red', label = 'Real data')
plt.plot(y_pred, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()

[]

model.evaluate 后我的测试损失。注意这里有5个损失函数,如代码所示。

1) 84.69654303799824 2) 7.030169963975834 3) 84.69654303799824 4) 5.241855282313331 5) -0.9999999996023872

【问题讨论】:

    标签: python tensorflow machine-learning keras loss


    【解决方案1】:

    要评估您的模型,您可以使用evaluate 方法:

    test_loss = model.evaluate(X_test, y_test)
    

    它返回使用您在训练期间使用的相同损失函数计算的给定测试数据的损失(即mean_squared_error)。 此外,如果您想在每个 epoch 结束时获得训练损失,您可以使用 History object which is returned by fit 方法:

    history = model.fit(...)
    loss = history.history['loss']
    

    loss 是一个列表,其中包含每个 epoch 结束时的训练损失值。如果您在训练模型时使用了验证数据(即model.fit(..., validation_data=(X_val, y_val))或使用了任何其他指标,如mean_absolute_error(即model.compile(..., metrics=['mae'])),您还可以访问它们的值:

    acc = history.history['mae']
    val_loss = history.history['val_loss']
    val_acc = history.history['val_mae']
    

    奖励:绘制训练损失曲线:

    epochs = range(1, len(loss)+1)
    plt.plot(epochs, loss, 'ro', label='Training loss')
    plt.legend()
    plt.show()
    

    【讨论】:

    • 谢谢。看到这些结果,如何理解我的模型是好的?
    • @user123098 好吧,这完全取决于您正在处理的问题以及您对“好”损失的定义。当然,在使用mean_squared_error作为损失的情况下,最好接近于零。但是,例如,如果您尝试预测明天的温度,则均方损失 1 意味着您的预测平均偏离度数。您可能会认为这种精度“非常好”、“好”、“还不错”或“很糟糕”。我希望我想说的很清楚。
    • 是的,我最后一个时期的损失 mse 约为 45.7。我的数据集很大,它有一个因变量 Y,它取决于 X 的 9 个输入特征。你能给我一些建议吗?
    • @user123098 我什么都不能告诉你。 45.7 的 MSE 意味着 sqrt(45.7) ~= 6.8 的平均误差。 6.8的错误被认为是好的吗?好吧,我唯一能说的是,如果 Y 值很大(例如在 [1000, 10000] 范围内),那么平均误差非常好。否则,如果它们非常小(例如 [1-10] 或 [0-1]),那就不好了,您需要修改模型的架构或参数或收集更多的训练数据。您应该定义一个验证集并比较训练错误和验证错误。如果训练误差很小而验证误差很大,那么你就是 >>>>
    • 过拟合。为防止过度拟合,您可以添加正则化、收集更多训练数据、减小网络大小等。顺便说一句,我无法理解您在评估时遇到的错误。您能否更新问题中的代码以包含评估部分?请在此处提及您遇到的错误,
    【解决方案2】:

    在训练时显示验证损失:

    model.fit(X_train, y_train, batch_size = 1000, epochs = 100, validation_data = (y_train,y_test))
    

    我认为您不能通过绘图轻松获得准确性,因为您的输入是 9 维的,您可以绘制每个特征的预测 y,只需关闭连接点的线,即 plt.plot(x,y ,'k.') 注意 'k' 所以没有行,但我不确定这是否有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2019-11-17
      • 2020-03-07
      • 2017-08-31
      • 2020-03-27
      • 2018-10-19
      • 2013-06-30
      相关资源
      最近更新 更多