I'm trying to make a neural network with PyTorch to predict student's final exam grades. I've done it like this -
# Hyper Parameters
input_size = 2
hidden_size = 50
num_classes =21
num_epochs = 500
batch_size = 5
learning_rate = 0.1
# define a customise torch dataset
class DataFrameDataset(torch.utils.data.Dataset):
def __init__(self, df):
self.data_tensor = torch.Tensor(df.as_matrix())
# a function to get items by index
def __getitem__(self, index):
obj = self.data_tensor[index]
input = self.data_tensor[index][0:-1]
target = self.data_tensor[index][-1] - 1
return input, target
# a function to count samples
def __len__(self):
n, _ = self.data_tensor.shape
return n
# load all data
data_i = pd.read_csv('dataset/student-mat.csv', header=None,delimiter=";")
data = data_i.iloc[:,30:33]
# normalise input data
for column in data:
# the last column is target
if column != data.shape[1] - 1:
data[column] = data.loc[:, [column]].apply(lambda x: (x - x.mean()) / x.std())
# randomly split data into training set (80%) and testing set (20%)
msk = np.random.rand(len(data)) < 0.8
train_data = data[msk]
test_data = data[~msk]
# define train dataset and a data loader
train_dataset = DataFrameDataset(df=train_data)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
# Neural Network
class Net(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(Net, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.sigmoid = nn.Sigmoid()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.sigmoid(out)
out = self.fc2(out)
return out
net = Net(input_size, hidden_size, num_classes)
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Rprop(net.parameters(), lr=learning_rate)
# store all losses for visualisation
all_losses = []
# train the model by batch
for epoch in range(num_epochs):
for step, (batch_x, batch_y) in enumerate(train_loader):
# convert torch tensor to Variable
X = Variable(batch_x)
Y = Variable(batch_y.long())
# Forward + Backward + Optimize
optimizer.zero_grad() # zero the gradient buffer
outputs = net(X)
loss = criterion(outputs, Y)
all_losses.append(loss.data[0])
loss.backward()
optimizer.step()
if epoch % 50 == 0:
_, predicted = torch.max(outputs, 1)
# calculate and print accuracy
total = predicted.size(0)
correct = predicted.data.numpy() == Y.data.numpy()
print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Accuracy: %.2f %%'
% (epoch + 1, num_epochs, step + 1,
len(train_data) // batch_size + 1,
loss.data[0], 100 * sum(correct)/total))
I'm getting an error at line loss = criterion(outputs, Y) which says -
RuntimeError: Assertion 'cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/torch/lib/THNN/generic/ClassNLLCriterion.c:62
I'm not able to figure out what I'm doing wrong as I'm pretty new to this and I've already checked the other posts here but, they don't seem to help.
The data dataframe looks like -
30 31 32
0 5 6 6
1 5 5 6
2 7 8 10
3 15 14 15
4 6 10 10
5 15 15 15
Can anyone tell me what I am doing wrong and how can I correct it. Any help is appreciated! Thanks! :)
I had the same error in my program and i just realized that the problem was in the number of output nodes in my network In my program the number of output nodes of my model was not equal to the number of labels of the data the number of output was 1 and the number of target labels was 10. then i changed the number of output to 10, there was no error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With