tensorflow——SVM实现

tensorflow——SVM实现

ei1994 2017-08-07 15:42:37 9182 收藏
版权
1、SVM实现

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# 生成数据集
def gen_two_clusters(size=100, n_dim=2, center=0, dis=2, scale=1, one_hot=True):
center1 = (np.random.random(n_dim) + center - 0.5) * scale + dis
center2 = (np.random.random(n_dim) + center - 0.5) * scale - dis
cluster1 = (np.random.randn(size, n_dim) + center1) * scale
cluster2 = (np.random.randn(size, n_dim) + center2) * scale
data = np.vstack((cluster1, cluster2)).astype(np.float32)
labels = np.array([1] * size + [0] * size)
indices = np.random.permutation(size * 2)
data, labels = data[indices], labels[indices]
if not one_hot:
return data, labels
labels = np.array([[0, 1] if label == 1 else [1, 0] for label in labels], dtype=np.int8)
return data, labels
# 生成网格数据集
def get_base(_nx, _ny):
_xf = np.linspace(x_min, x_max, _nx)
_yf = np.linspace(y_min, y_max, _ny)
n_xf, n_yf = np.meshgrid(_xf, _yf)
return _xf, _yf, np.c_[n_xf.ravel(), n_yf.ravel()]


#用于生成数据
#x, y = gen_two_clusters(n_dim=2, dis=2.5, center=5, one_hot=False)
#np.save('x.npy',x)
#np.save('y.npy',y)
x_ = np.load('x.npy')
y_ = np.load('y.npy')
y_ = y_.reshape(-1,1)

title = 'linear_SVM'
#plt.figure()
plt.title(title)
#plt.xlim(x_min, x_max)
#plt.ylim(y_min, y_max)
y_0 = np.where(y_==1)
y_1 = np.where(y_==-1)
#plt.scatter(x_[y_0,0], x_[y_0,1], c='g')
#plt.scatter(x_[y_1,0], x_[y_1,1], c='r')
#plt.show()

c = 1
lr = 0.01
batch_size = 128
epoch = 1000
tol = 1e-3
padding = 0.1

x = tf.placeholder(tf.float32, [None, 2])
y = tf.placeholder(tf.float32, [None, 1])

W = tf.Variable(np.zeros([2,1]), dtype=tf.float32, name='w')
b = tf.Variable(0, dtype=tf.float32, name='b')

y_pred1 = tf.matmul(x, W) + b
y_pred = tf.sign(y_pred1)
loss = tf.reduce_mean(tf.reduce_sum( tf.nn.relu(1-y_*y_pred1)) + c* tf.nn.l2_loss(W))
optimizer = tf.train.AdamOptimizer(learning_rate=lr).minimize(loss)

