Застрял на PyTorch - RuntimeError: несоответствие размера - PullRequest
0 голосов
/ 19 февраля 2020

Я пытаюсь создать систему рекомендаций в качестве демонстрационного проекта для самостоятельной работы, почти на грани, но получаю ошибку времени исполнения из-за несоответствия размеров, которая, похоже, просто не справляется. Это точный код ошибки ...

RuntimeError: несоответствие размера, m1: [3951 x 1], м2: [1682 x 20] при C: \ w \ 1 \ s \ tmp_conda_3.7_105232 \ conda \ conda-bld \ pytorch_1579085620499 \ work \ aten \ src \ TH / generic / THTensorMath. cpp: 136

Ниже приведен код и выходные данные, которые прилагаются. Попытка изменить unqueeze (0) на 1 & -1, но не помогла не найти выход в решении этой ошибки, может кто-то, пожалуйста, помогите. Спасибо:)

#CODE

import os.path
import csv
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
import torch.utils.data
from torch.autograd import Variable
from pathlib import Path


# Importing the dataset
movies = pd.read_csv('ml-1m/movies.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')
users = pd.read_csv('ml-1m/users.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')
ratings = pd.read_csv('ml-1m/ratings.dat', sep = '::', header = None, engine = 'python', encoding = 'latin-1')

# Preparing the training set and the test set
training_set = pd.read_csv('ml-100k/u2.base', delimiter = '\t')
training_set = np.array(training_set, dtype = 'int')
test_set = pd.read_csv('ml-100k/u2.test', delimiter = '\t')
test_set = np.array(test_set, dtype = 'int')

# Getting the number of users and movies
nb_users = int(max(max(training_set[:,0]), max(test_set[:,0])))
nb_movies = int(max(max(training_set[:,1]), max(test_set[:,1])))

# Converting the data into an array with users in lines and movies in columns
def convert(data):
    new_data = []
    for id_users in range(1, nb_users + 1):
        id_movies = data[:,1][data[:,0] == id_users]
        id_ratings = data[:,2][data[:,0] == id_users]
        ratings = np.zeros(nb_movies)
        ratings[id_movies - 1] = id_ratings
        new_data.append(list(ratings))
    return new_data
training_set = convert(training_set)
test_set = convert(test_set)

# Converting the data into Torch tensors
training_set = torch.FloatTensor(training_set)
test_set = torch.FloatTensor(test_set)

class SAE(nn.Module):
    def __init__(self, ):
        super(SAE, self).__init__()
        self.fc1 = nn.Linear(nb_movies, 20)
        self.fc2 = nn.Linear(20, 10)
        self.fc3 = nn.Linear(10, 20)
        self.fc4 = nn.Linear(20, nb_movies)
        self.activation = nn.Sigmoid()
    def forward(self, x):
        x = self.activation(self.fc1(x))
        x = self.activation(self.fc2(x))
        x = self.activation(self.fc3(x))
        x = self.fc4(x)
        return x
sae = SAE()
criterion = nn.MSELoss()
optimizer = optim.RMSprop(sae.parameters(), lr = 0.01, weight_decay = 0.5)


from pathlib import Path, PureWindowsPath
filename = Path("C:/Users/MrWhoztheBoss/Documents/Movie-Recommendation-System-using-AutoEncoders-master/checkpoint.pth")
# Convert path to Windows format
path_on_windows = PureWindowsPath(filename)
print(path_on_windows)


def load_checkpoint(path_on_windows):
    checkpoint = torch.load(path_on_windows)
    model = checkpoint['model']
    model.load_state_dict(checkpoint['state_dict'])
    for parameter in model.parameters():
        parameter.requires_grad = False
    model.eval()
    return model


model = load_checkpoint('checkpoint.pth')
print(model)

movie_dict = {3709 : 'Sleepwalkers',
              2846 : 'Adventures of Milo and Otis',
              3880 : 'Ballad of Ramblin Jack',
              2971 : 'All That Jazz',
              3951 : 'Two Family House',
              3681 : 'For a Few Dollars More',
              3921 : 'Beach Party',
              3541 : 'Third World Cop',
              2189 : 'I Married A Strange Person',
              3687 : 'Light Years',
              3390 : 'Shanghai Surprise',
              2940 : 'Gilda',
              3857 : 'Bless the Child',
              1464 : 'Lost Highway',
              3376 : 'Fantastic Night-The (La Nuit Fantastique)',
              3670 : 'Story of G.I. Joe',
              3906 : 'Under Suspicion',
              792 : 'Hungarian Fairy Tale'}
count = nb_users
sid = count

with open('ml-100k/u2.base', mode='a') as file:
    for iD in movie_dict:
        print(movie_dict[iD])
        a = int(input('Enter rating:'))
        writer = csv.writer(file, delimiter='\t', lineterminator='\n')
        writer.writerow([sid,iD,a,'888692464'])

def check():
    test_loss = 0
    s = 0.
    for id_user in range(nb_users):
        input = Variable(training_set[id_user]).unsqueeze(0)
        output = model(input)
        lol = output.detach().numpy()
        loll = lol[0]
        for i in loll:
            if(i>0):
                print(id_user,':',round(i))


check()
#OUTPUT

C:\Users\MrWhoztheBoss\Documents\Movie-Recommendation-System-using-AutoEncoders-master\checkpoint.pth
SAE(
  (fc1): Linear(in_features=1682, out_features=20, bias=True)
  (fc2): Linear(in_features=20, out_features=10, bias=True)
  (fc3): Linear(in_features=10, out_features=20, bias=True)
  (fc4): Linear(in_features=20, out_features=1682, bias=True)
  (activation): Sigmoid()
)
Sleepwalkers
Enter rating:3
Adventures of Milo and Otis
Enter rating:3
Ballad of Ramblin Jack
Enter rating:3
All That Jazz
Enter rating:3
Two Family House
Enter rating:3
For a Few Dollars More
Enter rating:3
Beach Party
Enter rating:3
Third World Cop
Enter rating:3
I Married A Strange Person
Enter rating:3
Light Years
Enter rating:3
Shanghai Surprise
Enter rating:3
Gilda
Enter rating:3
Bless the Child
Enter rating:3
Lost Highway
Enter rating:3
Fantastic Night-The (La Nuit Fantastique)
Enter rating:3
Story of G.I. Joe
Enter rating:3
Under Suspicion
Enter rating:3
Hungarian Fairy Tale
Enter rating:3
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-25c653a68ff6> in <module>
    124 
    125 
--> 126 check()

<ipython-input-1-25c653a68ff6> in check()
    116     for id_user in range(nb_users):
    117         input = Variable(training_set[id_user]).unsqueeze(dim=-1)
--> 118         output = model(input)
    119         lol = output.detach().numpy()
    120         loll = lol[0]

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

<ipython-input-1-25c653a68ff6> in forward(self, x)
     53         self.activation = nn.Sigmoid()
     54     def forward(self, x):
---> 55         x = self.activation(self.fc1(x))
     56         x = self.activation(self.fc2(x))
     57         x = self.activation(self.fc3(x))

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\Anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---> 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~\Anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
-> 1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
   1372         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [1682 x 1], m2: [1682 x 20] at C:\w\1\s\tmp_conda_3.7_105232\conda\conda-bld\pytorch_1579085620499\work\aten\src\TH/generic/THTensorMath.cpp:136
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...