继续

0x00.前言

0x01.引用

1.0 TensorFlow实现线性回归模型代码

1.1 前期准备

TensorFlow相关API可以到在实验TensorFlow - 相关 API中学习。

1.2 模型构建

现在您可以在/home/ubuntu目录下创建源文件linear_regression_model.py,内容可参考:

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*
3
import tensorflow as tf
4
import numpy as np
5
6
class linearRegressionModel:
7
8
  def __init__(self,x_dimen):
9
    self.x_dimen = x_dimen
10
    self._index_in_epoch = 0
11
    self.constructModel()
12
    self.sess = tf.Session()
13
    self.sess.run(tf.global_variables_initializer())
14
15
  #权重初始化
16
  def weight_variable(self,shape):
17
    initial = tf.truncated_normal(shape,stddev = 0.1)
18
    return tf.Variable(initial)
19
20
  #偏置项初始化
21
  def bias_variable(self,shape):
22
    initial = tf.constant(0.1,shape = shape)
23
    return tf.Variable(initial)
24
25
  #每次选取100个样本,如果选完,重新打乱
26
  def next_batch(self,batch_size):
27
    start = self._index_in_epoch
28
    self._index_in_epoch += batch_size
29
    if self._index_in_epoch > self._num_datas:
30
        perm = np.arange(self._num_datas)
31
        np.random.shuffle(perm)
32
        self._datas = self._datas[perm]
33
        self._labels = self._labels[perm]
34
        start = 0
35
        self._index_in_epoch = batch_size
36
        assert batch_size <= self._num_datas
37
    end = self._index_in_epoch
38
    return self._datas[start:end],self._labels[start:end]
39
40
  def constructModel(self):
41
    self.x = tf.placeholder(tf.float32, [None,self.x_dimen])
42
    self.y = tf.placeholder(tf.float32,[None,1])
43
    self.w = self.weight_variable([self.x_dimen,1])
44
    self.b = self.bias_variable([1])
45
    self.y_prec = tf.nn.bias_add(tf.matmul(self.x, self.w), self.b)
46
47
    mse = tf.reduce_mean(tf.squared_difference(self.y_prec, self.y))
48
    l2 = tf.reduce_mean(tf.square(self.w))
49
    self.loss = mse + 0.15*l2
50
    self.train_step = tf.train.AdamOptimizer(0.1).minimize(self.loss)
51
52
  def train(self,x_train,y_train,x_test,y_test):
53
    self._datas = x_train
54
    self._labels = y_train
55
    self._num_datas = x_train.shape[0]
56
    for i in range(5000):
57
        batch = self.next_batch(100)
58
        self.sess.run(self.train_step,feed_dict={self.x:batch[0],self.y:batch[1]})
59
        if i%10 == 0:
60
            train_loss = self.sess.run(self.loss,feed_dict={self.x:batch[0],self.y:batch[1]})
61
            print('step %d,test_loss %f' % (i,train_loss))
62
63
  def predict_batch(self,arr,batch_size):
64
    for i in range(0,len(arr),batch_size):
65
        yield arr[i:i + batch_size]
66
67
  def predict(self, x_predict):
68
    pred_list = []
69
    for x_test_batch in self.predict_batch(x_predict,100):
70
      pred = self.sess.run(self.y_prec, {self.x:x_test_batch})
71
      pred_list.append(pred)
72
    return np.vstack(pred_list)

1.3 训练模型并和sklearn库线性回归模型对比

现在您可以在/home/ubuntu目录下创建源文件run.py,内容可参考:

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*
3
4
from sklearn.model_selection import train_test_split
5
from sklearn.metrics import r2_score
6
from sklearn.datasets import make_regression
7
from sklearn.linear_model import LinearRegression
8
from linear_regression_model import linearRegressionModel as lrm
9
10
if __name__ == '__main__':
11
    x, y = make_regression(7000)
12
    x_train,x_test,y_train, y_test = train_test_split(x, y, test_size=0.5)
13
    y_lrm_train = y_train.reshape(-1, 1)
14
    y_lrm_test = y_test.reshape(-1, 1)
15
16
    linear = lrm(x.shape[1])
17
    linear.train(x_train, y_lrm_train,x_test,y_lrm_test)
18
    y_predict = linear.predict(x_test)
19
    print("Tensorflow R2: ", r2_score(y_predict.ravel(), y_lrm_test.ravel()))
20
21
    lr = LinearRegression()
22
    y_predict = lr.fit(x_train, y_train).predict(x_test)
23
    print("Sklearn R2: ", r2_score(y_predict, y_test)) #采用r2_score评分函数

然后执行:
cd /home/ubuntu
python run.py
执行结果:

1
step 2410,test_loss 26.531937
2
step 2420,test_loss 26.542793
3
step 2430,test_loss 26.533974
4
step 2440,test_loss 26.530540
5
step 2450,test_loss 26.551474
6
step 2460,test_loss 26.541542
7
step 2470,test_loss 26.560783
8
step 2480,test_loss 26.538080
9
step 2490,test_loss 26.535666
10
('Tensorflow R2: ', 0.99999612588302389)
11
('Sklearn R2: ', 1.0)

0x02.后记

emmm……真的好快

未完待续……