tf.summary.scalar('loss', loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('nlogs/', sess.graph)
sess.run(init)
for i in range(epoch):
_, loss_ ,W_,b_= sess.run([optimizer,loss,W,b], feed_dict={x: x_, y:y_})
y_pred_,y_pred1_, w = sess.run([ y_pred,y_pred1,W], feed_dict={x: x_, y:y_})
if i%50 ==0:
# loss_ = sess.run(loss, feed_dict={x: x_, y:y_})
# print(loss_)
result= sess.run(merged, feed_dict={x: x_, y:y_})
writer.add_summary(result, i)

x_min, x_max = np.min(x_[:,0]), np.max(x_[:,0])
y_min, y_max = np.min(x_[:,1]), np.max(x_[:,1])
x_padding = max(abs(x_min), abs(x_max)) * padding
y_padding = max(abs(y_min), abs(y_max)) * padding
x_min -= x_padding
x_max += x_padding
y_min -= y_padding
y_max += y_padding
xf, yf, base_matrix = get_base(200, 200)

z = np.sign(np.matmul(base_matrix,w)+b_).reshape((200, 200))
#z = npbase_matrix.reshape((200, 200))
plt.contour(xf, yf, z, c='k-', levels=[0.5]) #画分界限的(等高线)
#xy_xf, xy_yf = np.meshgrid(xf, yf, sparse=True)
plt.pcolormesh(xf, yf, z, cmap=plt.cm.Paired) #画分界线背景颜色的
#visualize2d(x_, y_, padding=0.1, dense=400)
plt.scatter(x_[y_0,0], x_[y_0,1], c='g')
plt.scatter(x_[y_1,0], x_[y_1,1], c='b')
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
2、SVM—定义类的函数

import tensorflow as tf
import numpy as np
import math
from matplotlib import pyplot as plt
from tensorflow import flags

class SVM():
def __init__(self):
self.x=tf.placeholder('float',shape=[None,2],name='x_batch')
self.y=tf.placeholder('float',shape=[None,1],name='y_batch')
# self.sess=tf.Session()

def creat_dataset(self,size, n_dim=2, center=0, dis=2, scale=1, one_hot=False):
center1 = (np.random.random(n_dim) + center - 0.5) * scale + dis
center2 = (np.random.random(n_dim) + center - 0.5) * scale - dis
cluster1 = (np.random.randn(size, n_dim) + center1) * scale
cluster2 = (np.random.randn(size, n_dim) + center2) * scale
x_data = np.vstack((cluster1, cluster2)).astype(np.float32)
y_data = np.array([1] * size + [-1] * size)
indices = np.random.permutation(size * 2)
data, labels = x_data[indices], y_data[indices]
labels=np.reshape(labels,(-1,1))
if not one_hot:
return data, labels
labels = np.array([[0, 1] if label == 1 else [1, 0] for label in labels], dtype=np.int8)
return data, labels

@staticmethod
def get_base(self,_nx, _ny):
_xf = np.linspace(self.x_min, self.x_max, _nx)
_yf = np.linspace(self.y_min, self.y_max, _ny)
n_xf, n_yf = np.meshgrid(_xf, _yf)
return _xf, _yf,np.c_[n_xf.ravel(), n_yf.ravel()]
# def readdata(self):
#
# x_data=np.load('x.npy')
# y1=np.load('y.npy')
# y_data=np.reshape(y1,[200,1])
# return x_data ,y_data


def predict(self,y_data):

correct = tf.equal(self.y_predict_value, y_data)

precision=tf.reduce_mean(tf.cast(correct, tf.float32))

precision_value=self.sess.run(precision)
return precision_value, self.y_predict_value


def shuffle(self,epoch,batch,x_data,y_data):
for i in range(epoch):
shuffle_index=np.random.permutation(y_data.shape[0])
x_data1, y_data1 = x_data[shuffle_index], y_data[shuffle_index]
batch_per_epoch = math.ceil(y_data.shape[0]*2 / batch)
for b in range(batch_per_epoch):
if (b*batch+batch)>y_data.shape[0]:
a,b = b*batch, y_data.shape[0]
else:
a,b = b*batch, b*batch+batch

data, labels = x_data1[a:b,:], y_data1[a:b,:]
yield data, labels


def train(self,epoch,x_data,y_data,x_edata,y_edata):

w = tf.Variable(np.ones([2,1]), dtype=tf.float32, name="w_v")
b = tf.Variable(0., dtype=tf.float32, name="b_v")


y_pred =tf.matmul(self.x,w)+b

cost = tf.nn.l2_loss(w)+tf.reduce_sum(tf.maximum(1-self.y*y_pred,0))
train_step = tf.train.AdamOptimizer(0.01).minimize(cost)

y_predict =tf.sign( y_pred)


init = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init)
shuffle= self.shuffle(epoch,100,x_data,y_data)
for i, (x_batch, y_batch) in enumerate(shuffle):

# index=np.random.permutation(y_data.shape[0])
# x_data1, y_data1 = x_data[index], y_data[index]

sess.run(train_step,feed_dict={self.x:x_batch,self.y:y_batch})

if i%1000==0:
self.y_predict_value,self.w_value,self.b_value,cost_value=sess.run([y_predict,w,b,cost],feed_dict={self.x:x_data,self.y:y_data})
print('step= %d , cost=%f '%(i, cost_value))

