首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建MLP模型来预测用户使用PyTorch给未看过的电影的评分

创建MLP模型来预测用户使用PyTorch给未看过的电影的评分
EN

Stack Overflow用户
提问于 2020-07-10 17:02:57
回答 1查看 440关注 0票数 0

在我的项目中,我试图根据用户给其他电影的评分来预测用户对一部看不见的电影的评分。我使用的是movielens dataset.The主文件夹,它是ml-100k,包含关于100,000 的信息。

在对数据进行处理之前,主要数据(分级数据)包含用户ID、电影ID、用户评等(从0到5,以及该项目考虑的).I,然后使用sklearn库将数据拆分为培训集(80%)和测试数据(20%)。

为了创建推荐系统,正在使用模型‘Stacked-Autoencoder’。我使用的是PyTorch,代码是在Google 上实现的。这个项目是基于这个https://towardsdatascience.com/stacked-auto-encoder-as-a-recommendation-system-for-movie-rating-prediction-33842386338

我刚开始深造,我想把这个模型(Stacked_Autoencoder)和另一个深度学习模式进行比较。例如,我想使用多层感知(MLP)。这是为了研究目的。这是下面的代码,用于创建堆叠-自动编码器模型和训练模型。

代码语言:javascript
运行
复制
### Part 1 : Archirecture of the AutoEncoder 

#nn.Module is a parent class 
# SAE is a child class of the parent class nn.Module
class SAE(nn.Module): 
# self is the object of the SAE class 

# Archirecture 
    def __init__(self, ): 
    # self can use alll the methods of the class nn.Module
        super(SAE,self).__init__()
    # Full connected layer  n°1, input and 20 neurons-nodes of the first layer
    # one neuron can be the genre of the movie
    
    # Encode step 
        self.fc1 = nn.Linear(nb_movies,20)
    # Full connected layer n°2 
        self.fc2 = nn.Linear(20,10)
    
    # Decode step 
    # Full connected layer n°3
        self.fc3 = nn.Linear(10,20) 
    # Full connected layer n°4
        self.fc4 = nn.Linear(20,nb_movies) 
    # Sigmoid activation function 
        self.activation = nn.Sigmoid()

# Action : activation of the neurons
def forward(self, x) : 
        x = self.activation(self.fc1(x))
        x = self.activation(self.fc2(x))
        x = self.activation(self.fc3(x))
        # dont's use the activation function 
        # use the linear function only 
        x = self.fc4(x)
        # x is th evector of predicted ratings
        return x 

# Create the AutoEncoder object 
sae=SAE()
#MSE Loss : imported from torch.nn 
criterion=nn.MSELoss() 
# RMSProp optimizer (update the weights) imported from torch.optim 
#sea.parameters() are weights and bias adjusted during the training
optimizer=optim.RMSProp(sae.parameters(),lr=0.01, weight_decay=0.5)

### Part 2 : Training of the SAE 
# number of epochs 
nb_epochs = 200 
# Epoch forloop 
for epoch in range(1, nb_epoch+1): 
        # at the beginning the loss is at zero
        s=0.
        train_loss = 0 

        #Users forloop 
        for id_user in range(nb_users)
            # add one dimension to make a two dimension vector.
            # create a new dimension and put it the first position .unsqueeze[0]
            input = Variable(training_set[id_user].unsqueeze[0])
            
            # clone the input to obtain the target  
            target= input.clone()
            
            # target.data are all the ratings 
            # ratings > 0
            if torch.sum(target.data >0) > 0
                output = sae(input)
                # don't compute the gradients regarding the target
                target.require_grad=False 
                # only deal with true ratings 
                output[target==0]=0
                
                # Loss Criterion 
                loss =criterion(output,target)
                
                # Average the error of the movies that don't have zero ratings
                mean_corrector=nb_movies/float(torch.sum(target.data>0)+1e-10)
                
                # Direction of the backpropagation 
                loss.backward()
                train_loss+=np.sqrt(loss.data[0]*mean_corrector)
                s+=1.
                
                # Intensity of the backpropagation 
                optimizer.step()
        
    print('epoch:' +str (epoch)+'loss:' +str(train_loss/s)

)

如果我想用MLP模型训练的话。如何实现这个类模型?另外,我还可以使用什么其他深度学习模型(除了MLP)来与堆叠式自动编码器进行比较呢?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-25 22:40:30

MLP不适合于建议。如果您想走这个路线,您将需要为您的userid创建一个嵌入,为您的itemid创建另一个嵌入,然后在嵌入的基础上添加线性层。您的目标将是预测用户to itemid对的评级。

我建议你看看变分自动编码器(VAE)。他们给出了最先进的推荐系统。他们也将提供一个公平的比较,您的堆叠-自动编码器。以下是将VAE应用于协同过滤的研究论文:https://arxiv.org/pdf/1802.05814.pdf

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62839095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档