FM代码

文章目录

  • pytorch实现
  • 其它方法

pytorch实现

import torch.nn as nn
from scipy.sparse import csr
import pandas as pd
import numpy as np
import torch
from tqdm import tqdm

#FM model
class FM_model(nn.Module):
    def __init__(self,p,k):
        super(FM_model,self).__init__()
        self.p = p
        self.k = k
        self.linear = nn.Linear(self.p,1,bias=True)
        self.v = nn.Parameter(torch.randn(self.k,self.p))
    def fm_layer(self,x):
        linear_part = self.linear(x)
        inter_part1 = torch.mm(x,self.v.t())
        inter_part2 = torch.mm(torch.pow(x,2),torch.pow(self.v,2).t())
        output = linear_part + 0.5*torch.sum(torch.pow(inter_part1,2) - inter_part2)
        return output
    def forward(self,x):
        output = self.fm_layer(x)
        return output

def vectorize_dic(dic,ix=None,p=None,n=0,g=0):
    """
    dic -- dictionary of feature lists. Keys are the name of features
    ix -- index generator (default None)
    p -- dimension of feature space (number of columns in the sparse matrix) (default None)
    """
    if ix==None:
        ix = dict()

    nz = n * g

    col_ix = np.empty(nz,dtype = int)

    i = 0
    for k,lis in dic.items():
        for t in range(len(lis)):
            ix[str(lis[t]) + str(k)] = ix.get(str(lis[t]) + str(k),0) + 1
            col_ix[i+t*g] = ix[str(lis[t]) + str(k)]
        i += 1

    row_ix = np.repeat(np.arange(0,n),g)
    data = np.ones(nz)
    if p == None:
        p = len(ix)

    ixx = np.where(col_ix < p)
    return csr.csr_matrix((data[ixx],(row_ix[ixx],col_ix[ixx])),shape=(n,p)),ix


def batcher(X_, y_=None, batch_size=-1):
    n_samples = X_.shape[0]

    if batch_size == -1:
        batch_size = n_samples
    if batch_size < 1:
        raise ValueError('Parameter batch_size={} is unsupported'.format(batch_size))

    for i in range(0, n_samples, batch_size):
        upper_bound = min(i + batch_size, n_samples)
        ret_x = X_[i:upper_bound]
        ret_y = None
        if y_ is not None:
            ret_y = y_[i:i + batch_size]
            yield (ret_x, ret_y)
if __name__ == "__main__":
    #data proprecess
    cols = ['user','item','rating','timestamp']

    train = pd.read_csv('data/ua.base', delimiter='\t', names = cols)
    test = pd.read_csv('data/ua.test', delimiter='\t', names = cols)

    x_train,ix = vectorize_dic({
     'users':train['user'].values,
                                'items':train['item'].values},n=len(train.index),g=2)


    x_test,ix = vectorize_dic({
     'users':test['user'].values,
                            'items':test['item'].values},ix,x_train.shape[1],n=len(test.index),g=2)


    y_train = train['rating'].values
    y_test = test['rating'].values

    x_train = x_train.todense()
    x_test = x_test.todense()


    print(x_train.shape)
    print(x_test.shape)

    #train
    n,p = x_train.shape
    k = 10
    batch_size=64
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = FM_model(p, k).to(device)
    loss_fn = nn.MSELoss()
    optimer = torch.optim.SGD(model.parameters(), lr=0.0001, weight_decay=0.001)
    epochs = 100
    for epoch in range(epochs):
        loss_epoch = 0.0
        loss_all = 0.0
        perm = np.random.permutation(x_train.shape[0])
        model.train()
        for x,y in tqdm(batcher(x_train[perm], y_train[perm], batch_size)):
            model.zero_grad()
            x = torch.as_tensor(np.array(x.tolist()), dtype=torch.float, device=device)
            y = torch.as_tensor(np.array(y.tolist()), dtype=torch.float, device=device)
            x = x.view(-1, p)
            y = y.view(-1, 1)
            preds = model(x)
            loss = loss_fn(preds,y)
            loss_all += loss.item()
            loss.backward()
            optimer.step()
        loss_epoch = loss_all/len(x)
        print(f"Epoch [{epoch}/{10}], "
                f"Loss: {loss_epoch:.8f} ")
    

其它方法



from __future__ import division
from math import exp
from numpy import *
from random import normalvariate  # 正态分布
from datetime import datetime
import pandas as pd
import numpy as np

