Keras损失函数不会因均方误差而减小

时间:2018-12-24 12:35:14

标签: keras neural-network regression loss-function mean-square-error

我用Keras实现了一个神经网络来预测物品的等级。我认为每个等级都是一个类别,所以这是我的代码(outputY是绝对的):

inputLayerU = Input(shape=(features,))
inputLayerM = Input(shape=(features,))
dense1 = Dense(features, activation='relu')
denseU = dense1(inputLayerU)         
denseM = dense1(inputLayerM)    
concatLayer = concatenate([denseU, denseM], axis = 1)
denseLayer = Dense(features*2, activation='relu')(concatLayer)
outputLayer = Dense(5, activation='softmax')(denseLayer)

model = Model(inputs=[inputLayerU, inputLayerM], outputs=outputLayer)
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.01), metrics=['accuracy'])

model.fit([inputU, inputM],outputY , epochs=10, steps_per_epoch=10)

当我训练该网络时,会得到以下结果:

10/10 [==============================] - 2s 187ms/step - loss: 1.4778 - acc: 0.3209
Epoch 2/10
10/10 [==============================] - 0s 49ms/step - loss: 1.4058 - acc: 0.3625
Epoch 3/10
10/10 [==============================] - 1s 54ms/step - loss: 1.3825 - acc: 0.3824
Epoch 4/10
10/10 [==============================] - 0s 47ms/step - loss: 1.3614 - acc: 0.3923
Epoch 5/10
10/10 [==============================] - 0s 48ms/step - loss: 1.3372 - acc: 0.4060
Epoch 6/10
10/10 [==============================] - 0s 45ms/step - loss: 1.3138 - acc: 0.4202
Epoch 7/10
10/10 [==============================] - 0s 46ms/step - loss: 1.2976 - acc: 0.4266
Epoch 8/10
10/10 [==============================] - 0s 48ms/step - loss: 1.2842 - acc: 0.4325
Epoch 9/10
10/10 [==============================] - 1s 62ms/step - loss: 1.2729 - acc: 0.4402
Epoch 10/10
10/10 [==============================] - 1s 54ms/step - loss: 1.2631 - acc: 0.4464

然后,我将问题视为回归并尝试预测用户评分的值(我需要同时计算两种误差)。这是我的代码:

inputLayerU = Input(shape=(features,))
inputLayerM = Input(shape=(features,))
dense1 = Dense(features, activation='relu')
denseU = dense1(inputLayerU)         
denseM = dense1(inputLayerM)    
concatLayer = concatenate([denseU, denseM], axis = 1)
denseLayer = Dense(features*2, activation='relu')(concatLayer)
outputLayer = Dense(1, activation='softmax')(denseLayer)

model = Model(inputs=[inputLayerU, inputLayerM], outputs=outputLayer)
model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.01), metrics=['accuracy'])

model.fit([inputU, inputM],outputY , epochs=10, steps_per_epoch=10)

我得到以下结果:

Epoch 1/10
10/10 [==============================] - 9s 894ms/step - loss: 7.9451 - acc: 0.0563
Epoch 2/10
10/10 [==============================] - 7s 711ms/step - loss: 7.9447 - acc: 0.0563
Epoch 3/10
10/10 [==============================] - 7s 709ms/step - loss: 7.9446 - acc: 0.0563
Epoch 4/10
10/10 [==============================] - 7s 710ms/step - loss: 7.9446 - acc: 0.0563
Epoch 5/10
10/10 [==============================] - 7s 702ms/step - loss: 7.9446 - acc: 0.0563
Epoch 6/10
10/10 [==============================] - 7s 706ms/step - loss: 7.9446 - acc: 0.0563
Epoch 7/10
10/10 [==============================] - 7s 701ms/step - loss: 7.9446 - acc: 0.0563
Epoch 8/10
10/10 [==============================] - 7s 702ms/step - loss: 7.9446 - acc: 0.0563
Epoch 9/10
10/10 [==============================] - 7s 717ms/step - loss: 7.9446 - acc: 0.0563
Epoch 10/10
10/10 [==============================] - 7s 700ms/step - loss: 7.9446 - acc: 0.0563

如您所见,它减少了一些,有时甚至根本没有变化。

那我的回归到底出了什么问题?

1 个答案:

答案 0 :(得分:1)

首先,我不确定是否应将softmax函数应用于回归问题,其次,请尝试使用具有默认参数的Adam Optimizer。