y_pre = np.sign(np.matmul(x_edata,self.w_value)+self.b_value)
correct = np.equal(y_pre, y_edata)

precision=np.mean(np.cast[ 'float32'](correct))

# precision_value=sess.run(precision)
print('eval= %d'%precision)


def drawresult(self,x_data):

x_min, x_max = np.min(x_data[:,0]), np.max(x_data[:,0])
y_min, y_max = np.min(x_data[:,1]), np.max(x_data[:,1])
x_padding = max(abs(x_min), abs(x_max)) * FLAGS.padding
y_padding = max(abs(y_min), abs(y_max)) * FLAGS.padding
self.x_min -= x_padding
self.x_max += x_padding
self.y_min -= y_padding
self.y_max += y_padding

# self.x_min, self.y_min = np.minimum.reduce(x_data,axis=0) -2
# self.x_max, self.y_max = np.maximum.reduce(x_data,axis=0) +2

xf, yf , matrix_= self.get_base(self,200, 200)

print(self.w_value,self.b_value)
z=np.sign(np.matmul(matrix_,self.w_value)+self.b_value).reshape((200,200))
plt.pcolormesh(xf, yf, z, cmap=plt.cm.Paired)

ypv = self.y_predict_value
y_0 = np.where(ypv==1)
y_1 = np.where(ypv==-1)
plt.scatter(x_data[y_0,0], x_data[y_0,1], c='g')
plt.scatter(x_data[y_1,0], x_data[y_1,1], c='r')

plt.axis([self.x_min,self.x_max,self.y_min ,self.y_max])
# plt.contour(xf, yf, z)
plt.show()


flags.DEFINE_integer('epoch', 1000, "number of epoch")
flags.DEFINE_float('lr', 0.01, "learning rate")
flags.DEFINE_integer('padding', 0.1, "padding")
flags.DEFINE_integer('batch', 100, "batch size")
FLAGS = flags.FLAGS

svm=SVM()
x_data,y_data=svm.creat_dataset(size=100, n_dim=2, center=0, dis=4, one_hot=False)
x_edata,y_edata=svm.creat_dataset(size=100, n_dim=2, center=0, dis=4, one_hot=False)

svm.train(FLAGS.epoch,x_data,y_data,x_edata,y_edata)
#precision_value,y_predict_value=svm.predict(y_data)

#print(precision_value)

svm.drawresult(x_data)
————————————————
版权声明:本文为CSDN博主「ei1994」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ei1990/java/article/details/76849994

 

 

 

用tensorflow实现svm的线性和非线性分类

