Tensorflow 梯度下降不是';行不通

Tensorflow 梯度下降不是';行不通,tensorflow,linear-regression,gradient-descent,non-linear-regression,Tensorflow,Linear Regression,Gradient Descent,Non Linear Regression,我正在斯坦福大学学习tensorflow课程,名为“tensorflow for Deep learning Research”。我从以下代码中获取了代码。在探索张量流时,我改变了 Y_预测=X*w+b 作为 Y_=​ X​* ​ X​* ​ W​+ ​ X​* ​ U​+ ​ b 检查非线性曲线是否拟合得更好。我补充说 你预测​= ​ X​* ​ X​* ​ W​+ ​ X​* ​ U​+ ​ b 根据作者的建议。但是在添加这一行并再次运行类似的代码之后,每个错误值似乎都得到了nan。 有人能

我正在斯坦福大学学习tensorflow课程,名为“tensorflow for Deep learning Research”。我从以下代码中获取了代码。在探索张量流时,我改变了

Y_预测=X*w+b

作为

Y_=​ X​* ​ X​* ​ W​+ ​ X​* ​ U​+ ​ b

检查非线性曲线是否拟合得更好。我补充说

你预测​= ​ X​* ​ X​* ​ W​+ ​ X​* ​ U​+ ​ b

根据作者的建议。但是在添加这一行并再次运行类似的代码之后,每个错误值似乎都得到了nan。 有人能指出问题并给出解决办法吗

""" Simple linear regression example in TensorFlow
This program tries to predict the number of thefts from 
the number of fire in the city of Chicago
Author: Chip Huyen
Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"
cs20si.stanford.edu
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import xlrd

#import utils

DATA_FILE = "slr05.xls"

# Step 1: read in data from the .xls file
book = xlrd.open_workbook(DATA_FILE, encoding_override="utf-8")
sheet = book.sheet_by_index(0)
data = np.asarray([sheet.row_values(i) for i in range(1, sheet.nrows)])
n_samples = sheet.nrows - 1

# Step 2: create placeholders for input X (number of fire) and label Y (number of theft)
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')

# Step 3: create weight and bias, initialized to 0
w = tf.Variable(0.0, name='weights')
u = tf.Variable(0.0, name='weights2')
b = tf.Variable(0.0, name='bias')

# Step 4: build model to predict Y
#Y_predicted = X * w + b 
Y_predicted = X ​* ​ X ​* ​ w ​+ ​ X ​* ​ u ​+ ​ b

# Step 5: use the square error as the loss function
loss = tf.square(Y - Y_predicted, name='loss')
# loss = utils.huber_loss(Y, Y_predicted)

# Step 6: using gradient descent with learning rate of 0.01 to minimize loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

with tf.Session() as sess:
    # Step 7: initialize the necessary variables, in this case, w and b
    sess.run(tf.global_variables_initializer()) 

    writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph)

    # Step 8: train the model
    for i in range(100): # train the model 100 epochs
        total_loss = 0
        for x, y in data:
            # Session runs train_op and fetch values of loss
            _, l = sess.run([optimizer, loss], feed_dict={X: x, Y:y}) 
            total_loss += l
        print('Epoch {0}: {1}'.format(i, total_loss/n_samples))

    # close the writer when you're done using it
    writer.close() 

    # Step 9: output the values of w and b
    w, u , b = sess.run([w, u , b]) 

# plot the results
X, Y = data.T[0], data.T[1]
plt.plot(X, Y, 'bo', label='Real data')
plt.plot(X, X * x * w + X * u + b, 'r', label='Predicted data')
plt.legend()
plt.show()

哎呀!您的学习率似乎太高,尝试类似于
learning\u rate=0.0000001
的方法,它会收敛。这是一个常见的问题,尤其是当您引入交互功能时,就像您的例子一样:您应该记住,
x**2
的范围将更大(如果原始值为[-100100],则二次值将为[-10000,10000]),因此,对于线性模型来说,适用的学习率可能对于多项式模型来说太大。检查一下。此图给出了更直观的解释:

希望有帮助

安德烈斯

哎呀!您的学习率似乎太高,尝试类似于
learning\u rate=0.0000001
的方法,它会收敛。这是一个常见的问题,尤其是当您引入交互功能时,就像您的例子一样:您应该记住,
x**2
的范围将更大(如果原始值为[-100100],则二次值将为[-10000,10000]),因此,对于线性模型来说,适用的学习率可能对于多项式模型来说太大。检查一下。此图给出了更直观的解释:

希望有帮助

安德烈斯

我是教这门课的人。就像@fr_andres说的,你的lr可能太大了。如果不起作用,请告诉我。

我是教这门课的人。就像@fr_andres说的,你的lr可能太大了。如果不起作用,请告诉我。

现在我知道问题出在哪里了。谢谢你指出。将梯度设置为0.00000001会产生比以前的线性基函数更好的误差739。绘图后,我得到以下输出,为什么有多条红线?这种情况是否发生在基函数展开中(因为数据在更高的维度中)嗨!这似乎是一个阴谋问题。。。你应该看到一条抛物线,它对应于你优化的w、u和b参数。你可以在WolframAlpha中尝试它:享受TF的乐趣!现在我明白问题出在哪里了。谢谢你指出。将梯度设置为0.00000001会产生比以前的线性基函数更好的误差739。绘图后,我得到以下输出,为什么有多条红线?这种情况是否发生在基函数展开中(因为数据在更高的维度中)嗨!这似乎是一个阴谋问题。。。你应该看到一条抛物线,它对应于你优化的w、u和b参数。你可以在WolframAlpha中尝试它:享受TF的乐趣!