如何降低平均损失?

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

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

为什么我们在K-means聚类方法中使用kmeans.fit函数?

我在一个视频中使用K-means聚类技术,但我不明白为…

如何获取Keras中ImageDataGenerator的.flow_from_directory函数扫描的类名?

我想制作一个用户友好的GUI图像分类器,用户只需指向数…

如何查看每个词的tf-idf得分

我试图了解文档中每个词的tf-idf得分。然而,它只返…

如何修复 ‘ValueError: Found input variables with inconsistent numbers of samples: [32979, 21602]’?

我在制作一个用于情感分析的逻辑回归模型时遇到了这个问题…

如何向神经网络输入两个不同大小的输入?

我想向神经网络输入两个数据集。第一个数据集(元素)具有…

逻辑回归与机器学习有何关联

我们正在开会讨论聘请一位我们信任的顾问来做机器学习。一…

发表回复

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