线性分割:

  1.  
    # coding: utf-8
  2.  
     
  3.  
    # In[1]:
  4.  
     
  5.  
     
  6.  
    import matplotlib.pyplot as plt
  7.  
    import numpy as np
  8.  
    from sklearn import datasets
  9.  
    import tensorflow as tf
  10.  
     
  11.  
     
  12.  
    # In[2]:
  13.  
     
  14.  
     
  15.  
    iris = datasets.load_iris()
  16.  
    x_vals = np.array([[x[0],x[3]] for x in iris.data])
  17.  
    y_vals = np.array([1 if y == 0 else -1 for y in iris.target])
  18.  
     
  19.  
     
  20.  
    # In[3]:
  21.  
     
  22.  
     
  23.  
    x_vals[:5],y_vals[:5]
  24.  
     
  25.  
     
  26.  
    # In[4]:
  27.  
     
  28.  
     
  29.  
    from sklearn import model_selection
  30.  
    train_data,test_data,train_target,test_target = model_selection.train_test_split(x_vals,y_vals,test_size=0.2)
  31.  
     
  32.  
     
  33.  
    # In[5]:
  34.  
     
  35.  
     
  36.  
    train_data.shape,test_data.shape
  37.  
     
  38.  
     
  39.  
    # In[6]:
  40.  
     
  41.  
     
  42.  
    batch_size = 100
  43.  
    x_data = tf.placeholder(shape=[None,2],dtype=tf.float32)
  44.  
    y_target = tf.placeholder(shape=[None,1],dtype=tf.float32)
  45.  
    A = tf.Variable(tf.random_normal(shape=[2,1]))
  46.  
    b = tf.Variable(tf.random_normal(shape=[1,1]))
  47.  
     
  48.  
     
  49.  
    # In[7]:
  50.  
     
  51.  
     
  52.  
    model_output = tf.subtract(tf.matmul(x_data,A),b)
  53.  
     
  54.  
     
  55.  
    # In[8]:
  56.  
     
  57.  
     
  58.  
    l2_norm = tf.reduce_sum(tf.square(A))
  59.  
    alpha = tf.constant([0.1])
  60.  
    classification_term=tf.reduce_mean(tf.maximum(0.,tf.subtract(1.,tf.multiply(model_output,y_target))))
  61.  
    loss = tf.add(classification_term,tf.multiply(alpha,l2_norm))
  62.  
     
  63.  
     
  64.  
    # In[9]:
  65.  
     
  66.  
     
  67.  
    prediction = tf.sign(model_output)
  68.  
    accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction,y_target),tf.float32))
  69.  
     
  70.  
     
  71.  
    # In[10]:
  72.  
     
  73.  
     
  74.  
    my_opt = tf.train.GradientDescentOptimizer(0.01)
  75.  
    train_step = my_opt.minimize(loss)
  76.  
    init = tf.global_variables_initializer()
  77.  
    sess = tf.Session()
  78.  
    sess.run(init)
  79.  
     
  80.  
     
  81.  
    # In[11]:
  82.  
     
  83.  
     
  84.  
    loss_vec = []
  85.  
    train_accuracy = []
  86.  
    test_accuracy = []
  87.  
    for i in range(500):
  88.  
    rand_index = np.random.choice(len(train_data),size = batch_size)
  89.  
    rand_x = train_data[rand_index]
  90.  
    rand_y = np.transpose([train_target[rand_index]])
  91.  
    sess.run(train_step,feed_dict={x_data:rand_x,y_target:rand_y})
  92.  
    temp_loss = sess.run(loss,feed_dict={x_data:rand_x,y_target:rand_y})
  93.  
    loss_vec.append(temp_loss)
  94.  
    train_acc_temp = sess.run(accuracy,feed_dict={x_data:train_data,y_target:np.transpose([train_target])})
  95.  
    train_accuracy.append(train_acc_temp)
  96.  
    test_acc_temp = sess.run(accuracy,feed_dict={x_data:test_data,y_target:np.transpose([test_target])})
  97.  
    test_accuracy.append(test_acc_temp)
  98.  
    if (i+1)% 100 ==0:
  99.  
    print('step # '+str(i+1)+'A='+str(sess.run(A))+'b='+str(sess.run(b)))
  100.  
    print('Loss = '+str(temp_loss))
  101.  
     
  102.  
     
  103.  
    # In[12]:
  104.  
     
  105.  
     
  106.  
    [[a1],[a2]]= sess.run(A)
  107.  
    [[b]]=sess.run(b)
  108.  
    slope=-a2/a1
  109.  
    y_intercept=b/a1
  110.  
    x1_vals = [d[1] for d in x_vals]
  111.  
    best_fit = []
  112.  
    for i in x1_vals:
  113.  
    best_fit.append(slope*i+y_intercept)
  114.  
    setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==1]
  115.  
    setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==1]
  116.  
    not_setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==-1]
  117.  
    not_setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==-1]
  118.  
    best_fit
  119.  
     
  120.  
     
  121.  
    # In[13]:
  122.  
     
  123.  
     
  124.  
    plt.plot(setosa_x,setosa_y,'o',label='I setosa')
  125.  
    plt.plot(not_setosa_x,not_setosa_y,'x',label='Non-setosa')
  126.  
    plt.plot(x1_vals,best_fit,'r-',label='Linear separator',linewidth=3)
  127.  
    plt.ylim([0,10])
  128.  
    plt.legend(loc='lower right')
  129.  
    plt.title('sepal length vs edal width')
  130.  
    plt.xlabel('pedal width')
  131.  
    plt.ylabel('sepal length')
  132.  
    plt.show()
  133.  
     
  134.  
    plt.plot(train_accuracy,'k-',label='Training Accuracy')
  135.  
    plt.plot(test_accuracy,'r--',label='Test Accuracy')
  136.  
    plt.title('Train and Test Set Accuracy')
  137.  
    plt.xlabel('Generation')
  138.  
    plt.ylabel('Accuracy')
  139.  
    plt.legend(loc='lower right')
  140.  
    plt.show()
  141.  
     
  142.  
    plt.plot(loss_vec,'k--')
  143.  
    plt.title('Loss per Generation')
  144.  
    plt.xlabel('Generation')
  145.  
    plt.ylabel('Loss')
  146.  
    plt.show()
  147.  
     

