简体   繁体   English

在 tensorflow 中训练模型时的 Epochs 问题

[英]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.

相关问题 在使用多个时期进行训练时如何检查我的模型是否过拟合 - How to check if my model is overfitting or not when training with many epochs 从 tensorflow 和 keras 中的 mnist 训练 model 的问题 - problem with training model from mnist in tensorflow and keras tensorflow.keras model 准确度、损失和验证指标在训练期间保持 ZA81259CEF8E959C2297DF1D456EZ5 - tensorflow.keras model accuracy, loss, and validation metrics remain static during 30 epochs of training 使用 tensorflow keras 训练模型时,如何每 10 个 epoch 打印一个日志行? - How to print one log line per every 10 epochs when training models with tensorflow keras? 使用 lstm 训练的 model 需要多少个 epoch - how many epochs required for model with lstm training 训练我保存的 tensorflow model 时出错 - Errors when training my saved tensorflow model 使用Keras(Tensorflow)训练模型时出现形状错误 - Shape error when training a model with Keras (Tensorflow) 训练 CNN 模型时出错(tensorflow) - Getting error when training the CNN model(tensorflow) 训练聊天机器人时 Tensorflow 模型错误 - Tensorflow model error when training chatbot 具有张量流keras模型的动态历元数 - Dynamic number of epochs with a tensorflow keras model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM