我真的是StackOverflow的新手,所以如果我说些蠢话,请原谅我。我在tensorflow库中编码了这种多元线性回归,但是由于某种原因它无法正常工作,损耗只会增加,然后变为零。

# coding: utf-8

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().magic('matplotlib inline')

from sklearn.datasets import load_boston

data=load_boston()

X_data = data.data
y_data = data.target
m = len(X_data)
n = len(X_data[0])

X = tf.placeholder(tf.float32,[m,n])
y = tf.placeholder(tf.float32,[m,1])

W = tf.Variable(tf.ones([n,1]))
b = tf.Variable(tf.ones([1]))

y_ = tf.matmul(X,W)+b

loss = tf.reduce_mean(tf.square( y - y_))

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    vals = []
    for i in range(100):
        val = sess.run([train,loss],feed_dict={X:X_data , y:y_data[:,None]})
        vals.append(val)

print(vals)


输出为:

[[None, 823712.88],
 [None, 3.2238912e+13],
 [None, 1.2631078e+21],
 [None, 4.9488092e+28],
 [None, 1.9389255e+36],
 [None, inf],
 [None, inf],
 [None, inf],
 [None, inf],
 [None, inf],
 [None, inf],
 [None, nan],
 [None, nan],
 [None, nan],
 [None, nan],
 [None, nan],
...
 [None, nan],
 [None, nan]]


我找不到哪里出了问题...帮助吗?任何人?

最佳答案

看来您的学习率太高了。如果您将学习率降低到1e-6,则收敛。

关于python - Tensorflow的多元回归,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45262280/

10-12 19:09