【发布时间】:2020-05-28 16:38:57
【问题描述】:
我目前正在 Udemy 上一门深度学习课程。 我目前正在设计一个受限玻尔兹曼机,其中 我的训练运行良好,但我在测试时遇到了这个错误
IndexError: 索引 0 处的掩码 [1, 1682] 的形状与索引 0 处的索引张量 [100, 1682] 的形状不匹配
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
# Importing 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 training and test set
training_set = pd.read_csv('ml-100k/u1.base',delimiter='\t')
training_set = np.array(training_set,dtype='int')
test_set = pd.read_csv('ml-100k/u1.test',delimiter='\t')
test_set = np.array(test_set,dtype='int')
#Getting the no of users and movies
nb_users = int(max(max(training_set[:,0]),max(test_set[:,0]))) #max out of both
nb_movies = int(max(max(training_set[:,1]),max(test_set[:,1])))
#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 data to torcch tensors
training_set = torch.FloatTensor(training_set)
test_set = torch.FloatTensor(test_set)
# Converting the rating into binary ratings 1 (Liked) or 0 (Not liked)
training_set[training_set == 0] = -1 #taking all zero values in trainingset
training_set[training_set == 1] = 0
training_set[training_set == 2] = 0
training_set[training_set >= 3] = 1
test_set[test_set == 0] = -1 #taking all zero values in trainingset
test_set[test_set == 1] = 0
test_set[test_set == 2] = 0
test_set[test_set >= 3] = 1
# Creating the architecture of the Neural network
class RBM():
def __init__(self, nv, nh):
self.W = torch.randn(nh, nv)
self.a = torch.randn(1, nh)
self.b = torch.randn(1, nv)
def sample_h(self, x):
wx = torch.mm(x, self.W.t())
activation = wx + self.a.expand_as(wx)
p_h_given_v = torch.sigmoid(activation)
return p_h_given_v, torch.bernoulli(p_h_given_v)
def sample_v(self, y):
wy = torch.mm(y, self.W)
activation = wy + self.b.expand_as(wy)
p_v_given_h = torch.sigmoid(activation)
return p_v_given_h, torch.bernoulli(p_v_given_h)
def train(self, v0, vk, ph0, phk):
self.W += (torch.mm(v0.t(), ph0) - torch.mm(vk.t(), phk)).t()
self.b += torch.sum((v0 - vk), 0)
self.a += torch.sum((ph0 - phk), 0)
nv = len(training_set[0])
nh = 100
batch_size = 100
rbm = RBM(nv, nh)
#Training the RBM
nb_epoch = 10
for epoch in range(1, nb_epoch + 1):
train_loss = 0
s = 0.
for id_user in range(0, nb_users - batch_size, batch_size):
vk = training_set[id_user:id_user+batch_size]
v0 = training_set[id_user:id_user+batch_size]
ph0,_ = rbm.sample_h(v0)
for k in range(10):
_,hk = rbm.sample_h(vk)
_,vk = rbm.sample_v(hk)
vk[v0<0] = v0[v0<0]
phk,_ = rbm.sample_h(vk)
rbm.train(v0, vk, ph0, phk)
train_loss += torch.mean(torch.abs(v0[v0>=0] - vk[v0>=0]))
s += 1.
print(f'epoch: {epoch} loss: {train_loss/s}')
# Testing the RBM
test_loss = 0
s = 0.
for id_user in range(nb_users):
v = training_set[id_user:id_user+1]
vt = training_set[id_user:id_user+1]
if len(vt[vt>=0]) > 0:
_,h = rbm.sample_h(v)
_,v = rbm.sample_v(hk)
test_loss += torch.mean(torch.abs(vt[vt>=0] - v[vt>=0]))
s += 1.
print(f'test_loss: {test_loss/s}')
【问题讨论】:
-
请提供有关您的问题的更多详细信息尝试阅读this about how to ask和this about minimal reproducible example
-
这只是意味着当你尝试使用大小为
100,1682的张量时,你的面具的形状是1x1682。另外,对于未来,请查看stackoverflow.com/help/how-to-ask。共享代码,添加适当的标签(什么库?什么语言?是python)等等。 -
是的,很抱歉,我已经更新了信息
-
此外,最好提供完整的堆栈跟踪而不是只提供错误消息。
标签: python deep-learning pytorch