首页 > 其他分享 >LR、FM对比

LR、FM对比

时间:2023-03-31 11:46:03浏览次数:27  
标签:loss train metric 对比 self valid LR test FM

%%time

from utils.utils import create_dataset, Trainer
from layer.layer import Embedding, FeaturesEmbedding, EmbeddingsInteraction, MultiLayerPerceptron

import torch
import torch.nn as nn
import torch.optim as optim

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Training on [{}].'.format(device))

Training on [cpu].
CPU times: user 2.59 s, sys: 1.43 s, total: 4.03 s
Wall time: 1.95 s
%%time

dataset = create_dataset('lrmovielens100k', read_part=False, sample_num=1000000, device=device)
field_dims, (train_X, train_y), (valid_X, valid_y), (test_X, test_y) = dataset.train_valid_test_split()

CPU times: user 933 ms, sys: 30.5 ms, total: 964 ms
Wall time: 1.38 s
class FactorizationMachine(nn.Module):
    
    def __init__(self, field_dims, embed_dim=4):
        super(FactorizationMachine, self).__init__()
        
        self.embed1 = FeaturesEmbedding(field_dims, 1)
        self.embed2 = FeaturesEmbedding(field_dims, embed_dim)
        self.bias = nn.Parameter(torch.zeros((1, )))
        
    def forward(self, x):
        # x shape: (batch_size, num_fields)
        # embed(x) shape: (batch_size, num_fields, embed_dim)
        square_sum = self.embed2(x).sum(dim=1).pow(2).sum(dim=1)
        sum_square = self.embed2(x).pow(2).sum(dim=1).sum(dim=1)
        output = self.embed1(x).squeeze(-1).sum(dim=1) + self.bias + (square_sum - sum_square) / 2
        output = torch.sigmoid(output).unsqueeze(-1)
        return output

%%time

EMBEDDING_DIM = 8
LEARNING_RATE = 1e-4
REGULARIZATION = 1e-6
BATCH_SIZE = 4096
EPOCH = 600
TRIAL = 100

fm = FactorizationMachine(field_dims, EMBEDDING_DIM).to(device)
optimizer = optim.Adam(fm.parameters(), lr=LEARNING_RATE, weight_decay=REGULARIZATION)
criterion = nn.BCELoss()

trainer = Trainer(fm, optimizer, criterion, BATCH_SIZE)
trainer.train(train_X, train_y, epoch=EPOCH, trials=TRIAL, valid_X=valid_X, valid_y=valid_y)
test_loss, test_metric = trainer.test(test_X, test_y)
print('test_loss:  {:.5f} | test_metric:  {:.5f}'.format(test_loss, test_metric))

 68%|█████████████████████████████████████████▊                   | 411/600 [03:54<01:47,  1.75it/s]

image

train_loss: 0.66286 | train_metric: 0.60023
valid_loss: 0.66593 | valid_metric: 0.59360
test_loss:  0.66421 | test_metric:  0.59830
CPU times: user 15min 34s, sys: 1.09 s, total: 15min 35s
Wall time: 3min 55s
from sklearn import metrics
test_loss, test_metric = trainer.test(test_X, test_y, metricer=metrics.precision_score)
print('test_loss:  {:.5f} | test_metric:  {:.5f}'.format(test_loss, test_metric))
test_loss:  0.66421 | test_metric:  0.61538
class LogisticRegression(nn.Module):
    
    def __init__(self, field_dims):
        super(LogisticRegression, self).__init__()
        
        self.bias = nn.Parameter(torch.zeros((1, )))
        self.embed = FeaturesEmbedding(field_dims, 1)

    def forward(self, x):
        # x shape: (batch_size, num_fields)
        output = self.embed(x).sum(dim=1) + self.bias
        output = torch.sigmoid(output)
        return output
%%time

LEARNING_RATE = 1e-4
REGULARIZATION = 1e-6
BATCH_SIZE = 4096
EPOCH = 600
TRIAL = 100

lr = LogisticRegression(field_dims).to(device)
optimizer = optim.Adam(lr.parameters(), lr=LEARNING_RATE, weight_decay=REGULARIZATION)
criterion = nn.BCELoss()

