2014-01-09 31 views
1

本文实现了一种图像分类算法,它采用支持向量机与直方图相交核和随机梯度下降法。我到目前为止的代码:支持向量机与直方图相交核使用梯度下降

import numpy as np 

# calculate the kernel matrix for x and examples 
# kernel is (n_samples x 1) matrix 
# n_samples number of the examples 
def histogram_intersection_kernel(x,u): 
    n_samples , n_features = x.shape 
    K = np.zeros(shape=(n_samples,1),dtype=np.float) 
    for d in xrange(n_samples): 
     K[d][0] = np.sum(np.minimum(x[d],u),axis =1) 
    return K 

# related to the hinge loss 
# returns 1 if y*f_{t-1}(xt) < 1 
#   0 otherwise 
def get_sigma(self,y,y_prediction) : 
    if np.dot(y,y_prediction) <1 : 
     return 1 
    else : 
     return 0 

# alpha is a predicted matrix for f = alpha *K(x,.) 
# returns the predicted value y' for given x 
def get_prediction(self,X,alpha,n_samples,u): 
    y_predict = np.dot(alpha.T,self.get_kernel(X,u)) 
    return np.sign(y_predict.item(0)) 

# calculate the update for gradient descent function 
# alpha is the parameter of the equation that i try to find 
# eta is the learning rate 
# (xt,yt) is the t^th example 
# update rule : f = (1-lambda*eta)*f_{t-1} + eta*sigma*yt*K(xt,.) 
def update_rule(self,alpha,eta,lmbda,y,X,n_samples,xt,yt): 
    param1 = (1-lmbda * eta)*alpha 
    y_prediction = self.get_prediction(X,alpha,n_samples,xt) 
    kernel_value = self.get_kernel(X,xt) 
    param2 = eta * self.get_sigma(yt,y_prediction)*yt.item(0)*kernel_value 
    return param1 + param2 

# go through all of the examples 
# try to find minimum 
def gradient_descent(self,n_samples,X,y,alpha,eta,lmbda): 
    for i in xrange(n_samples): 
     alpha = self.update_rule(alpha,eta,lmbda,y,X,n_samples,X[i],y[i]) 
    return alpha 

此程序不适用于我的数据。看来我错过了一些东西。我想知道这个基本想法是否属实。

  1. 这就像是一个梯度下降的实现。为了找到支持向量,我应该计算向量,其中0为预测函数?
  2. 我应该如何更新eta?

谢谢。

回答

0
  1. Alpha是对原始示例的权重向量。支持向量是具有正权重的那些。
  2. 您可以在梯度下降过程中绘制成本函数曲线。观察曲线趋势,选择使曲线收敛的学习率eta
+0

Alpha是(features_number×1)的矩阵是吗? – yossarianlives

+1

@yossarianlives alpha是一个具有数据集编号长度(xi,n数)的向量 – lennon310

+0

ok然后在代码中使用alpha是真实的,但是我仍然不明白为什么我的代码没有给出好的分类 – yossarianlives