TensorFlow中使用支持向量机拟合线性回归

支持向量机可以用来拟合线性回归。
相同的最大间隔(maximum margin)的概念应用到线性回归拟合。代替最大化分割两类目标是,最大化分割包含大部分的数据点(x,y)。我们将用相同的iris数据集,展示用刚才的概念来进行花萼长度与花瓣宽度之间的线性拟合。

相关的损失函数类似于max(0,|yi-(Axi+b)|-ε)。ε这里,是间隔宽度的一半,这意味着如果一个数据点在该区域,则损失等于0。

# SVM Regression#----------------------------------## This function shows how to use TensorFlow to
# solve support vector regression. We are going# to find the line that has the maximum margin
# which INCLUDES as many points as possible## We will use the iris data, specifically:
#  y = Sepal Length#  x = Pedal Widthimport matplotlib.pyplot as pltimport numpy as npimport tensorflow 
as tffrom sklearn import datasetsfrom tensorflow.python.framework import opsops.reset_default_graph()
# Create graphsess = tf.Session()# Load the data# iris.data = [(Sepal Length, Sepal Width, Petal Length, 
Petal Width)]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])# Split data into train/test setstrain_indices = np.random.choice(len(x_vals), 
round(len(x_vals)*0.8), replace=False)test_indices = np.array(list(set(range(len(x_vals))) - 
set(train_indices)))x_vals_train = x_vals[train_indices]x_vals_test = x_vals[test_indices]y_vals_train = 
y_vals[train_indices]y_vals_test = y_vals[test_indices]# Declare batch sizebatch_size = 50
# Initialize placeholdersx_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)y_target = tf
.placeholder(shape=[None, 1], dtype=tf.float32)# Create variables for linear regressionA = tf.Variable(tf
.random_normal(shape=[1,1]))b = tf.Variable(tf.random_normal(shape=[1,1]))
# Declare model operationsmodel_output = tf.add(tf.matmul(x_data, A), b)# Declare loss function
# = max(0, abs(target - predicted) + epsilon)# 1/2 margin width parameter = epsilonepsilon = tf.constant([0.5])
# Margin term in lossloss = tf.reduce_mean(tf.maximum(0., tf.subtract(tf.abs(tf
.subtract(model_output, y_target)), epsilon)))# Declare optimizermy_opt = tf.train
.GradientDescentOptimizer(0.075)train_step = my_opt.minimize(loss)
# Initialize variablesinit = tf.global_variables_initializer()sess.run(init)
# Training looptrain_loss = []test_loss = []for i in range(200):    
rand_index = np.random.choice(len(x_vals_train), size=batch_size)    
rand_x = np.transpose([x_vals_train[rand_index]])    
rand_y = np.transpose([y_vals_train[rand_index]])    
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})    
temp_train_loss = sess.run(loss, feed_dict={x_data: np
.transpose([x_vals_train]), y_target: np.transpose([y_vals_train])})    
train_loss.append(temp_train_loss)    
temp_test_loss = sess.run(loss, 
feed_dict={x_data: np.transpose([x_vals_test]), y_target: np.transpose([y_vals_test])})    
test_loss.append(temp_test_loss)    if (i+1)%50==0:        print('-----------')        
print('Generation: ' + str(i+1))        print('A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))        
print('Train Loss = ' + str(temp_train_loss))        print('Test Loss = ' + str(temp_test_loss))
# Extract Coefficients[[slope]] = sess.run(A)[[y_intercept]] = sess.run(b)[width] = sess.run(epsilon)
# Get best fit linebest_fit = []best_fit_upper = []best_fit_lower = []for i in x_vals:  best_fit
.append(slope*i+y_intercept)  best_fit_upper.append(slope*i+y_intercept+width)  best_fit_lower
.append(slope*i+y_intercept-width)# Plot fit with dataplt.plot(x_vals, y_vals, 'o', 
label='Data Points')plt.plot(x_vals, best_fit, 'r-', label='SVM Regression Line', linewidth=3)plt
.plot(x_vals, best_fit_upper, 'r--', linewidth=2)plt.plot(x_vals, best_fit_lower, 'r--', linewidth=2)plt
.ylim([0, 10])plt.legend(loc='lower right')plt.title('Sepal Length vs Pedal Width')plt.xlabel('Pedal Width')
plt.ylabel('Sepal Length')plt.show()# Plot loss over timeplt.plot(train_loss, 'k-', label='Train Set Loss')
plt.plot(test_loss, 'r--', label='Test Set Loss')plt.title('L2 Loss per Generation')plt.xlabel('Generation')plt.ylabel('L2 Loss')plt.legend(loc='upper right')plt.show()1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.

输出结果:

-----------Generation: 50A = [[ 2.91328382]] b = [[ 1.18453276]]Train Loss = 1.17104Test Loss = 1.1143-----------Generation: 100A = [[ 2.42788291]] b = [[ 2.3755331]]Train Loss = 0.703519Test Loss = 0.715295-----------Generation: 150A = [[ 1.84078252]] b = [[ 3.40453291]]Train Loss = 0.338596Test Loss = 0.365562-----------Generation: 200A = [[ 1.35343242]] b = [[ 4.14853334]]Train Loss = 0.125198Test Loss = 0.161211.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.

Tensorflow用支持向量机来拟合线性回归_拟合


基于iris数据集(花萼长度和花瓣宽度)的支持向量机回归,间隔宽度为0.5
Tensorflow用支持向量机来拟合线性回归_tensorflow_02


每次迭代的支持向量机回归的损失值(训练集和测试集)

直观地讲,我们认为SVM回归算法试图把更多的数据点拟合到直线两边2ε宽度的间隔内。这时拟合的直线对于ε参数更有意义。如果选择太小的ε值,SVM回归算法在间隔宽度内不能拟合更多的数据点;如果选择太大的ε值,将有许多条直线能够在间隔宽度内拟合所有的数据点。作者更倾向于选取更小的ε值,因为在间隔宽度附近的数据点比远处的数据点贡献更少的损失。

               



免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空