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. 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. 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:
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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.