欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用TensorFlow實(shí)現(xiàn)SVM

 更新時(shí)間:2018年09月06日 16:42:11   作者:sdoddyjm68  
這篇文章主要為大家詳細(xì)介紹了使用TensorFlow實(shí)現(xiàn)SVM的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

較基礎(chǔ)的SVM,后續(xù)會(huì)加上多分類以及高斯核,供大家參考。

Talk is cheap, show me the code

import tensorflow as tf
from sklearn.base import BaseEstimator, ClassifierMixin
import numpy as np

class TFSVM(BaseEstimator, ClassifierMixin):

 def __init__(self, 
  C = 1, kernel = 'linear', 
  learning_rate = 0.01, 
  training_epoch = 1000, 
  display_step = 50,
  batch_size = 50,
  random_state = 42):
  #參數(shù)列表
  self.svmC = C
  self.kernel = kernel
  self.learning_rate = learning_rate
  self.training_epoch = training_epoch
  self.display_step = display_step
  self.random_state = random_state
  self.batch_size = batch_size

 def reset_seed(self):
  #重置隨機(jī)數(shù)
  tf.set_random_seed(self.random_state)
  np.random.seed(self.random_state)

 def random_batch(self, X, y):
  #調(diào)用隨機(jī)子集,實(shí)現(xiàn)mini-batch gradient descent
  indices = np.random.randint(1, X.shape[0], self.batch_size)
  X_batch = X[indices]
  y_batch = y[indices]
  return X_batch, y_batch

 def _build_graph(self, X_train, y_train):
  #創(chuàng)建計(jì)算圖
  self.reset_seed()

  n_instances, n_inputs = X_train.shape

  X = tf.placeholder(tf.float32, [None, n_inputs], name = 'X')
  y = tf.placeholder(tf.float32, [None, 1], name = 'y')

  with tf.name_scope('trainable_variables'):
   #決策邊界的兩個(gè)變量
   W = tf.Variable(tf.truncated_normal(shape = [n_inputs, 1], stddev = 0.1), name = 'weights')
   b = tf.Variable(tf.truncated_normal([1]), name = 'bias')

  with tf.name_scope('training'):
   #算法核心
   y_raw = tf.add(tf.matmul(X, W), b)
   l2_norm = tf.reduce_sum(tf.square(W))
   hinge_loss = tf.reduce_mean(tf.maximum(tf.zeros(self.batch_size, 1), tf.subtract(1., tf.multiply(y_raw, y))))
   svm_loss = tf.add(hinge_loss, tf.multiply(self.svmC, l2_norm))
   training_op = tf.train.AdamOptimizer(learning_rate = self.learning_rate).minimize(svm_loss)

  with tf.name_scope('eval'):
   #正確率和預(yù)測(cè)
   prediction_class = tf.sign(y_raw)
   correct_prediction = tf.equal(y, prediction_class)
   accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

  init = tf.global_variables_initializer()

  self._X = X; self._y = y
  self._loss = svm_loss; self._training_op = training_op
  self._accuracy = accuracy; self.init = init
  self._prediction_class = prediction_class
  self._W = W; self._b = b

 def _get_model_params(self):
  #獲取模型的參數(shù),以便存儲(chǔ)
  with self._graph.as_default():
   gvars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
  return {gvar.op.name: value for gvar, value in zip(gvars, self._session.run(gvars))}

 def _restore_model_params(self, model_params):
  #保存模型的參數(shù)
  gvar_names = list(model_params.keys())
  assign_ops = {gvar_name: self._graph.get_operation_by_name(gvar_name + '/Assign') for gvar_name in gvar_names}
  init_values = {gvar_name: assign_op.inputs[1] for gvar_name, assign_op in assign_ops.items()}
  feed_dict = {init_values[gvar_name]: model_params[gvar_name] for gvar_name in gvar_names}
  self._session.run(assign_ops, feed_dict = feed_dict)

 def fit(self, X, y, X_val = None, y_val = None):
  #fit函數(shù),注意要輸入驗(yàn)證集
  n_batches = X.shape[0] // self.batch_size

  self._graph = tf.Graph()
  with self._graph.as_default():
   self._build_graph(X, y)

  best_loss = np.infty
  best_accuracy = 0
  best_params = None
  checks_without_progress = 0
  max_checks_without_progress = 20

  self._session = tf.Session(graph = self._graph)

  with self._session.as_default() as sess:
   self.init.run()

   for epoch in range(self.training_epoch):
    for batch_index in range(n_batches):
     X_batch, y_batch = self.random_batch(X, y)
     sess.run(self._training_op, feed_dict = {self._X:X_batch, self._y:y_batch})
    loss_val, accuracy_val = sess.run([self._loss, self._accuracy], feed_dict = {self._X: X_val, self._y: y_val})
    accuracy_train = self._accuracy.eval(feed_dict = {self._X: X_batch, self._y: y_batch})

    if loss_val < best_loss:
     best_loss = loss_val
     best_params = self._get_model_params()
     checks_without_progress = 0
    else:
     checks_without_progress += 1
     if checks_without_progress > max_checks_without_progress:
      break

    if accuracy_val > best_accuracy:
     best_accuracy = accuracy_val
     #best_params = self._get_model_params()

    if epoch % self.display_step == 0:
     print('Epoch: {}\tValidaiton loss: {:.6f}\tValidation Accuracy: {:.4f}\tTraining Accuracy: {:.4f}'
      .format(epoch, loss_val, accuracy_val, accuracy_train))
   print('Best Accuracy: {:.4f}\tBest Loss: {:.6f}'.format(best_accuracy, best_loss))
   if best_params:
    self._restore_model_params(best_params)
    self._intercept = best_params['trainable_variables/weights']
    self._bias = best_params['trainable_variables/bias']
   return self

 def predict(self, X):
  with self._session.as_default() as sess:
   return self._prediction_class.eval(feed_dict = {self._X: X})

 def _intercept(self):
  return self._intercept

 def _bias(self):
  return self._bias

