【问题标题】:Epochs problem when training model in tensorflow在张量流中训练模型时的时代问题
【发布时间】:2019-12-22 03:02:50
【问题描述】:

所以我是 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 个 epoch 就可以产生一个精确的值,但至少需要 5000 个 epoch 才能得到一个准确的值。发生这种情况的原因可能是什么?

【问题讨论】:

  • 因为这个epochs = 10000

标签: python tensorflow keras prediction


【解决方案1】:

您的代码在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]))

【讨论】:

  • 我知道需要 5000 个 epoch 才能获得较低的损失值(这在生成的图表中很明显)。当我在另一台计算机上运行它时,该程序只运行了 500 个 epoch 并产生了更精确的值。为什么我的计算机需要 5000 个 epoch 才能产生准确的值,而其他计算机只需 500 个 epoch 就可以产生更精确的值?
猜你喜欢
  • 2021-12-24
  • 2017-09-22
  • 2020-02-29
  • 2023-03-12
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多