繁体   English   中英

Tensorflow简单线性回归

[英]Tensorflow on simple linear regression

我是机器学习和张量流的初学者。 在尝试张量流的第一步中,我尝试了一个简单的多元线性回归。 然而,似乎该模型陷入了局部最低限度。 这是我的代码。

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

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=1)
    return tf.Variable(initial)

# dataset
xx = np.random.randint(0,1000,[1000,3])/1000.
yy = xx[:,0] * 2 + xx[:,1] * 1.4 + xx[:,2] * 3

# model
x = tf.placeholder(tf.float32, shape=[None, 3])
y_ = tf.placeholder(tf.float32, shape=[None])
W1 = weight_variable([3, 1])
y = tf.matmul(x, W1)

# training and cost function
cost_function = tf.reduce_mean(tf.square(y - y_))
train_function = tf.train.AdamOptimizer(1e-2).minimize(cost_function)

# create a session
sess = tf.Session()

# train
sess.run(tf.initialize_all_variables())
for i in range(10000):
    sess.run(train_function, feed_dict={x:xx, y_:yy})
    if i % 1000 == 0:
        print(sess.run(cost_function, feed_dict={x:xx, y_:yy}))

输出是:

14.8449
2.20154
2.18375
2.18366
2.18366
2.18366
2.18366
2.18366
2.18366

输出值(yy)的范围从0到6,因此,均方误差2.18相当大,因为知道数据集中没有添加噪声。 我也尝试过GradientDescentOptimizer,学习率为0.1和1e-2,但它并没有太大改善结果。

我的实施有什么问题吗?

这是因为yy_形状不同。 y具有形状( y_ )并且y_具有形状(1000)。 因此,当您减去它们时,您无意中会创建一个二维矩阵。

要修复它,请将成本函数更改为:

cost_function = tf.reduce_mean(tf.square(tf.squeeze(y) - y_))

如另一个答案所述,你必须使用

predictions = tf.add(b, tf.matmul(x, w))
error = tf.reduce_mean(tf.square(y - predictions))

正如你所说,你是Tensorflow的初学者,你可以看看这里的例子: -

https://medium.com/@saxenarohan97/intro-to-tensorflow-solving-a-simple-regression-problem-e87b42fd4845

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM