使用GPU的PyTorch进行矩阵分解

时间:2019-10-01 22:05:14

标签: python gpu pytorch matrix-factorization

下面的代码确实可以运行,但是使用for循环非常慢。在我的大学中,具有GPU资源的服务器可用。同样,我想了解如何使用批处理更有效地训练模型。

import torch
import torch.nn as nn
import torch.nn.functional as F

class MatrixFactorization(torch.nn.Module):
    def __init__(self, n_items=len(movie_ids), n_factors=300):
        super().__init__()

        self.vectors = nn.Embedding(n_items, n_factors,sparse=True)


    def forward(self, i,j):
        feat_i = self.vectors(i)
        feat_j = self.vectors(j)
        result = (feat_i * feat_j).sum(-1)
        return result


model = MatrixFactorization(n_items= len(movie_ids),n_factors=300)
loss_fn = nn.MSELoss() 
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

epochs = 100
for epoch in range(epochs):
    loss = 0
    for r,c in zip(r_index, c_index):
        i = torch.LongTensor([int(r)])
        j = torch.LongTensor([int(c)])
        rating = torch.FloatTensor([Xij[i, j]])
        # predict
        prediction = model(i, j)
        loss += loss_fn(prediction, rating)

                # Reset the gradients to 0
    optimizer.zero_grad()

                # backpropagate
    loss.backward()

                # update weights
    optimizer.step()
    print(loss)

我尝试了以下更改,但产生了警告。我不确定为什么我的目标尺寸不匹配,但这似乎是问题的原因。


epochs = 50
for epoch in range(epochs):
    loss = 0

    # predict
    i = torch.LongTensor(r_index)
    j = torch.LongTensor(c_index)
    ratings = Xij[i, j]
    prediction = model(i, j)
    loss += loss_fn(prediction, rating)

                # Reset the gradients to 0
    optimizer.zero_grad()

                # backpropagate
    loss.backward()

                # update weights
    optimizer.step()
    print(loss)

警告(不确定我哪里出了问题):

/anaconda3/lib/python3.6/site-packages/torch/nn/modules/loss.py:431: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([5931640])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)

1 个答案:

答案 0 :(得分:0)

第二个代码段中有一个错字

loss += loss_fn(prediction, ratings) # instead of rating