简体   繁体   English

PyTorch 矩阵分解嵌入错误

[英]PyTorch matrix factorization embedding error

I'm trying to use a single hidden layer NN to perform matrix factorization.我正在尝试使用单个隐藏层 NN 来执行矩阵分解。 In general, I'm trying to solve for a tensor, V, with dimensions [9724x300] where there are 9724 items in inventory, and 300 is the arbitrary number of latent features.一般来说,我试图求解一个尺寸为 [9724x300] 的张量 V,其中库存中有 9724 个项目,而 300 是任意数量的潜在特征。

The data that I have is a [9724x9724] matrix, X, where columns and rows represent the number of mutual likes.我拥有的数据是一个 [9724x9724] 矩阵 X,其中的列和行代表相互喜欢的数量。 (eg X[0,1] represents the sum of users who like both item 0 and item 1. Diagonal entries are not of importance. (例如,X[0,1] 表示同时喜欢项目 0 和项目 1 的用户的总和。对角线条目并不重要。

My goal is to use MSE loss, such that the dot product of V[i,:] on V[j,:] transposed is very, very close to X[i,j].我的目标是使用 MSE 损失,使得 V[i,:] 在 V[j,:] 转置后的点积非常非常接近 X[i,j]。

Below is code that I've adapted from the below link.以下是我从以下链接改编的代码。

https://blog.fastforwardlabs.com/2018/04/10/pytorch-for-recommenders-101.html https://blog.fastforwardlabs.com/2018/04/10/pytorch-for-recommenders-101.html

import torch
from torch.autograd import Variable

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):
        return (self.vectors([i])*torch.transpose(self.vectors([j]))).sum(1)

    def predict(self, i, j):
        return self.forward(i, j)

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

for i in range(len(movie_ids)):
    for j in range(len(movie_ids)):
    # get user, item and rating data
        rating = Variable(torch.FloatTensor([Xij[i, j]]))
        # predict
#         i = Variable(torch.LongTensor([int(i)]))
#         j = Variable(torch.LongTensor([int(j)]))
        prediction = model(i, j)
        loss = loss_fn(prediction, rating)

        # backpropagate
        loss.backward()

        # update weights
        optimizer.step()

The error returned is:返回的错误是:

TypeError: embedding(): argument 'indices' (position 2) must be Tensor, not list

I'm very new to embeddings.我对嵌入很陌生。 I had tried replacing embeddings as a simple float tensor, however the MatrixFactorization class, which I defined, did not recognize the tensor as a model parameters to be optimized over.我曾尝试将嵌入替换为简单的浮点张量,但是我定义的 MatrixFactorization class 没有将张量识别为要优化的 model 参数。

Any thoughts on where I'm going wrong?关于我要去哪里错的任何想法?

You are passing a list to self.vectors ,您正在将列表传递给self.vectors

return (self.vectors([i])*torch.transpose(self.vectors([j]))).sum(1)

Try to convert it to tensor before you call self.vectors()在调用self.vectors()之前尝试将其转换为张量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM