利用矩阵,梯度下降算法实现的因子分解机
import numpy as np
LEARNING_RATE = 0.95
REGULARIZATION_COEFFICIENT = 0.000001
STEP_THRESHOLD = 300
K = 6
def sigmoid(x):
return 1/(1+np.exp(-x))
class factorizationMachine:
#x: a sample in train_set
#cross_v: it is a two-dimensional numpy, whose shape is (k,n).And n is sample's feature number.
def __init__(self, w0, linear_w, cross_v):
self.w0 = w0
self.linear_w = linear_w
self.cross_v = cross_v
#def pi_map(self, i):
def inference(self, x):
return self.w0 + self.linear_w.dot(x) + np.sum((self.cross_v.dot(x))**2) - np.sum((self.cross_v**2).dot(x**2))
def loss(self, y, x):
# y_ is predicted value, y is real value
y_ = self.inference(x)
#L2 regularization