Tensorflow 多元线性回归结果为 NaN

Posted

技术标签:

【中文标题】Tensorflow 多元线性回归结果为 NaN【英文标题】:Tensorflow Multivariate linear regression results in NaN 【发布时间】:2017-04-10 09:06:05 【问题描述】:

我正在使用sklearn 的波士顿住房数据集(一个 506x13 矩阵)进行多元线性回归。我计划使用所有数据对其进行训练,然后“插入”一个随机数据,例如boston_dataset.data[39],然后查看损失。但是当我打印结果时,我得到的只是NaN。这是我的代码。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston

np.set_printoptions(suppress=True)

boston = load_boston()

m = boston.data.shape[0] - 1

bt_unfixed = np.transpose(boston.data)
bt = np.insert(bt_unfixed, 0, 1)

Y = tf.placeholder(tf.float64, name='Y___')
X = tf.placeholder(tf.float64, [1, 13], name='X_____')
#print X.shape
W = tf.Variable(tf.zeros([13, 1]), name='weights')
b = tf.Variable(0.5, name='bias')

hypothesis = tf.add(tf.matmul(X, tf.cast(W, tf.float64)), tf.cast(b, tf.float64))

loss = tf.reduce_sum(tf.square(hypothesis - Y)) / (2 * m)

optimizer = tf.train.GradientDescentOptimizer(0.01)

train_op = optimizer.minimize(loss)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for i in range(0, 500):
        for (x, y) in zip(boston.data, boston.target):
            sess.run(train_op, feed_dict=X:x.reshape(1, 13), Y:y)
        if (i + 1)%50 == 0:
            print "Ran " + str(i) + "times\nW=" +str(sess.run(W)) + "\nb=" +str(sess.run(b))

    print "Done!\n"
    print "Running test...\n"
    t = sess.run(cost, feed_dict=X:boston.data[504], Y:boston.target.data[504])
    print "loss =" + str(t) + "Real value" + str(boston.target.data[504]) + "Pred " +str(sess.run(hypothesis, feed_dict=X:boston.data[504]))

谢谢!也请随时添加任何建议

【问题讨论】:

【参考方案1】:

您似乎没有对 boston 数据 进行任何数据预处理,这使得损失和假设值变为 inf (NaN) .所以我对数据进行了标准化,它可以工作。这是我的代码。

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston


boston = load_boston()

data = boston.data
label = boston.target

# normalized data
data -= np.mean(data, axis=0)
data /= np.std(data, axis=0)

M = boston.data.shape[0]


Y = tf.placeholder(tf.float32, name='Y')
X = tf.placeholder(tf.float32, [1, 13], name='X')

W = tf.Variable(tf.random_normal([13, 1]), name='weights')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.add(tf.matmul(X, W), b)

loss = tf.reduce_sum(tf.square(hypothesis - Y)) / (2. * (M - 1))

optimizer = tf.train.GradientDescentOptimizer(0.01)

train_op = optimizer.minimize(loss)

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())

    for i in range(0, 500):
        for l in xrange(M):
            _, loss_val, hypo = sess.run(
                [train_op, loss, hypothesis],
                feed_dict=X: data[l, :].reshape([1, 13]),
                           Y: label[l])
        if (i + 1) % 50 == 0:
            print "Ran " + str(i) + "times\nW=" + \
                str(sess.run(W)) + "\nb=" + str(sess.run(b))

    print "Done!\n"
    print "Running test...\n"
    t = sess.run(
        loss, feed_dict=X: data[50].reshape([1, 13]),
                         Y: label[50])
    print "loss =" + str(t)
    print "Real value Y: " + str(label[50])
    print "Pred Y: " + str(sess.run(hypothesis,
                                    feed_dict=X: data[50].reshape([1, 13])))

【讨论】:

以上是关于Tensorflow 多元线性回归结果为 NaN的主要内容,如果未能解决你的问题,请参考以下文章

使用 Tensorflow 的多元线性回归模型

多元线性回归问题(Tensorflow 求解)

回归分析 R语言 -- 多元线性回归

SPSS多元线性回归输出结果的详细解释

5.2 多元线性回归完成广告投放销售额预测——python实战

tensorflow 做多元线性回归时怎样对非数据型数据(分类型数据)进行处理(编码)