Hello Forks,
I am doing text classification using Pytorch and Torchtext. Therefore, my problem is that i am getting a very low accuracy compared to the one i expected. Did i make any mistake in the computation of my accuracy or in the evaluation function?
 My dataset has 5 labels (1,2,3,4,5), i converted them to index_to_one_hot like this:
def index_to_one_hot(label):
sample_nums=label.size()[0]
one_hot=torch.tensor([0.,1.,2.,3.,4.])
one_hot=one_hot.view([1,5]).expand([sample_nums,5])
label=label.view([sample_nums,1]).expand([sample_nums,5])
one_hot=(label.float()==one_hot).float()
return one_hot
 My function that compute the accuracy is like this:
def compute_accuracy(preds, y):
p_top1=preds.topk(1,dim=1)[1]
y_top1=y.topk(1,dim=1)[1]
correct=(p_top1==y_top1).float().sum()
label_nums=preds.size()[0]
return correct,label_nums

My training function is like this:
#labels are called (Overall)
#My x is (ReviewText)
def train(model, iterator, optimizer, criterion):epoch_loss = 0
epoch_cor = 0
epoch_label=0
model.train()
for batch in iterator:
optimizer.zero_grad()
predictions = model(batch.ReviewText).squeeze(1)
loss = criterion(predictions,batch.Overall)
correct,label_nums= compute_accuracy(predictions, index_to_one_hot(batch.Overall))
loss.backward()
clip_gradient(model, 1e1)
optimizer.step()
epoch_loss += loss.item()
epoch_cor +=correct
epoch_label+=label_nums
return epoch_loss / len(iterator), epoch_cor/epoch_label 
My evaluation function is like this:
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_cor = 0
epoch_label=0
model.eval()
with torch.no_grad():
for batch in iterator:
predictions = model(batch.ReviewText).squeeze(1)
loss = criterion(predictions, batch.Overall)
correct,label_nums = compute_accuracy(predictions, index_to_one_hot(batch.Overall))
epoch_loss += loss.item()
epoch_cor +=correct
epoch_label+=label_nums
return epoch_loss / len(iterator), epoch_cor/epoch_label 
I used CrossEntropyLoss() as the loss the function.
Thanks for any assistance.