麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

Tensorflow使用支持向量機擬合線性回歸

2020-02-15 22:54:30
字體:
來源:轉載
供稿:網友

支持向量機可以用來擬合線性回歸。

相同的最大間隔(maximum margin)的概念應用到線性回歸擬合。代替最大化分割兩類目標是,最大化分割包含大部分的數據點(x,y)。我們將用相同的iris數據集,展示用剛才的概念來進行花萼長度與花瓣寬度之間的線性擬合。

相關的損失函數類似于max(0,|yi-(Axi+b)|-ε)。ε這里,是間隔寬度的一半,這意味著如果一個數據點在該區域,則損失等于0。

# SVM Regression#----------------------------------## This function shows how to use TensorFlow to# solve support vector regression. We are going# to find the line that has the maximum margin# which INCLUDES as many points as possible## We will use the iris data, specifically:# y = Sepal Length# x = Pedal Widthimport matplotlib.pyplot as pltimport numpy as npimport tensorflow as tffrom sklearn import datasetsfrom tensorflow.python.framework import opsops.reset_default_graph()# Create graphsess = tf.Session()# Load the data# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]iris = datasets.load_iris()x_vals = np.array([x[3] for x in iris.data])y_vals = np.array([y[0] for y in iris.data])# 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_vals_train = x_vals[train_indices]x_vals_test = x_vals[test_indices]y_vals_train = y_vals[train_indices]y_vals_test = y_vals[test_indices]# Declare batch sizebatch_size = 50# Initialize placeholdersx_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)# Create variables for linear regressionA = tf.Variable(tf.random_normal(shape=[1,1]))b = tf.Variable(tf.random_normal(shape=[1,1]))# Declare model operationsmodel_output = tf.add(tf.matmul(x_data, A), b)# Declare loss function# = max(0, abs(target - predicted) + epsilon)# 1/2 margin width parameter = epsilonepsilon = tf.constant([0.5])# Margin term in lossloss = tf.reduce_mean(tf.maximum(0., tf.subtract(tf.abs(tf.subtract(model_output, y_target)), epsilon)))# Declare optimizermy_opt = tf.train.GradientDescentOptimizer(0.075)train_step = my_opt.minimize(loss)# Initialize variablesinit = tf.global_variables_initializer()sess.run(init)# Training looptrain_loss = []test_loss = []for i in range(200):  rand_index = np.random.choice(len(x_vals_train), size=batch_size)  rand_x = np.transpose([x_vals_train[rand_index]])  rand_y = np.transpose([y_vals_train[rand_index]])  sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})  temp_train_loss = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_train]), y_target: np.transpose([y_vals_train])})  train_loss.append(temp_train_loss)  temp_test_loss = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_test]), y_target: np.transpose([y_vals_test])})  test_loss.append(temp_test_loss)  if (i+1)%50==0:    print('-----------')    print('Generation: ' + str(i+1))    print('A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))    print('Train Loss = ' + str(temp_train_loss))    print('Test Loss = ' + str(temp_test_loss))# Extract Coefficients[[slope]] = sess.run(A)[[y_intercept]] = sess.run(b)[width] = sess.run(epsilon)# Get best fit linebest_fit = []best_fit_upper = []best_fit_lower = []for i in x_vals: best_fit.append(slope*i+y_intercept) best_fit_upper.append(slope*i+y_intercept+width) best_fit_lower.append(slope*i+y_intercept-width)# Plot fit with dataplt.plot(x_vals, y_vals, 'o', label='Data Points')plt.plot(x_vals, best_fit, 'r-', label='SVM Regression Line', linewidth=3)plt.plot(x_vals, best_fit_upper, 'r--', linewidth=2)plt.plot(x_vals, best_fit_lower, 'r--', linewidth=2)plt.ylim([0, 10])plt.legend(loc='lower right')plt.title('Sepal Length vs Pedal Width')plt.xlabel('Pedal Width')plt.ylabel('Sepal Length')plt.show()# Plot loss over timeplt.plot(train_loss, 'k-', label='Train Set Loss')plt.plot(test_loss, 'r--', label='Test Set Loss')plt.title('L2 Loss per Generation')plt.xlabel('Generation')plt.ylabel('L2 Loss')plt.legend(loc='upper right')plt.show()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色毛片a级 | 欧美成视频在线观看 | 欧美极品欧美精品欧美视频 | 国产老师做www爽爽爽视频 | 亚洲aⅴ免费在线观看 | 免费一级欧美大片视频 | 免费人成年短视频在线观看网站 | 成片免费观看大全 | 欧美精品欧美 | 韩国精品久久久 | 黄色片网站免费看 | 国产精品亚洲精品久久 | 99ri精品 | 国产色片在线观看 | 国产精品视频一区二区三区四 | 国产噜噜噜噜噜久久久久久久久 | 美国一级免费视频 | 黄片毛片一级 | 成人性视频免费网站下载软件 | 中文字幕在线观看日韩 | 一级免费特黄视频 | 亚洲视屏在线观看 | 精品久久久久久中文字幕 | 色av成人天堂桃色av | 国产一区二区久久精品 | 男男羞羞视频网站国产 | 亚洲 91 | 欧美成人一二三区 | 在线区| 毛片午夜| 久久国产精品小视频 | 精品国产一区二区三区四区在线 | 久久久久免费精品国产小说色大师 | 免费观看一级 | 一级做受大片免费视频 | 泰剧19禁啪啪无遮挡大尺度 | 成人福利免费在线观看 | 国内精品久久久久久2021浪潮 | 中文字幕在线第二页 | 亚洲国产视频网 | 中文日韩在线视频 |