加了一句 batch_size=1, 后,运行结果相差很大?

来源:2-4 实战分类模型之模型构建

慕神4535282

2021-04-10

老师,下午好!
本节的11:03秒处,可以看到在验证集上准确率是 0.8342,我运行你git上的代码,也是类似的准确率,如下:

history = model.fit(x_train, y_train, epochs = 10, 
                    validation_data = (x_valid, y_valid))
Epoch 1/10
1719/1719 [==============================] - 3s 2ms/step - loss: 8.7104 - accuracy: 0.6663 - val_loss: 0.5905 - val_accuracy: 0.7922
Epoch 2/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.5476 - accuracy: 0.8003 - val_loss: 0.5241 - val_accuracy: 0.8180
Epoch 3/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4774 - accuracy: 0.8255 - val_loss: 0.4888 - val_accuracy: 0.8302
Epoch 4/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4387 - accuracy: 0.8398 - val_loss: 0.4630 - val_accuracy: 0.8414
Epoch 5/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4131 - accuracy: 0.8456 - val_loss: 0.4496 - val_accuracy: 0.8444
Epoch 6/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3961 - accuracy: 0.8544 - val_loss: 0.4409 - val_accuracy: 0.8520
Epoch 7/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3865 - accuracy: 0.8549 - val_loss: 0.4524 - val_accuracy: 0.8412
Epoch 8/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3702 - accuracy: 0.8622 - val_loss: 0.4279 - val_accuracy: 0.8496
Epoch 9/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3677 - accuracy: 0.8648 - val_loss: 0.4320 - val_accuracy: 0.8560
Epoch 10/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.3516 - accuracy: 0.8694 - val_loss: 0.4243 - val_accuracy: 0.8564

但是,因为看到视频11:03 上的batch 大小是 55000/55000, 而非上面的 1719/1719, 我在了解了相关概念后,把上述代码

history = model.fit(x_train, y_train, epochs = 10, 
                    validation_data = (x_valid, y_valid))

改为了

history = model.fit(x_train, y_train, epochs = 10, batch_size=1,
                    validation_data = (x_valid, y_valid))

其余都不变,但是,再次运行的结果却是

Epoch 1/10
55000/55000 [==============================] - 61s 1ms/step - loss: nan - accuracy: 0.1016 - val_loss: nan - val_accuracy: 0.0914
Epoch 2/10
55000/55000 [==============================] - 61s 1ms/step - loss: nan - accuracy: 0.0994 - val_loss: nan - val_accuracy: 0.0914
Epoch 3/10
55000/55000 [==============================] - 59s 1ms/step - loss: nan - accuracy: 0.1006 - val_loss: nan - val_accuracy: 0.0914
Epoch 4/10
55000/55000 [==============================] - 60s 1ms/step - loss: nan - accuracy: 0.1010 - val_loss: nan - val_accuracy: 0.0914
Epoch 5/10
55000/55000 [==============================] - 62s 1ms/step - loss: nan - accuracy: 0.1005 - val_loss: nan - val_accuracy: 0.0914
Epoch 6/10
55000/55000 [==============================] - 59s 1ms/step - loss: nan - accuracy: 0.1008 - val_loss: nan - val_accuracy: 0.0914
Epoch 7/10
55000/55000 [==============================] - 61s 1ms/step - loss: nan - accuracy: 0.1025 - val_loss: nan - val_accuracy: 0.0914
Epoch 8/10
55000/55000 [==============================] - 60s 1ms/step - loss: nan - accuracy: 0.0984 - val_loss: nan - val_accuracy: 0.0914
Epoch 9/10
55000/55000 [==============================] - 59s 1ms/step - loss: nan - accuracy: 0.1010 - val_loss: nan - val_accuracy: 0.0914
Epoch 10/10
55000/55000 [==============================] - 58s 1ms/step - loss: nan - accuracy: 0.1015 - val_loss: nan - val_accuracy: 0.0914

可以看到,改了之后的准确率连 0.1 都达不到,
请问老师,为什么会出现这种情况?
我需要再次修改哪里才可以回复到以前的准确率呢?
谢谢老师解答!!!

补充一点:因为我刚开始学习,所以我使用的是tf版本是:2.4.1 ,其余的版本号与老师你所使用的类似。

写回答

1回答

正十七

2021-04-14

batch_size=1影响的,batch_size相当于每次训练使用的样本数,如果=1的话,相当于每次只用一个样本,这样梯度就更加随机不具有统计意义,因而训练结果会变差。

让batch_size=32,可以解决这个问题。

0
0

Google老师亲授 TensorFlow2.0 入门到进阶

Tensorflow2.0实战—以实战促理论的方式学习深度学习

1849 学习 · 896 问题

查看课程