Inference of bert-base with sst2 dataset
runbert-base-uncasedwithsst2dataset, but all predictions are 1!!from transformers import AutoModelForSequenceClassification, AutoTokenizerimport datasetsimport torchfrom sklearn.metrics import accuracy_scorefrom transformers import pipelinefrom transformers.pipelines.pt_utils import KeyDatasetfrom datasets import load_datasetimport evaluateload pretrained modelmodel_name=‘bert-base-uncased’model = AutoModelForSequenceClassification.from_pretrained(model_name)load the tokenixer for the pretrained modeltokenizer = AutoTokenizer.from_pretrained(model_name)create the pielinegenerator = pipeline(task=‘sentiment-analysis’, model=model, tokenizer=tokenizer)load datasetdataset = load_dataset(“sst2”,split=“test”)pass the dataset to the pipelineresults = generator(KeyDataset(dataset, “sentence”))Extract true labels from the datasePrttrue_labels = dataset[“label”]print(“True labels”, true_labels)print(“TrueLabelSize=”,len(true_labels))Extract the labels from the pipeline resultspred_labels= [0 if result[“label”]==“LABEL_0” else 1 for result in results]print(“PredLabels”,pred_labels)print(“predLabelSize=”,len(pred_labels))Load the accuracy metricaccuracy_metric = evaluate.load(“accuracy”)Compute accuracyaccuracy = accuracy_metric.compute(predictions=pred_labels, references=true_labels)print(“Accuracy:”, accuracy)