非线性分割:

  1.  
    # coding: utf-8
  2.  
     
  3.  
    # In[1]:
  4.  
     
  5.  
     
  6.  
    import matplotlib.pyplot as plt
  7.  
    import numpy as np
  8.  
    import tensorflow as tf
  9.  
    from sklearn import datasets
  10.  
     
  11.  
     
  12.  
    # In[11]:
  13.  
     
  14.  
     
  15.  
    sess = tf.Session()
  16.  
    x_vals,y_vals = datasets.make_circles(n_samples = 1000,factor=0.5,noise=0.1)
  17.  
    y_vals = np.array([1 if y==1 else -1 for y in y_vals])
  18.  
    class1_x = [x[0] for i,x in enumerate(x_vals) if y_vals[i]==1]
  19.  
    class1_y = [x[1] for i,x in enumerate(x_vals) if y_vals[i]==1]
  20.  
    class2_x = [x[0] for i,x in enumerate(x_vals) if y_vals[i]==-1]
  21.  
    class2_y = [x[1] for i,x in enumerate(x_vals) if y_vals[i]==-1]
  22.  
     
  23.  
     
  24.  
    # In[12]:
  25.  
     
  26.  
     
  27.  
    batch_size = 250
  28.  
    x_data = tf.placeholder(shape=[None,2],dtype=tf.float32)
  29.  
    y_target = tf.placeholder(shape=[None,1],dtype=tf.float32)
  30.  
    prediction_grid = tf.placeholder(shape=[None,2],dtype=tf.float32)
  31.  
    b = tf.Variable(tf.random_normal(shape=[1,batch_size]))
  32.  
     
  33.  
     
  34.  
    # In[13]:
  35.  
     
  36.  
     
  37.  
    gamma = tf.constant(-50.0)
  38.  
    dist = tf.reduce_sum(tf.square(x_data),1)
  39.  
    dist = tf.reshape(dist,[-1,1])
  40.  
    sq_dists = tf.add(tf.subtract(dist,tf.multiply(2.,tf.matmul(x_data,tf.transpose(x_data)))),tf.transpose(dist))
  41.  
    my_kernel = tf.exp(tf.multiply(gamma,tf.abs(sq_dists)))
  42.  
     
  43.  
     
  44.  
    # In[14]:
  45.  
     
  46.  
     
  47.  
    model_output = tf.matmul(b,my_kernel)
  48.  
    first_term = tf.reduce_sum(b)
  49.  
    b_vec_cross = tf.matmul(tf.transpose(b),b)
  50.  
    y_target_cross = tf.matmul(y_target,tf.transpose(y_target))
  51.  
    second_term = tf.reduce_sum(tf.multiply(my_kernel,tf.multiply(b_vec_cross,y_target_cross)))
  52.  
    loss = tf.negative(tf.subtract(first_term,second_term))
  53.  
     
  54.  
     
  55.  
    # In[15]:
  56.  
     
  57.  
     
  58.  
    rA = tf.reshape(tf.reduce_sum(tf.square(x_data),1),[-1,1])
  59.  
    rB= tf.reshape(tf.reduce_sum(tf.square(prediction_grid),1),[-1,1])
  60.  
    pred_sq_dist = tf.add(tf.subtract(rA,tf.multiply(2.,tf.matmul(x_data,tf.transpose(prediction_grid)))),tf.transpose(rB))
  61.  
    pred_kernel = tf.exp(tf.multiply(gamma,tf.abs(pred_sq_dist)))
  62.  
    prediction_output = tf.matmul(tf.multiply(tf.transpose(y_target),b),pred_kernel)
  63.  
    prediction = tf.sign(prediction_output- tf.reduce_mean(prediction_output))
  64.  
    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.squeeze(prediction),tf.squeeze(y_target)),tf.float32))
  65.  
     
  66.  
     
  67.  
    # In[16]:
  68.  
     
  69.  
     
  70.  
    my_opt = tf.train.GradientDescentOptimizer(0.001)
  71.  
    train_step = my_opt.minimize(loss)
  72.  
    init = tf.global_variables_initializer()
  73.  
    sess.run(init)
  74.  
     
  75.  
     
  76.  
    # In[17]:
  77.  
     
  78.  
     
  79.  
    loss_vec = []
  80.  
    batch_accuracy = []
  81.  
    for i in range(500):
  82.  
    rand_index = np.random.choice(len(x_vals),size=batch_size)
  83.  
    rand_x = x_vals[rand_index]
  84.  
    rand_y = np.transpose([y_vals[rand_index]])
  85.  
    sess.run(train_step,feed_dict={x_data:rand_x,y_target:rand_y})
  86.  
    temp_loss = sess.run(loss,feed_dict={x_data:rand_x,y_target:rand_y})
  87.  
    loss_vec.append(temp_loss)
  88.  
    acc_temp = sess.run(accuracy,feed_dict={x_data:rand_x,y_target:rand_y,prediction_grid:rand_x})
  89.  
    batch_accuracy.append(acc_temp)
  90.  
    if (i+1)%100==0:
  91.  
    print('Step #'+str(i+1))
  92.  
    print('Loss = '+str(temp_loss))
  93.  
     
  94.  
     
  95.  
    # In[18]:
  96.  
     
  97.  
     
  98.  
    x_min,x_max = x_vals[:,0].min()-1,x_vals[:,0].max()+1
  99.  
    y_min,y_max = x_vals[:,1].min()-1,x_vals[:,1].max()+1
  100.  
    xx,yy = np.meshgrid(np.arange(x_min,x_max,0.02),np.arange(y_min,y_max,0.02))
  101.  
    grid_points = np.c_[xx.ravel(),yy.ravel()]
  102.  
    [grid_predictions] = sess.run(prediction,feed_dict={x_data:rand_x,y_target:rand_y,prediction_grid:grid_points})
  103.  
    grid_predictions = grid_predictions.reshape(xx.shape)
  104.  
     
  105.  
     
  106.  
    # In[19]:
  107.  
     
  108.  
     
  109.  
    plt.contourf(xx,yy,grid_predictions,cmap = plt.cm.Paired,alpha=0.8)
  110.  
    plt.plot(class1_x,class1_y,'ro',label='Class 1')
  111.  
    plt.plot(class2_x,class2_y,'kx',label='Class -1')
  112.  
    plt.legend(loc='lower right')
  113.  
    plt.ylim([-1.5,1.5])
  114.  
    plt.xlim([-1.5,1.5])
  115.  
    plt.show()
  116.  
     
  117.  
    plt.plot(batch_accuracy,'k-',label='Accuracy')
  118.  
    plt.title('Batch Accuracy')
  119.  
    plt.xlabel('Generation')
  120.  
    plt.ylabel('Accuracy')
  121.  
    plt.legend(loc='lower right')
  122.  
    plt.show()
  123.  
     
  124.  
    plt.plot(loss_vec,'k-')
  125.  
    plt.title('Loss per Generation')
  126.  
    plt.xlabel('Generation')
  127.  
    plt.ylabel('Loss')
  128.  
    plt.show()
  129.  
     
  130.  

posted on 2020-06-30 17:32  曹明  阅读(501)  评论(0)    收藏  举报