较基础的SVM,后续会加上多分类以及高斯核
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):
#参数列表
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):
#重置随机数
tf.set_random_seed(self.random_state)
np.random.seed(self.random_state)
def random_batch(self, X, y):
#调用随机子集,实现mini-batch gradient descent
indices = np.random.randint(1, X.shape[0], self.batch_size)