trainData = r'./diabetes_train.txt'
testData = r'./diabetes_test.txt'


def preprocessData(data):
    feature = np.array(data.iloc[:, :-1])  # 取特征
    label = data.iloc[:, -1].map(lambda x: 1 if x == 1 else -1)  # 取标签并转化为 +1,-1
    # 将数组按行进行归一化
    zmax, zmin = feature.max(axis=0), feature.min(axis=0)
    feature = (feature - zmin) / (zmax - zmin)
    label = np.array(label)

    return feature, label


def sigmoid(inx):
    # return 1. / (1. + exp(-max(min(inx, 15.), -15.)))
    return 1.0 / (1 + exp(-inx))


def SGD_FM(dataMatrix, classLabels, k, iter):
    '''
    :param dataMatrix:  特征矩阵
    :param classLabels: 类别矩阵
    :param k:           辅助向量的大小
    :param iter:        迭代次数
    :return:
    '''
    # dataMatrix用的是mat, classLabels是列表
    m, n = shape(dataMatrix)  # 矩阵的行列数,即样本数和特征数
    alpha = 0.01
    # 初始化参数
    # w = random.randn(n, 1)#其中n是特征的个数
    w = zeros((n, 1))  # 一阶特征的系数
    w_0 = 0.
    v = normalvariate(0, 0.2) * ones((n, k))  # 即生成辅助向量,用来训练二阶交叉特征的系数

    for it in range(iter):
        for x in range(m):  # 随机优化,每次只使用一个样本
            # 二阶项的计算
            inter_1 = dataMatrix[x] * v
            inter_2 = multiply(dataMatrix[x], dataMatrix[x]) * multiply(v, v)  # 二阶交叉项的计算
            interaction = sum(multiply(inter_1, inter_1) - inter_2) / 2.  # 二阶交叉项计算完成

            p = w_0 + dataMatrix[x] * w + interaction  # 计算预测的输出,即FM的全部项之和
            loss = 1 - sigmoid(classLabels[x] * p[0, 0])  # 计算损失

            w_0 = w_0 + alpha * loss * classLabels[x]

            for i in range(n):
                if dataMatrix[x, i] != 0:
                    w[i, 0] = w[i, 0] + alpha * loss * classLabels[x] * dataMatrix[x, i]
                    for j in range(k):
                        v[i, j] = v[i, j] + alpha * loss * classLabels[x] * (
                                dataMatrix[x, i] * inter_1[0, j] - v[i, j] * dataMatrix[x, i] * dataMatrix[x, i])
        print("第{}次迭代后的损失为{}".format(it, loss))

    return w_0, w, v


def getAccuracy(dataMatrix, classLabels, w_0, w, v):
    m, n = shape(dataMatrix)
    allItem = 0
    error = 0
    result = []
    for x in range(m):  # 计算每一个样本的误差
        allItem += 1
        inter_1 = dataMatrix[x] * v
        inter_2 = multiply(dataMatrix[x], dataMatrix[x]) * multiply(v, v)
        interaction = sum(multiply(inter_1, inter_1) - inter_2) / 2.
        p = w_0 + dataMatrix[x] * w + interaction  # 计算预测的输出

        pre = sigmoid(p[0, 0])
        result.append(pre)

        if pre < 0.5 and classLabels[x] == 1.0:
            error += 1
        elif pre >= 0.5 and classLabels[x] == -1.0:
            error += 1
        else:
            continue

    return float(error) / allItem


if __name__ == '__main__':
    train = pd.read_csv(trainData)
    test = pd.read_csv(testData)
    dataTrain, labelTrain = preprocessData(train)
    dataTest, labelTest = preprocessData(test)
    date_startTrain = datetime.now()
    print("开始训练")
    w_0, w, v = SGD_FM(mat(dataTrain), labelTrain, 20, 200)
    print(
        "训练准确性为:%f" % (1 - getAccuracy(mat(dataTrain), labelTrain, w_0, w, v)))
    date_endTrain = datetime.now()
    print(
        "训练用时为:%s" % (date_endTrain - date_startTrain))
    print("开始测试")
    print(
        "测试准确性为:%f" % (1 - getAccuracy(mat(dataTest), labelTest, w_0, w, v)))

你可能感兴趣的:(推荐系统,深度学习,pytorch)