1、线性SVM,检查C对分界线的影响
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 21 16:05:25 2019
@author: 无限未来
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
svmC=1
BATCH_SIZE=51
data = loadmat('ex6data1.mat')
X = data['X']
Y = data['y'].astype('float64')
Y[np.where(Y==0)]=-1
y_0 = np.where(Y==-1)
y_1 = np.where(Y==1)
x = tf.placeholder("float", shape=[None,2])
y = tf.placeholder("float", shape=[None,1])
W = tf.Variable(tf.zeros([2,1]))
b = tf.Variable(tf.zeros([1]))
y_raw = tf.matmul(x,W) + b
# Optimization.
regularization_loss = 0.5*tf.reduce_sum(tf.square(W))
hinge_loss = tf.reduce_sum(tf.maximum(tf.zeros([BATCH_SIZE,1]),
1 - y*y_raw))
svm_loss = regularization_loss + svmC*hinge_loss;
train = tf.train.AdamOptimizer(0.1).minimize(svm_loss)
# Evaluation.
predicted_class = tf.sign(y_raw);
correct_prediction = tf.equal(y,predicted_class)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
with tf.Session() as s:
tf.initialize_all_variables().run()
for step in range(5000):
_,lossval= s.run([train,svm_loss],feed_dict={x: X, y: Y})
print ( "cost=","{:.9f}".format(lossval) )
print ("Accuracy on train:", accuracy.eval(feed_dict={x: X, y: Y}))
w,b=s.run([W,b], feed_dict={x: X, y: Y})
plt.scatter(X[y_0,0], X[y_0,1], c='g')
plt.scatter(X[y_1,0], X[y_1,1], c='b')
x = np.linspace(0,5,5)
y = -(w[0]*x+b)/w[1]
plt.plot(x,y,c='r')
2、高斯核SVM
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 21 16:05:25 2019
@author: 无限未来
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import loadmat
svmC=1
BATCH_SIZE=51
data = loadmat('ex6data2.mat')
X = data['X']
Y = data['y'].astype('float64')
Y[np.where(Y==0)]=-1
y_0 = np.where(Y==-1)
y_1 = np.where(Y==1)
#声明批量大小、占位符以及变量b
batch_size=863
x_data=tf.placeholder(shape=[None,2],dtype=tf.float32)
y_target=tf.placeholder(shape=[None,1],dtype=tf.float32)
prediction_grid=tf.placeholder(shape=[None,2],dtype=tf.float32)
b=tf.Variable(tf.random_normal(shape=[1,batch_size]))
#创建高斯函数
gamma=tf.constant(-50.0)
dist=tf.reduce_sum(tf.square(x_data),1)
dist=tf.reshape(dist,[-1,1])
sq_dists=tf.add(tf.subtract(dist,tf.multiply(2.,tf.matmul(x_data,tf.transpose(x_data)))),tf.transpose(dist))
my_kernel=tf.exp(tf.multiply(gamma,tf.abs(sq_dists)))
#PS:线性核函数的表达式可以为:my_kernel=tf.matmul(x_data,tf.transpose(x_data))
#声明对偶问题,为了最大化,这里采用最小损失函数的负数:tf.negative()
model_output=tf.matmul(b,my_kernel)
first_term=tf.reduce_sum(b)
b_vec_cross=tf.matmul(tf.transpose(b),b)
y_target_cross=tf.matmul(y_target,tf.transpose(y_target))
second_term=tf.reduce_sum(tf.multiply(my_kernel,tf.multiply(b_vec_cross,y_target_cross)))
#取反
loss=tf.negative(tf.subtract(first_term,second_term))
#创建预测函数和准确度函数
rA=tf.reshape(tf.reduce_sum(tf.square(x_data),1),[-1,1])
rB=tf.reshape(tf.reduce_sum(tf.square(prediction_grid),1),[-1,1])
pred_sq_dist=tf.add(tf.subtract(rA,tf.multiply(2.,tf.matmul(x_data,tf.transpose(prediction_grid)))),tf.transpose(rB))
pred_kernel=tf.exp(tf.multiply(gamma,tf.abs(pred_sq_dist)))
prediction_output=tf.matmul(tf.multiply(tf.transpose(y_target),b),pred_kernel)
prediction=tf.sign(prediction_output-tf.reduce_mean(prediction_output))
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.squeeze(prediction),tf.squeeze(y_target)),tf.float32))
#创建优化器
train = tf.train.AdamOptimizer(0.01).minimize(loss)
# Create a local session to run this computation.
with tf.Session() as s:
# Run all the initializers to prepare the trainable parameters.
tf.initialize_all_variables().run()
# Iterate and train.
for step in range(1000):
_,lossval= s.run([train,loss],feed_dict={x_data: X, y_target: Y,prediction_grid:X})
print ( "cost=","{:.9f}".format(lossval) )
print ("Accuracy on train:", accuracy.eval(feed_dict={x_data: X, y_target: Y,prediction_grid:X}))
#创建数据点网格用于后续的数据空间可视化分类
x_min,x_max=X[:,0].min(),X[:,0].max()
y_min,y_max=X[:,1].min(),X[:,1].max()
xx,yy=np.meshgrid(np.arange(x_min,x_max,0.02),
np.arange(y_min,y_max,0.02))
grid_points=np.c_[xx.ravel(),yy.ravel()]
[grid_predictions]=s.run(prediction,feed_dict={x_data:X,
y_target:Y,
prediction_grid:grid_points})
grid_predictions=grid_predictions.reshape(xx.shape)
#绘制预测结果
plt.contourf(xx,yy,grid_predictions,cmap=plt.cm.Paired,alpha=0.8)
class1_x=[x[0] for i,x in enumerate(X) if Y[i]==1]
class1_y=[x[1] for i,x in enumerate(X) if Y[i]==1]
class2_x=[x[0] for i,x in enumerate(X) if Y[i]==-1]
class2_y=[x[1] for i,x in enumerate(X) if Y[i]==-1]
plt.plot(class1_x,class1_y,'ro',label='NO')
plt.plot(class2_x,class2_y,'kx',label='YES')
plt.legend(loc='lower right')
plt.show()