当我使用更多数据来训练 tensorflow NN 模型时,发生了一些意想不到的事情
When I use more data to train a tensorflow NN model, some unexpected thing happened
我尝试使用 tensorflow 的 exercise,当我在训练期间给 xs
和 ys
更高的值时,损失开始增加。为什么会这样?难道我做错了什么?
当 max
为 19
但 20
.
时看起来没问题
import tensorflow as tf
import numpy as np
from tensorflow import keras
def main():
print(tf.__version__)
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss="mean_squared_error")
min = 0
max = 20 # the loss will increse if gives more data
xs = np.array([float(x) for x in range(min, max)], dtype=np.float32)
ys = np.array([float(3 * x + 1) for x in range(min, max)], dtype=np.float32)
print(xs, sep=',')
print(ys, sep=',')
model.fit(xs, ys, epochs=10)
print(model.predict([100]))
if __name__ == "__main__":
main()
结果:
Epoch 1/10
1/1 [==============================] - 0s 85ms/step - loss: 1813.5785
Epoch 2/10
1/1 [==============================] - 0s 444us/step - loss: 3997.2852
Epoch 3/10
1/1 [==============================] - 0s 392us/step - loss: 8810.5635
Epoch 4/10
1/1 [==============================] - 0s 349us/step - loss: 19419.8730
Epoch 5/10
1/1 [==============================] - 0s 315us/step - loss: 42804.6797
Epoch 6/10
1/1 [==============================] - 0s 309us/step - loss: 94348.9844
Epoch 7/10
1/1 [==============================] - 0s 365us/step - loss: 207961.6875
Epoch 8/10
1/1 [==============================] - 0s 414us/step - loss: 458384.1875
Epoch 9/10
1/1 [==============================] - 0s 494us/step - loss: 1010359.8125
Epoch 10/10
1/1 [==============================] - 0s 347us/step - loss: 2227010.5000
问题不在于 max
值是 19
或 20
。
在密集层中使用 2 个神经元而不是 1 个神经元,如下所示:
model = tf.keras.Sequential([keras.layers.Dense(units=2, input_shape=[1])])
也许 1 个神经元无法处理该回归函数。
我尝试更改一些配置,最大的更改是将学习率从默认的 0.01 更改为 0.001。
要做到这一点改变
model.compile(optimizer="sgd",.....)
至
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),.....
它使用相同的优化器,但允许您设置学习率。
太大的学习率会使你超过局部最小值,并可能使你的损失激增。学习率是开始调整的好地方当 你的损失激增时。
我尝试使用 tensorflow 的 exercise,当我在训练期间给 xs
和 ys
更高的值时,损失开始增加。为什么会这样?难道我做错了什么?
当 max
为 19
但 20
.
import tensorflow as tf
import numpy as np
from tensorflow import keras
def main():
print(tf.__version__)
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss="mean_squared_error")
min = 0
max = 20 # the loss will increse if gives more data
xs = np.array([float(x) for x in range(min, max)], dtype=np.float32)
ys = np.array([float(3 * x + 1) for x in range(min, max)], dtype=np.float32)
print(xs, sep=',')
print(ys, sep=',')
model.fit(xs, ys, epochs=10)
print(model.predict([100]))
if __name__ == "__main__":
main()
结果:
Epoch 1/10
1/1 [==============================] - 0s 85ms/step - loss: 1813.5785
Epoch 2/10
1/1 [==============================] - 0s 444us/step - loss: 3997.2852
Epoch 3/10
1/1 [==============================] - 0s 392us/step - loss: 8810.5635
Epoch 4/10
1/1 [==============================] - 0s 349us/step - loss: 19419.8730
Epoch 5/10
1/1 [==============================] - 0s 315us/step - loss: 42804.6797
Epoch 6/10
1/1 [==============================] - 0s 309us/step - loss: 94348.9844
Epoch 7/10
1/1 [==============================] - 0s 365us/step - loss: 207961.6875
Epoch 8/10
1/1 [==============================] - 0s 414us/step - loss: 458384.1875
Epoch 9/10
1/1 [==============================] - 0s 494us/step - loss: 1010359.8125
Epoch 10/10
1/1 [==============================] - 0s 347us/step - loss: 2227010.5000
问题不在于 max
值是 19
或 20
。
在密集层中使用 2 个神经元而不是 1 个神经元,如下所示:
model = tf.keras.Sequential([keras.layers.Dense(units=2, input_shape=[1])])
也许 1 个神经元无法处理该回归函数。
我尝试更改一些配置,最大的更改是将学习率从默认的 0.01 更改为 0.001。 要做到这一点改变
model.compile(optimizer="sgd",.....)
至
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),.....
它使用相同的优化器,但允许您设置学习率。
太大的学习率会使你超过局部最小值,并可能使你的损失激增。学习率是开始调整的好地方当 你的损失激增时。