tensorflow——SVM实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ei1990/article/details/76849994

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')

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)
阅读更多
换一批

没有更多推荐了,返回首页