trainer = Trainer(lr, optimizer, criterion, BATCH_SIZE)
trainer.train(train_X, train_y, epoch=EPOCH, trials=TRIAL, valid_X=valid_X, valid_y=valid_y)
test_loss, test_metric = trainer.test(test_X, test_y)
print('test_loss:  {:.5f} | test_metric:  {:.5f}'.format(test_loss, test_metric))
 38%|███████████████████████▍                                     | 230/600 [00:21<00:34, 10.76it/s]

image

train_loss: 0.67392 | train_metric: 0.57983
valid_loss: 0.67499 | valid_metric: 0.58050
test_loss:  0.67422 | test_metric:  0.57600
CPU times: user 1min 25s, sys: 239 ms, total: 1min 25s
Wall time: 21.6 s
from sklearn import metrics
test_loss, test_metric = trainer.test(test_X, test_y, metricer=metrics.precision_score)
print('test_loss:  {:.5f} | test_metric:  {:.5f}'.format(test_loss, test_metric))
test_loss:  0.67422 | test_metric:  0.58674

标签:loss,train,metric,对比,self,valid,LR,test,FM
From: https://www.cnblogs.com/RanX2018/p/17275802.html

相关文章

  • 基于MATLAB的ASK,FSK,PSK误码率对比仿真,输出调制后波形以及误码率曲线
    1.算法描述       在数字通信的三种调制方式(ASK、FSK、PSK)中,就频带利用率和抗噪声性能(或功率利用率)两个方面来看,一般而言,都是PSK系统最佳。所以PSK在中、高速数据传输中得到了广泛的应用。 ASK:幅度键控可以通过乘法器和开关电路来实现。载波在数字信号1或0的控制......
  • BIOS、BootLoader、uboot对比
    三者其实是一个概念,Android系统的CPU主频,内存大小都是由Uboot等控制的。biosBIOS是英文"BasicInputOutputSystem"的缩略语,直译过来后中文名称就是"基本输入输出系统"。其实,它是一组固化到计算机内主板上一个ROM芯片上的程序,它保存着计算机最重要的基本输入输出的程序、系统设......
  • [ABC273D] LRUD Instructions
    题目链接题解模拟题。观察题目,我们发现,无论问的是前/后/左/右,你都只会在一条直线上走,那对于这条直线,我们可以记录所有这条直线上的障碍物,然后找到距离当前点最近的障碍物,也就是说我们只能走到那个障碍物那块。虽然数据范围高达\(10^9\),但是\(n\le10^5\),所以用\(map\)套\(......
  • NSURLConnection和NSURLRequest
    iOS中我们可以通过NSURLRequest和NSURLConnection来建立一些HTTP请求下载数据等等先看一个例子:NSURLRequest*theRequest=[NSURLRequestrequestWithURL:[NSURLURLWithString:@“http://www.baidu.com/”]cachePolicy:NSURLRequestUse......
  • crictl和ctr与docker的命令的对比
    containerd相比于docker,多了namespace概念,每个image和container都会在各自的namespace下可见,目前k8s会使用k8s.io作为命名空间crictl使用命名空间k8s.iocric......
  • 一种huhffman树的写法
    在这个代码里我规定了字符集是英文字母,输入是26行,每行代表一个字母的频数(从a~z);为了方便起见,在储存中我用小写英文字母表示。(暂时只放代码,详解的话过阵子再补充)/*由......
  • threejs-camera&controls&renderer(WebGLRenderer)
    ArrayCamera:一般用于,展示益、一个场景存在多个物体,每个物体各自拥有自己的视角的这种场景。CubeCamera:一次性创建六个方位的相机(类似于正方体六个面,立方全景图中所有方......
  • Go语言:编写一个 WebsiteRacer 的函数,用来对比请求两个 URL 来「比赛」,并返回先响应的
    问题:你被要求编写一个叫做WebsiteRacer的函数,用来对比请求两个URL来「比赛」,并返回先响应的URL。如果两个URL在10秒内都未返回结果,那么应该返回一个error。......
  • Linux软件安装——lrzsz
    从windows系统上传文件1、搜索lrzsz安装包,命令为:yumlistlrzsz2、使用yum命令在线安装,yuminstalllrzsz.x86_64注:Yum(YellowdogUpdater,Modified)是一个在Fedora和Red......
  • 使用golang+antlr4构建一个自己的语言解析器(完结篇)
    Goland中Antlr4插件在goland中安装Antlr4插件,用于识别输入的字符在在语法文件中生成的语法树的样子,大概就是如下的摸样下载步骤:1.点击文件中的设置选项2.在插件目录......