[英]Epochs problem when training model in tensorflow
So I am a newbie to Tensorflow 2.0 and was trying to train a simple model which converts a value in Celsius to Fahrenheit.所以我是 Tensorflow 2.0 的新手,并试图训练一个简单的模型,将摄氏值转换为华氏度。 Here is the code:
这是代码:
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]))
The model was supposed to produce a precise value with just 500 epochs but it takes at least 5000 epochs to get an accurate value.该模型应该只用 500 个 epoch 就产生一个精确的值,但至少需要 5000 个 epoch 才能得到一个准确的值。 What might be the reason this happens?
发生这种情况的原因可能是什么?
Your code is having epochs=10000
as an argument in model.fit
method.Please use the below code:您的代码在
model.fit
中有model.fit
epochs=10000
作为参数。请使用以下代码:
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]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.