Keras 模型准确率高但预测不佳
Keras model with high accuracy but poor predictions
我正在尝试训练一个神经网络来为具有预定义段长度的机械臂进行反向运动学计算。我不包括神经网络输入中的段长度,而是通过训练数据。训练数据是一个 pandas 数据框,其中包含手臂的空间映射,标签是手臂三段的旋转角度,特征是 x 和 y 坐标端点的解最后一段将结束。
我正在使用 Keras 和 Theano 作为后端。
model = Sequential([
Dense(3, input_shape=(2,), activation="relu"),
Dense(3, activation="relu"),
Dense(3)
])
model.summary()
model.compile(Adam(lr=0.001), loss='mean_squared_error', metrics=['accuracy'])
model.fit(samples, labels, validation_split=0.2, batch_size=1000, epochs=10,shuffle=True, verbose=1)
score = model.evaluate(samples, labels, batch_size=32, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
weights = model.get_weights()
predictions = model.predict(samples, verbose=1)
print predictions
model.save("IK_NN_7-4-3_keras.h5")
OUTPUT===============================================================
Train on 6272736 samples, validate on 1568184 samples
Epoch 1/10
- 5s - loss: 10198.7558 - acc: 0.9409 - val_loss: 12149.1703 - val_acc: 0.9858
Epoch 2/10
- 5s - loss: 4272.9105 - acc: 0.9932 - val_loss: 12117.0527 - val_acc: 0.9858
Epoch 3/10
- 5s - loss: 4272.7862 - acc: 0.9932 - val_loss: 12113.3804 - val_acc: 0.9858
Epoch 4/10
- 5s - loss: 4272.7567 - acc: 0.9932 - val_loss: 12050.8211 - val_acc: 0.9858
Epoch 5/10
- 5s - loss: 4272.7271 - acc: 0.9932 - val_loss: 12036.5538 - val_acc: 0.9858
Epoch 6/10
- 5s - loss: 4272.7350 - acc: 0.9932 - val_loss: 12103.8665 - val_acc: 0.9858
Epoch 7/10
- 5s - loss: 4272.7553 - acc: 0.9932 - val_loss: 12175.0442 - val_acc: 0.9858
Epoch 8/10
- 5s - loss: 4272.7282 - acc: 0.9932 - val_loss: 12161.4815 - val_acc: 0.9858
Epoch 9/10
- 5s - loss: 4272.7213 - acc: 0.9932 - val_loss: 12101.4021 - val_acc: 0.9858
Epoch 10/10
- 5s - loss: 4272.7909 - acc: 0.9932 - val_loss: 12152.4966 - val_acc: 0.9858
Test score: 5848.549130022683
Test accuracy: 0.9917127071823204
[[ 59.452095 159.26912 258.94424 ]
[ 59.382706 159.41936 259.25183 ]
[ 59.72419 159.69777 259.48584 ]
...
[ 59.58721 159.33467 258.9603 ]
[ 59.51745 159.69331 259.62595 ]
[ 59.984367 160.5533 260.7689 ]]
测试准确率和验证准确率看起来都不错,但并不能完全反映实际情况。预测应该看起来像这样
[[ 0 0 0]
[ 0 0 1]
[ 0 0 2]
...
[358 358 359]
[358 359 359]
[359 359 359]]
因为我反馈了相同的特征期望得到相同的标签。相反,我出于某种原因得到了这个数字:
[[ 59.452095 159.26912 258.94424 ]
[ 59.382706 159.41936 259.25183 ]
[ 59.72419 159.69777 259.48584 ]
...
[ 59.58721 159.33467 258.9603 ]
[ 59.51745 159.69331 259.62595 ]
[ 59.984367 160.5533 260.7689 ]]
感谢您的宝贵时间。
首先,您的指标是准确性,并且您预测的是连续值。你得到预测,但它们没有任何意义。您的问题是回归,您的指标用于分类。您可以只使用 "MSE" "R²" 或其他回归指标
from keras import metrics
model.compile(loss='mse', optimizer='adam', metrics=[metrics.mean_squared_error, metrics.mean_absolute_error])
此外,您应该考虑增加神经元的数量,如果您的输入数据真的只有二维,请考虑一些浅层模型,而不是人工神经网络。 (例如具有高斯内核的 SVM)
我正在尝试训练一个神经网络来为具有预定义段长度的机械臂进行反向运动学计算。我不包括神经网络输入中的段长度,而是通过训练数据。训练数据是一个 pandas 数据框,其中包含手臂的空间映射,标签是手臂三段的旋转角度,特征是 x 和 y 坐标端点的解最后一段将结束。
我正在使用 Keras 和 Theano 作为后端。
model = Sequential([
Dense(3, input_shape=(2,), activation="relu"),
Dense(3, activation="relu"),
Dense(3)
])
model.summary()
model.compile(Adam(lr=0.001), loss='mean_squared_error', metrics=['accuracy'])
model.fit(samples, labels, validation_split=0.2, batch_size=1000, epochs=10,shuffle=True, verbose=1)
score = model.evaluate(samples, labels, batch_size=32, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
weights = model.get_weights()
predictions = model.predict(samples, verbose=1)
print predictions
model.save("IK_NN_7-4-3_keras.h5")
OUTPUT===============================================================
Train on 6272736 samples, validate on 1568184 samples
Epoch 1/10
- 5s - loss: 10198.7558 - acc: 0.9409 - val_loss: 12149.1703 - val_acc: 0.9858
Epoch 2/10
- 5s - loss: 4272.9105 - acc: 0.9932 - val_loss: 12117.0527 - val_acc: 0.9858
Epoch 3/10
- 5s - loss: 4272.7862 - acc: 0.9932 - val_loss: 12113.3804 - val_acc: 0.9858
Epoch 4/10
- 5s - loss: 4272.7567 - acc: 0.9932 - val_loss: 12050.8211 - val_acc: 0.9858
Epoch 5/10
- 5s - loss: 4272.7271 - acc: 0.9932 - val_loss: 12036.5538 - val_acc: 0.9858
Epoch 6/10
- 5s - loss: 4272.7350 - acc: 0.9932 - val_loss: 12103.8665 - val_acc: 0.9858
Epoch 7/10
- 5s - loss: 4272.7553 - acc: 0.9932 - val_loss: 12175.0442 - val_acc: 0.9858
Epoch 8/10
- 5s - loss: 4272.7282 - acc: 0.9932 - val_loss: 12161.4815 - val_acc: 0.9858
Epoch 9/10
- 5s - loss: 4272.7213 - acc: 0.9932 - val_loss: 12101.4021 - val_acc: 0.9858
Epoch 10/10
- 5s - loss: 4272.7909 - acc: 0.9932 - val_loss: 12152.4966 - val_acc: 0.9858
Test score: 5848.549130022683
Test accuracy: 0.9917127071823204
[[ 59.452095 159.26912 258.94424 ]
[ 59.382706 159.41936 259.25183 ]
[ 59.72419 159.69777 259.48584 ]
...
[ 59.58721 159.33467 258.9603 ]
[ 59.51745 159.69331 259.62595 ]
[ 59.984367 160.5533 260.7689 ]]
测试准确率和验证准确率看起来都不错,但并不能完全反映实际情况。预测应该看起来像这样
[[ 0 0 0]
[ 0 0 1]
[ 0 0 2]
...
[358 358 359]
[358 359 359]
[359 359 359]]
因为我反馈了相同的特征期望得到相同的标签。相反,我出于某种原因得到了这个数字:
[[ 59.452095 159.26912 258.94424 ]
[ 59.382706 159.41936 259.25183 ]
[ 59.72419 159.69777 259.48584 ]
...
[ 59.58721 159.33467 258.9603 ]
[ 59.51745 159.69331 259.62595 ]
[ 59.984367 160.5533 260.7689 ]]
感谢您的宝贵时间。
首先,您的指标是准确性,并且您预测的是连续值。你得到预测,但它们没有任何意义。您的问题是回归,您的指标用于分类。您可以只使用 "MSE" "R²" 或其他回归指标
from keras import metrics
model.compile(loss='mse', optimizer='adam', metrics=[metrics.mean_squared_error, metrics.mean_absolute_error])
此外,您应该考虑增加神经元的数量,如果您的输入数据真的只有二维,请考虑一些浅层模型,而不是人工神经网络。 (例如具有高斯内核的 SVM)