TensorFlow学习笔记(1):线性回归

https://segmentfault.com/a/1190000007966370

前言

本文使用tensorflow训练线性回归模型,并将其与scikit-learn做比较。数据集来自Andrew Ng的网上公开课程Deep Learning

代码


In [1]:
import numpy as np
import tensorflow as tf

from sklearn import linear_model

# Read x and y
x_data = np.loadtxt("input/ex2x.dat")
y_data = np.loadtxt("input/ex2y.dat")

# we use sklearn first to get a sense of coefficients
reg = linear_model.LinearRegression()
reg.fit(x_data.reshape(-1, 1), y_data)
print("Coeficient of sklearn linear regression: k=%f, b=%f"
      % (reg.coef_, reg.intercept_))

# Then we apply tensorflow to achieve the similar results
# The structure of tensorflow code can be divided into two parts:
# 
# First part: set up computation graph
W = tf.Variable(tf.random_uniform([1], -1, 1))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

loss = tf.reduce_mean(tf.square(y - y_data)) / 2
optimizer = tf.train.GradientDescentOptimizer(0.07)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

# Second part: launch the graph
sess = tf.Session()
sess.run(init)

for step in range(1500):
    sess.run(train)
    
    if step % 100 == 0:
        print("Coeficient of tensorflow linear regression: k=%f, b=%f"
             % (sess.run(W), sess.run(b)))


Coeficient of sklearn linear regression: k=0.063881, b=0.750163
Coeficient of tensorflow linear regression: k=0.633821, b=0.171544
Coeficient of tensorflow linear regression: k=0.133563, b=0.434749
Coeficient of tensorflow linear regression: k=0.091980, b=0.597511
Coeficient of tensorflow linear regression: k=0.077249, b=0.676734
Coeficient of tensorflow linear regression: k=0.070305, b=0.714853
Coeficient of tensorflow linear regression: k=0.066970, b=0.733184
Coeficient of tensorflow linear regression: k=0.065367, b=0.741998
Coeficient of tensorflow linear regression: k=0.064595, b=0.746237
Coeficient of tensorflow linear regression: k=0.064225, b=0.748275
Coeficient of tensorflow linear regression: k=0.064046, b=0.749255
Coeficient of tensorflow linear regression: k=0.063961, b=0.749726
Coeficient of tensorflow linear regression: k=0.063919, b=0.749953
Coeficient of tensorflow linear regression: k=0.063900, b=0.750062
Coeficient of tensorflow linear regression: k=0.063890, b=0.750114
Coeficient of tensorflow linear regression: k=0.063885, b=0.750139

思考

对于tensorflow,梯度下降的步长alpha参数需要很仔细的设置,步子太大容易扯到蛋导致无法收敛;步子太小容易等得蛋疼。迭代次数也需要细致的尝试。