高斯核函数SVM TensorFlow实现
来源:互联网 发布:淘宝家具大品牌有哪些 编辑:程序博客网 时间:2024/06/08 06:56
import matplotlib.pyplot as pltimport numpy as npimport tensorflow as tffrom sklearn import datasetsfrom tensorflow.python.framework import ops(x_vals, y_vals) = datasets.make_circles(n_samples=350, factor=.5, noise=.1)y_vals = np.array([1 if y==1 else -1 for y in y_vals])#plot pointsclass1_x = [x[0] for i,x in enumerate(x_vals) if y_vals[i]==1]class1_y = [x[1] for i,x in enumerate(x_vals) if y_vals[i]==1]class2_x = [x[0] for i,x in enumerate(x_vals) if y_vals[i]==-1]class2_y = [x[1] for i,x in enumerate(x_vals) if y_vals[i]==-1]%matplotlib inlineplt.plot(class1_x, class1_y, 'ro', label='Class 1')plt.plot(class2_x, class2_y, 'kx', label='Class -1')plt.title('distrubtion')plt.xlabel('x')plt.ylabel('y')plt.ylim([-1.5, 1.5])plt.xlim([-1.5, 1.5])plt.show()#data processingx = np.transpose(x_vals) #shape=(features,examples)y = y_vals.reshape((1,-1)) #shape=(targets,examples)# 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_train = x[:,train_indices]x_test = x[:,test_indices]y_train = y[:,train_indices]y_test = y[:,test_indices]def model(x_train,y_train,x_test,y_test,learning_rate=0.01): ops.reset_default_graph() X_place = tf.placeholder(tf.float32,[2,None]) Y_place = tf.placeholder(tf.float32,[1,None]) w = tf.Variable(tf.random_normal(shape=[1,x_train.shape[1]])) b = tf.Variable(tf.random_normal(shape=[1,1])) #compute Gaussian Kernel gamma = tf.constant(-10.0) dist = tf.reduce_sum(tf.square(tf.transpose(X_place)), 1) dist = tf.reshape(dist, [-1,1]) sq_dists = tf.add(tf.subtract(dist, tf.multiply(2., tf.matmul(tf.transpose(X_place),X_place))), tf.transpose(dist)) my_kernel = tf.exp(tf.multiply(gamma, sq_dists)) output = tf.add(tf.matmul(w,my_kernel),b) loss = tf.reduce_mean(tf.maximum(0.,tf.subtract(1.,tf.multiply(output,Y_place)))) + tf.matmul(w,tf.transpose(w)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) train_loss = [] for epoch in range(1000): _,train_cost = sess.run([optimizer,loss],feed_dict={X_place:x_train,Y_place:y_train}) train_loss.append(train_cost) if (epoch+1)%50==0: print('epoch'+str(epoch+1)+'cost:'+str(train_cost)) w_new = sess.run(w) b_new = sess.run(b) x_trainfloat = tf.to_float(x_train) x_testfloat = tf.to_float(x_test) dist = tf.reduce_sum(tf.square(tf.transpose(x_trainfloat)), 1) dist = tf.reshape(dist, [-1,1]) sq_dists = tf.add(tf.subtract(dist, tf.multiply(2., tf.matmul(tf.transpose(x_trainfloat),x_trainfloat))), tf.transpose(dist)) my_kernel = tf.exp(tf.multiply(gamma, sq_dists)) predict_train = tf.add(tf.matmul(w_new,my_kernel),b_new) prediction = tf.sign(predict_train) accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, y_train), tf.float32)) train_accuracy = sess.run(accuracy) print('train_accuracy:' + str(train_accuracy)) j = 0. for i in range(x_test.shape[1]): k = np.array([[x_test[0,i]],[x_test[1,i]]]) a = np.add(np.dot(np.exp(np.sum(np.square(x_train - k),0)* (-10)),np.transpose(w_new)),b_new) if a*y_test[0,i]>0: j = j+1. test_accuracy = j/x_test.shape[1] print('test_accuracy:' + str(test_accuracy)) return train_loss train_loss = model(x_train,y_train,x_test,y_test)
阅读全文
0 0
- 高斯核函数SVM TensorFlow实现
- tensorflow实现线性svm
- tensorflow——SVM实现
- tensorflow——SVM实现
- SVM原理以及Tensorflow 实现SVM分类(附代码)
- SVM支持向量机Tensorflow实现
- Tensorflow实现经典损失函数
- 【机器学习】tensorflow: GPU求解带核函数的SVM二分类支持向量机
- SVM实现
- tensorflow中softmax_cross_entropy_with_logits函数的实现
- MATLAB自带的svm实现函数与libsvm差别
- 8.2 TensorFlow实现KNN与TensorFlow中的损失函数,优化函数
- SVM核函数
- svm kernel 函数
- 【模式识别】SVM核函数
- SVM-核函数
- SVM核函数
- SVM常用核函数
- 二叉树遍历算法
- TCP:为什么是三次握手
- POJ3751 时间日期格式转换【日期计算】
- C# 类的单例模式
- NGUI实现背包效果
- 高斯核函数SVM TensorFlow实现
- Python 面向对象编程
- 定义类 和 对象
- JAVA何时使用覆盖(重写)操作
- 阿里云centos7.3下 python3.6 使用 virtualenv 创建独立的虚拟环境
- 关于使用Phalcon [PHQL]:Model could not be loaded
- 乘法口诀表
- 自定义封装Jquery ajax方法
- 继承 和 多继承