gpt4 book ai didi

python - Tensorflow 产生 NaN

转载 作者:行者123 更新时间:2023-11-30 09:09:04 27 4
gpt4 key购买 nike

我正在尝试将 TensorFlow 入门页面上的示例线性回归程序调整为二次回归。为此,我只是添加了另一个变量并更改了函数。然而,这似乎会导致 NaN 值。这是我的代码:

import numpy as np
import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sess = tf.Session()
a = tf.Variable([1.], dtype=tf.float32)
b = tf.Variable([0.3], dtype=tf.float32)
c = tf.Variable([0.3], dtype = tf.float32)
x = tf.placeholder(tf.float32)
x_train = [1,2,3,4]
y_train =[1,4,9,16]
quad_model = a * x * x + b * x + c
init = tf.global_variables_initializer()
sess.run(init)
y = tf.placeholder(tf.float32) # Model Data
squared_deltas = tf.square(quad_model - y)
loss = tf.reduce_sum(squared_deltas)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
for i in range(1000):
sess.run(train, {x:x_train, y:y_train})
print(sess.run([a,b,c]))

print(sess.run([a, b, c]))

如有任何帮助,我们将不胜感激!

最佳答案

也许过度拟合导致NaN,你可以减少学习率和训练次数。

关于python - Tensorflow 产生 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009776/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com