Tensorflow:简单的线性回归

时间:2017-05-23 17:21:19

标签: tensorflow

我刚开始在python中使用Tensorflow来解决优化问题。我只是尝试了一个非常简单的回归模型。但是我得到的结果(斜率和常数)似乎与我的期望相差甚远,任何人都可以指出我做错了什么(代码运行,但我不确定我是否正确使用Tensorflow)。

我做了什么:
1个导入模块:

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

2基于线性结构创建数据(y = 3 X + 4 +错误):

train_X = np.array(range(-20,20,1))
b = 3; c = 4; sd = 0.5;
error = np.random.normal(loc=0.0, scale=sd, size=40);
deterministic =  b* train_X + c;
train_Y  = np.add(deter,error)

3设置优化:

X = tf.placeholder(tf.float32,[40])
Y = tf.placeholder(tf.float32,[40])

beta = tf.Variable(np.random.randn(), name="beta") 
alpha = tf.Variable(np.random.randn(), name="alpha")

n_samples = 40
learning_rate = 0.01

pred_full = tf.add(tf.scalar_mul(beta, X),alpha)
cost = tf.reduce_mean(tf.pow(tf.subtract(Y, pred_full),2))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

4运行它:

init = tf.global_variables_initializer()
with tf.Session() as sess:
        sess.run(init)
        sess.run(optimizer, feed_dict={X: train_X, Y: train_Y}) 
        result = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) 
        result_beta = sess.run(beta, feed_dict={X: train_X, Y: train_Y}) 
        result_alpha = sess.run(alpha, feed_dict={X: train_X, Y: train_Y}) 
        print('result:', result, ';', 'result_beta:', result_beta, ';', 'result_alpha:',result_alpha)

我获得的结果是:

 result: 1912.99 ; result_beta: 6.75786 ; result_alpha: -0.209623

显然beta应该接近3,而alhpa应该接近4.我想知道我的代码出了什么问题?

由于

1 个答案:

答案 0 :(得分:1)

您必须多次调用优化器才能进行多次梯度下降迭代。至于,@ dv3注意到,试试

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for i in range(50):
        opt, result_alpha, result_beta = sess.run([optimizer, alpha, beta], feed_dict={X: train_X, Y: train_Y})

    print('beta =', result_beta, 'alpha =', result_alpha)

注意:没有必要通过单独调用run()来访问多个张量值。您可以使用要获取的值列表来执行此操作。