如何降低平均损失?

我是一名水利工程专业的学生,目前在实习期间研究神经网络,这对我来说是新的领域。我创建了一个神经网络,但它的损失值很高,我不知道问题出在哪里…你可以查看我的代码:

def create_model():    model = Sequential()    # Adding the input layer    model.add(Dense(26,activation='relu',input_shape=(n_cols,)))    # Adding the hidden layer    model.add(Dense(60,activation='relu'))    model.add(Dense(60,activation='relu'))    model.add(Dense(60,activation='relu'))    # Adding the output layer    model.add(Dense(2))    # Compiling the RNN    model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])    return modelkf = KFold(n_splits = 5, shuffle = True)model = create_model()scores = []for i in range(5):    result = next(kf.split(data_input), None)    input_train = data_input[result[0]]    input_test = data_input[result[1]]    output_train = data_output[result[0]]    output_test = data_output[result[1]]    # Fitting the RNN to the Training set    model.fit(input_train, output_train, epochs=5000, batch_size=200 ,verbose=2)    predictions = model.predict(input_test)     scores.append(model.evaluate(input_test, output_test))print('Scores from each Iteration: ', scores)print('Average K-Fold Score :' , np.mean(scores))

当我执行代码时,结果如下:

Scores from each Iteration:  [[93.90406122928908, 0.8907562990148529], [89.5892979597845, 0.8907563030218878], [81.26530176050522, 0.9327731132507324], [56.46526102659081, 0.9495798339362905], [54.314151876112994, 0.9579831877676379]]Average K-Fold Score : 38.0159922589274

请问有人能帮帮我吗?我该怎么做才能降低损失值?


回答:

你的问题和代码都有几个问题…

首先,通常我们不能说均方误差(MSE)损失值X是低还是高。与分类问题中的准确率定义在[0, 1]范围内不同,损失值没有类似的界限,所以没有一般的方法来判断某个特定的值是低还是高,如你所暗示的(这总是取决于具体问题)。

澄清了这一点后,让我们来看你的代码。

首先,从你的loss='mean_squared_error'来看,你似乎是在做回归任务,在这种情况下,准确率是没有意义的;请参阅当损失为均方误差(MSE)时,Keras中准确率的定义函数是什么?。你没有分享你具体要解决什么问题,但如果确实是回归问题(即预测某个数值),你应该在模型编译时删除metrics=['accuracy'],并可能将最后一层改为单个单元,即model.add(Dense(1))

其次,就你当前的代码而言,你实际上并没有在每个交叉验证折叠中从头开始拟合独立的模型(这是CV的本质);在Keras中,model.fit累积的,即每次调用时不会“重置”模型,而是继续从上次调用的地方进行拟合。这就是为什么如果你查看你的scores,显然在后面的折叠中模型表现得更好(这已经给出了改进的提示:增加更多的epoch)。为了正确地进行CV,你应该将create_model()移到for循环内部

第三,你在这里使用np.mean()再次是没有意义的,因为你同时平均了损失准确率(即苹果和橙子);从54到94之间的5个损失值,你最终得到的“平均值”为38,这应该已经提醒你你在尝试一些错误的事情了。事实上,如果你像上面提到的,忽略准确率指标,你就不会在这里遇到这个问题了。

总的来说,原则上你的代码应该如下所示(但再次声明,我对你试图解决的具体问题一无所知,所以一些细节可能会有所不同):

def create_model():    model = Sequential()    # Adding the input layer    model.add(Dense(26,activation='relu',input_shape=(n_cols,)))    # Adding the hidden layer    model.add(Dense(60,activation='relu'))    model.add(Dense(60,activation='relu'))    model.add(Dense(60,activation='relu'))    # Adding the output layer    model.add(Dense(1))  # change to 1 unit    # Compiling the RNN    model.compile(optimizer='adam', loss='mean_squared_error') # dismiss accuracy    return modelkf = KFold(n_splits = 5, shuffle = True)scores = []for i in range(5):    result = next(kf.split(data_input), None)    input_train = data_input[result[0]]    input_test = data_input[result[1]]    output_train = data_output[result[0]]    output_test = data_output[result[1]]    # Fitting the RNN to the Training set    model = create_model()  # move create_model here    model.fit(input_train, output_train, epochs=10000, batch_size=200 ,verbose=2)  # increase the epochs    predictions = model.predict(input_test)     scores.append(model.evaluate(input_test, output_test))print('Loss from each Iteration: ', scores)print('Average K-Fold Loss :' , np.mean(scores))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注