基于tensroflow的线性回归(2):训练迭代求解(python)

这次使用Scikit Learn 的内建iris数据集,数据点(x值代表花瓣宽度,y值代表花瓣长度),然后找到它们的最优直线。

使用到不同的损失函数来对比其影响。

首先使用L1正则损失函数:L1 = tf.reduce_mean(tf.abs(y_data-model_output))

代码如下:

# cost fuction 为L1正则损失函数
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
sess = tf.Session()
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])
batch_size = 25
learning_rate = 0.1
iterations = 100
x_data = tf.placeholder(shape=[None,1],dtype=tf.float32)
y_data = tf.placeholder(shape=[None,1],dtype=tf.float32)
A = tf.Variable(tf.random_normal(shape=[1,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))
model_output = tf.add(tf.matmul(x_data,A),b)
loss_l1 = tf.reduce_mean(tf.abs(y_data-model_output))
init = tf.global_variables_initializer()
sess.run(init)
my_opt_l1 = tf.train.GradientDescentOptimizer(learning_rate)
train_step_l1 = my_opt_l1.minimize(loss_l1)
loss_vec_l1 = []
for i in range(iterations):
    rand_index = np.random.choice(len(x_vals),size = batch_size)
    rand_x = np.transpose([x_vals[rand_index]])
    rand_y = np.transpose([y_vals[rand_index]])
    sess.run(train_step_l1,feed_dict={x_data:rand_x,y_data:rand_y})
    temp_loss_l1 = sess.run(loss_l1,feed_dict={x_data:rand_x,y_data:rand_y})
    loss_vec_l1.append(temp_loss_l1)
    if(i+1)%25==0:
        print('step #'+str(i+1)+' A = '+str(sess.run(A))+' b = '+str(sess.run(b)))
[slope] = sess.run(A)
[y_intercept] = sess.run(b)
best_fit = []
for i in x_vals:
    best_fit.append(slope*i+y_intercept)
plt.plot(x_vals,y_vals,'o',label = 'data points')
plt.plot(x_vals,best_fit,'r-',label = 'best fit line',linewidth = 3)
plt.legend(loc = 'upper left')
plt.title('sepal length vs pedal width')
plt.xlabel('pedal width')
plt.ylabel('sepal length')
plt.show()
plt.plot(loss_vec_l1,'r--',label = 'l1 loss')
plt.title('l1 loss per generation')
plt.xlabel('generation')
plt.ylabel('l1 loss')
plt.legend(loc = 'upper right')
plt.show()

实验结果:



迭代训练也可以找到拟合直线,但看上并不理想,从第二幅图中可以看出随着迭代次数的增加,损失值在变小。

接着我们将损失函数改为L2正则函数: L2 =tf.reduce_mean(tf.square(y_data - model_output)),(y_data就是代码中的y_target)

代码如下:

# cost fuction 为L2正则损失函数
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
from sklearn import datasets
ops.reset_default_graph()
sess = tf.Session()
iris = datasets.load_iris()
x_vals = np.array([x[3] for x in iris.data])
y_vals = np.array([y[0] for y in iris.data])
learning_rate = 0.05
batch_size = 25
x_data = tf.placeholder(shape = [None,1],dtype = tf.float32)
y_target = tf.placeholder(shape=[None,1],dtype = tf.float32)
A = tf.Variable(tf.random_normal(shape = [1,1]))
B = tf.Variable(tf.random_normal(shape = [1,1]))
model_output = tf.add(tf.matmul(x_data,A),B)
loss = tf.reduce_mean(tf.square(y_target-model_output))
init = tf.global_variables_initializer()
sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(learning_rate)
train_step = my_opt.minimize(loss)
loss_vec = []
for i in range(100):
    rand_index = np.random.choice(len(x_vals),size = batch_size)
    rand_x = np.transpose([x_vals[rand_index]])
    rand_y = np.transpose([y_vals[rand_index]])
    sess.run(train_step,feed_dict= {x_data:rand_x,y_target:rand_y})
    temp_loss = sess.run(loss,feed_dict={x_data: rand_x,y_target:rand_y})
    loss_vec.append(temp_loss)
    if (i+1)%25==0:
        print('step#'+str(i+1)+',A = '+str(sess.run(A))+',B = '+str(sess.run(B)))
        print('loss = '+str(temp_loss))
[slope] = sess.run(A)
[y_intercept] = sess.run(B)
best_fit = []
for i in x_vals:
    best_fit.append(slope*i+y_intercept)
plt.plot(x_vals,y_vals,'o',label = 'data points')
plt.plot(x_vals,best_fit,'r-',label = 'best fit line',linewidth = 3)
plt.legend(loc = 'upper left')
plt.title('sepal length vs pedal width')
plt.xlabel('pedal width')
plt.ylabel('sepal length')
plt.show()
plt.plot(loss_vec,'k-')
plt.title('l2 loss per generation')
plt.xlabel('generation')
plt.ylabel('l2 loss')
plt.show()



实验结果:



用L2损失正则函数得到的优化直线看起来更好一点,损失值也下降的更快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值