keshav22bansal / BAKSA_IITK

Official implementation of paper - "BAKSA at SemEval-2020 Task 9: Bolstering CNN with Self-Attention for Sentiment Analysis of Code Mixed Text" accepted at Proceeding of the 14th International Workshop on Semantic Evaluation.
18 stars 3 forks source link

Can u tell me what does this last part of code do? #7

Closed som983 closed 3 years ago

som983 commented 3 years ago

``def ensemble_write_to_file(models, test_iterator): label_dict = {'0':'negative', '1':'neutral', '2':'positive'} file = open("answer.txt", "w") file.write('Uid,Sentiment\n') count = 0 for batch in test_iterator: predictions0 = models0.squeeze(1) predictions1 = models[1](batch.text, batch_size=len(batch)).squeeze(1) predictions = F.softmax(predictions0, dim=1) * F.softmax(predictions1, dim=1) max_preds = predictions.argmax(dim = 1, keepdim = True).detach().cpu().numpy() for i,row in enumerate(batch.uid.cpu().numpy()): count += 1 label_number = max_preds[i][0] label_number_str = list(LABEL.vocab.stoi.keys())[list(LABEL.vocab.stoi.values()).index(label_number)] predicted_label_name = label_dict[label_number_str] if count != len(test_data): file.write('%s,%s\n'%(row,predicted_label_name)) else: file.write('%s,%s'%(row,predicted_label_name)) file.close()

valid_loss, valid_acc, tot = ensemble_evaluate(models, test_iterator, criterion) print(f'\t Val. Loss: {valid_loss:.3f} | Val. Acc: {valid_acc*100:.2f}%') print(tot)``

Harshagarwal19 commented 3 years ago

Hi @som983, The function runs inferences for test data using the trained model. And the predicted labels are finally written to a text file.

Thanks!