實(shí)際運(yùn)行效果如下(以Iris數(shù)據(jù)集為樣本):

這里寫(xiě)圖片描述 

畫(huà)出決策邊界來(lái)看看:

這里寫(xiě)圖片描述 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Scrapy中如何向Spider傳入?yún)?shù)的方法實(shí)現(xiàn)

    Scrapy中如何向Spider傳入?yún)?shù)的方法實(shí)現(xiàn)

    這篇文章主要介紹了Scrapy中如何向Spider傳入?yún)?shù)的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python模擬登陸阿里媽媽生成商品推廣鏈接

    python模擬登陸阿里媽媽生成商品推廣鏈接

    這篇文章主要介紹了python模擬登陸阿里媽媽生成商品推廣鏈接,需要的朋友可以參考下
    2014-04-04
  • Python如何通過(guò)地址獲取變量

    Python如何通過(guò)地址獲取變量

    這篇文章主要介紹了Python如何通過(guò)地址獲取變量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 音頻和視頻合并自動(dòng)裁剪

    python 音頻和視頻合并自動(dòng)裁剪

    本文主要介紹了python 音頻和視頻合并自動(dòng)裁剪,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • python多進(jìn)程共享Array問(wèn)題

    python多進(jìn)程共享Array問(wèn)題

    multiprocessing庫(kù)提供了Array類,允許在多個(gè)進(jìn)程間共享數(shù)組,Array在共享內(nèi)存中創(chuàng)建,各進(jìn)程可直接訪問(wèn)和修改其元素,實(shí)現(xiàn)數(shù)據(jù)同步,Array支持多種數(shù)據(jù)類型,可選鎖定參數(shù)以保證數(shù)據(jù)安全
    2024-09-09
  • Python實(shí)現(xiàn)修改文件內(nèi)容的方法分析

    Python實(shí)現(xiàn)修改文件內(nèi)容的方法分析

    這篇文章主要介紹了Python實(shí)現(xiàn)修改文件內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python文件讀寫(xiě)、字符串替換及shell方法調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Python機(jī)器學(xué)習(xí)之決策樹(shù)算法實(shí)例詳解

    Python機(jī)器學(xué)習(xí)之決策樹(shù)算法實(shí)例詳解

    這篇文章主要介紹了Python機(jī)器學(xué)習(xí)之決策樹(shù)算法,較為詳細(xì)的分析了實(shí)例詳解機(jī)器學(xué)習(xí)中決策樹(shù)算法的概念、原理及相關(guān)Python實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-12-12
  • Python實(shí)現(xiàn)視頻剪輯的示例詳解

    Python實(shí)現(xiàn)視頻剪輯的示例詳解

    這篇文章主要為大家詳細(xì)介紹了如何Python實(shí)現(xiàn)視頻剪輯的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • Python中斷點(diǎn)調(diào)試pdb包的用法詳解

    Python中斷點(diǎn)調(diào)試pdb包的用法詳解

    pdb(python debugger) 是 python 中的一個(gè)命令行調(diào)試包,為 python 程序提供了一種交互的源代碼調(diào)試功能,下面就跟隨小編一起學(xué)習(xí)一下它的具體使用吧
    2024-01-01
  • 如何利用Fabric自動(dòng)化你的任務(wù)

    如何利用Fabric自動(dòng)化你的任務(wù)

    大家都知道Fabric是一個(gè)Python庫(kù),可以通過(guò)SSH在多個(gè)host上批量執(zhí)行任務(wù)。你可以編寫(xiě)任務(wù)腳本,然后通過(guò)Fabric在本地就可以使用SSH在大量遠(yuǎn)程服務(wù)器上自動(dòng)運(yùn)行。這些功能非常適合應(yīng)用的自動(dòng)化部署,或者執(zhí)行系統(tǒng)管理任務(wù)。本文將介紹如何利用Fabric自動(dòng)化你的任務(wù)。
    2016-10-10

最新評(píng)論