因此,我是Tensorflow 2.0的新手,并且正在尝试训练一个简单的模型,该模型将摄氏温度值转换为华氏温度。这是代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
c = np.array([-40, -10, 0, 8, 15, 22, 38], dtype = float)
f = np.array([-40, 14, 32, 46, 59, 72, 100], dtype = float)
lyr = tf.keras.layers.Dense(units = 1, input_shape = [1])
mod = tf.keras.Sequential([lyr])
mod.compile(loss = "mean_squared_error", optimzer = tf.keras.optimizers.Adam(0.1))
hist = mod.fit(c, f, epochs = 5000, verbose = False)
plt.xlabel("Epoch Number")
plt.ylabel("Loss Magnitude")
plt.plot(hist.history["loss"])
plt.show()
print(mod.predict([100.0]))
该模型原本只能产生500个纪元的精确值,但至少需要5000个纪元才能获得准确的值。发生这种情况的原因可能是什么?
答案 0 :(得分:0)
您的代码将epochs=10000
作为model.fit
方法的参数。请使用以下代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
c = np.array([-40, -10, 0, 8, 15, 22, 38], dtype = float)
f = np.array([-40, 14, 32, 46, 59, 72, 100], dtype = float)
lyr = tf.keras.layers.Dense(units = 1, input_shape = [1])
mod = tf.keras.Sequential([lyr])
mod.compile(loss = "mean_squared_error", optimzer = tf.keras.optimizers.Adam(0.1))
hist = mod.fit(c, f, epochs = 5000, verbose = False)
plt.xlabel("Epoch Number")
plt.ylabel("Loss Magnitude")
plt.plot(hist.history["loss"])
plt.show()
print(mod.predict([100.0]))