使用tensorflow实现线性svm
2019/7/15 0:21:05
本文主要是介绍使用tensorflow实现线性svm,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了tensorflow实现线性svm的具体代码,供大家参考,具体内容如下
简单方法:
import tensorflow as tf import numpy as np from matplotlib import pyplot as plt def placeholder_input(): x=tf.placeholder('float',shape=[None,2],name='x_batch') y=tf.placeholder('float',shape=[None,1],name='y_batch') return x,y def get_base(_nx, _ny): _xf = np.linspace(x_min, x_max, _nx) _yf = np.linspace(y_min, y_max, _ny) xf1, yf1 = np.meshgrid(_xf, _yf) n_xf,n_yf=np.hstack((xf1)),np.hstack((yf1)) return _xf, _yf,np.c_[n_xf.ravel(), n_yf.ravel()] x_data=np.load('x.npy') y1=np.load('y.npy') y_data=np.reshape(y1,[200,1]) step=10000 tol=1e-3 x,y=placeholder_input() 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(x,w)+b y_predict =tf.sign( tf.matmul(x,w)+b ) # cost = ∑_(i=1)^N max(1-y_i⋅(w⋅x_i+b),0)+1/2 + 0.5 * ‖w‖^2 cost = tf.nn.l2_loss(w)+tf.reduce_sum(tf.maximum(1-y*y_pred,0)) train_step = tf.train.AdamOptimizer(0.01).minimize(cost) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(step): sess.run(train_step,feed_dict={x:x_data,y:y_data}) y_p,y_p1,loss,w_value,b_value=sess.run([y_predict,y_pred,cost,w,b],feed_dict={x:x_data,y:y_data}) x_min, y_min = np.minimum.reduce(x_data,axis=0) -2 x_max, y_max = np.maximum.reduce(x_data,axis=0) +2 xf, yf , matrix_= get_base(200, 200) #xy_xf, xy_yf = np.meshgrid(xf, yf, sparse=True) z=np.sign(np.matmul(matrix_,w_value)+b_value).reshape((200,200)) plt.pcolormesh(xf, yf, z, cmap=plt.cm.Paired) for i in range(200): if y_p[i,0]==1.0: plt.scatter(x_data[i,0],x_data[i,1],color='r') else: plt.scatter(x_data[i,0],x_data[i,1],color='g') plt.axis([x_min,x_max,y_min ,y_max]) #plt.contour(xf, yf, z) plt.show()
进阶:
import tensorflow as tf import numpy as np from matplotlib import pyplot as plt 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() @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 train(self,step,x_data,y_data): w = tf.Variable(np.ones([2,1]), dtype=tf.float32, name="w_v") b = tf.Variable(0., dtype=tf.float32, name="b_v") self.y_pred =tf.matmul(self.x,w)+b cost = tf.nn.l2_loss(w)+tf.reduce_sum(tf.maximum(1-self.y*self.y_pred,0)) train_step = tf.train.AdamOptimizer(0.01).minimize(cost) self.y_predict =tf.sign( tf.matmul(self.x,w)+b ) self.sess.run(tf.global_variables_initializer()) for i in range(step): self.sess.run(train_step,feed_dict={self.x:x_data,self.y:y_data}) self.y_predict_value,self.w_value,self.b_value,cost_value=self.sess.run([self.y_predict,w,b,cost],feed_dict={self.x:x_data,self.y:y_data}) print('**********cost=%f***********'%cost_value) 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 def drawresult(self,x_data): 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) w_value=self.w_value b_value=self.b_value print(w_value,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) for i in range(200): if self.y_predict_value[i,0]==1.0: plt.scatter(x_data[i,0],x_data[i,1],color='r') else: plt.scatter(x_data[i,0],x_data[i,1],color='g') plt.axis([self.x_min,self.x_max,self.y_min ,self.y_max]) #plt.contour(xf, yf, z) plt.show() svm=SVM() x_data,y_data=svm.readdata() svm.train(5000,x_data,y_data) precision_value=svm.predict(y_data) svm.drawresult(x_data)
没有数据的可以用这个
import tensorflow as tf import numpy as np from matplotlib import pyplot as plt 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) x_data, y_data = x_data[indices], y_data[indices] y_data=np.reshape(y_data,(y_data.shape[0],1)) if not one_hot: return x_data, y_data y_data = np.array([[0, 1] if label == 1 else [1, 0] for label in y_data], dtype=np.int8) return x_data, y_data @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 train(self,step,x_data,y_data): w = tf.Variable(np.ones([2,1]), dtype=tf.float32, name="w_v") b = tf.Variable(0., dtype=tf.float32, name="b_v") self.y_pred =tf.matmul(self.x,w)+b cost = tf.nn.l2_loss(w)+tf.reduce_sum(tf.maximum(1-self.y*self.y_pred,0)) train_step = tf.train.AdamOptimizer(0.01).minimize(cost) self.y_predict =tf.sign( tf.matmul(self.x,w)+b ) self.sess.run(tf.global_variables_initializer()) for i in range(step): index=np.random.permutation(y_data.shape[0]) x_data1, y_data1 = x_data[index], y_data[index] self.sess.run(train_step,feed_dict={self.x:x_data1[0:50],self.y:y_data1[0:50]}) self.y_predict_value,self.w_value,self.b_value,cost_value=self.sess.run([self.y_predict,w,b,cost],feed_dict={self.x:x_data,self.y:y_data}) if i%1000==0:print('**********cost=%f***********'%cost_value) 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 drawresult(self,x_data): 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) for i in range(x_data.shape[0]): if self.y_predict_value[i,0]==1.0: plt.scatter(x_data[i,0],x_data[i,1],color='r') else: plt.scatter(x_data[i,0],x_data[i,1],color='g') plt.axis([self.x_min,self.x_max,self.y_min ,self.y_max]) # plt.contour(xf, yf, z) plt.show() svm=SVM() x_data,y_data=svm.creat_dataset(size=200, n_dim=2, center=0, dis=4, one_hot=False) svm.train(5000,x_data,y_data) precision_value,y_predict_value=svm.predict(y_data) svm.drawresult(x_data)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于使用tensorflow实现线性svm的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门