RuntimeError: size mismatch, m1: [1 x 3951], m2: [1682 x 20]

username: Saswata_Bhattacharya
cooked: <p>import os.path<br>
import csv<br>
import numpy as np<br>
import pandas as pd<br>
import torch<br>
import torch.nn as nn<br>
import torch.nn.parallel<br>
import torch.optim as optim<br>
import torch.utils.data<br>
from torch.autograd import Variable<br>
from pathlib import Path</p>
<h1>Importing the dataset</h1>
<p>movies = pd.read_csv(‘ml-1m/movies.dat’, sep = ‘::’, header = None, engine = ‘python’, encoding = ‘latin-1’)<br>
users = pd.read_csv(‘ml-1m/users.dat’, sep = ‘::’, header = None, engine = ‘python’, encoding = ‘latin-1’)<br>
ratings = pd.read_csv(‘ml-1m/ratings.dat’, sep = ‘::’, header = None, engine = ‘python’, encoding = ‘latin-1’)</p>
<h1>Preparing the training set and the test set</h1>
<p>training_set = pd.read_csv(‘ml-100k/u2.base’, delimiter = ‘\t’)<br>
training_set = np.array(training_set, dtype = ‘int’)<br>
test_set = pd.read_csv(‘ml-100k/u2.test’, delimiter = ‘\t’)<br>
test_set = np.array(test_set, dtype = ‘int’)</p>
<h1>Getting the number of users and movies</h1>
<p>nb_users = int(max(max(training_set[:,0]), max(test_set[:,0])))<br>
nb_movies = int(max(max(training_set[:,1]), max(test_set[:,1])))</p>
<h1>Converting the data into an array with users in lines and movies in columns</h1>
<p>def convert(data):<br>
new_data = []<br>
for id_users in range(1, nb_users + 1):<br>
id_movies = data[:,1][data[:,0] == id_users]<br>
id_ratings = data[:,2][data[:,0] == id_users]<br>
ratings = np.zeros(nb_movies)<br>
ratings[id_movies - 1] = id_ratings<br>
new_data.append(list(ratings))<br>
return new_data<br>
training_set = convert(training_set)<br>
test_set = convert(test_set)</p>
<h1>Converting the data into Torch tensors</h1>
<p>training_set = torch.FloatTensor(training_set)<br>
test_set = torch.FloatTensor(test_set)</p>
<p>class SAE(nn.Module):<br>
def <strong>init</strong>(self, ):<br>
super(SAE, self).<strong>init</strong>()<br>
self.fc1 = nn.Linear(nb_movies, 20)<br>
self.fc2 = nn.Linear(20, 10)<br>
self.fc3 = nn.Linear(10, 20)<br>
self.fc4 = nn.Linear(20, nb_movies)<br>
self.activation = nn.Sigmoid()<br>
def forward(self, x):<br>
x = self.activation(self.fc1(x))<br>
x = self.activation(self.fc2(x))<br>
x = self.activation(self.fc3(x))<br>
x = self.fc4(x)<br>
x.view(x.size(0), -1)<br>
return x<br>
sae = SAE()<br>
criterion = nn.MSELoss()<br>
optimizer = optim.RMSprop(sae.parameters(), lr = 0.01, weight_decay = 0.5)</p>
<p>from pathlib import Path, PureWindowsPath<br>
filename = Path(“D:/Movie-Recommendation-System-using-AutoEncoders-master/Movie-Recommendation-System-using-AutoEncoders-master”)</p>
<h1>Convert path to Windows format</h1>
<p>path_on_windows = PureWindowsPath(filename)<br>
print(path_on_windows)</p>
<p>def load_checkpoint(path_on_windows):<br>
checkpoint = torch.load(path_on_windows)<br>
model = checkpoint[‘model’]<br>
model.load_state_dict(checkpoint[‘state_dict’])<br>
for parameter in model.parameters():<br>
parameter.requires_grad = False<br>
model.eval()<br>
return model</p>
<p>model = load_checkpoint(‘checkpoint.pth’)<br>
<span class="hashtag">#print</span>(model)</p>
<p>movie_dict = {3709 : ‘Sleepwalkers’,<br>
2846 : ‘Adventures of Milo and Otis’,<br>
3880 : ‘Ballad of Ramblin Jack’,<br>
2971 : ‘All That Jazz’,<br>
3951 : ‘Two Family House’,<br>
3681 : ‘For a Few Dollars More’,<br>
3921 : ‘Beach Party’,<br>
3541 : ‘Third World Cop’,<br>
2189 : ‘I Married A Strange Person’,<br>
3687 : ‘Light Years’,<br>
3390 : ‘Shanghai Surprise’,<br>
2940 : ‘Gilda’,<br>
3857 : ‘Bless the Child’,<br>
1464 : ‘Lost Highway’,<br>
3376 : ‘Fantastic Night-The (La Nuit Fantastique)’,<br>
3670 : ‘Story of G.I. Joe’,<br>
3906 : ‘Under Suspicion’,<br>
792 : ‘Hungarian Fairy Tale’}<br>
count = nb_users<br>
sid = count+1<br>
sid2 = sid-1<br>
with open(‘ml-100k/u2.base’, mode=‘a’) as file:<br>
writer = csv.writer(file, delimiter=’\t’, lineterminator=’\n’)<br>
for iD in movie_dict:<br>
<span class="hashtag">#print</span>(movie_dict[iD])<br>
<span class="hashtag">#a</span> = int(input(‘Enter rating:’))<br>
writer.writerow([sid,iD,4,‘888692464’])<br>
file.close()<br>
<span class="hashtag">#nb_users</span> = int(max(max(training_set[:,0]), max(test_set[:,0])))<br>
training_set = pd.read_csv(‘ml-100k/u2.base’, delimiter = ‘\t’)<br>
training_set = np.array(training_set, dtype = ‘int’)<br>
training_set = convert(training_set)<br>
training_set = torch.FloatTensor(training_set)<br>
sid = 954<br>
input = Variable(training_set[sid]).unsqueeze(0)<br>
output = model(input)<br>
lol = output.detach().numpy()<br>
loll = lol[0]<br>
for i in loll:<br>
if(i&gt;4):<br>
print(100,’:’,round(i))</p>
<p>The above is my code for recommender system using autoencoder. While running the code, I am getting the following error:</p>
<p>RuntimeError: size mismatch, m1: [1 x 3951], m2: [1682 x 20] at C:\w\1\s\windows\pytorch\aten\src\TH/generic/THTensorMath.cpp:136</p>

username: ptrblck
cooked: <p>Based on the error message I assume the first layer might throw this error.<br>
Could you check the shape of your input tensors and make sure the <code>in_features</code> are set to the number of features for each sample?</p>
<p>PS: You can add code snippets by wrapping them into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>