gpt4 book ai didi

python - Tensorflow 上的多元回归

转载 作者:太空宇宙 更新时间:2023-11-04 05:02:57 24 4
gpt4 key购买 nike

我是 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/

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