You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why the prediction accuracy is only ~0.5 for IMDB sentimental analysis when applying the "AdapterHub/roberta-base-pf-imdb" adapter with 'roberta-base' transformer?
#723
I wrote a code based on 02_Adapter_Inference.ipynb to apply the "AdapterHub/roberta-base-pf-imdb" adapter for IMDB sentimental analysis. But I found the prediction results are always around ~0.5, which is like random selection.
I have 2 questions and would like to ask for help:
Why the prediction accuracy is only 0.5? Does that mean I have to train the adapter first before interfering it? is it possible to directly interfering the adapter without training it?
I also see a warning message'There are adapters available but none are activated for the forward pass.' when running the code. Is this the reason why the prediction accuracy is so low? How to resolve this issue?
Here is my code:
from adapters import AutoAdapterModel
from transformers import TextClassificationPipeline, pipeline, RobertaConfig,RobertaTokenizer
from datasets import load_dataset
from warnings import simplefilter
import numpy as np
import torch
import os
device = "cuda" if torch.cuda.is_available() else "cpu"
os.environ['CURL_CA_BUNDLE'] = ''
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
config = RobertaConfig.from_pretrained(
"roberta-base",
num_labels=2,
)
model = AutoAdapterModel.from_pretrained(
"roberta-base",
config=config,
)
sa_adapter = model.add_adapter("AdapterHub/roberta-base-pf-imdb", config="seq_bn")
model.add_classification_head(
"AdapterHub/roberta-base-pf-imdb",
num_labels=2,
id2label={ 0: 0, 1: 1}
)
model.set_active_adapters(sa_adapter)
model.to(device)
sa_pipeline = TextClassificationPipeline(model=model, tokenizer=tokenizer, batch_size=300, device=0 if device == "cuda" else -1)
simplefilter(action='ignore', category=FutureWarning)
os.environ['CURL_CA_BUNDLE'] = ''
dataset = load_dataset("stanfordnlp/imdb")
print(dataset['test'][0])
def batch_process(dataset):
labels = [data['label'] for data in dataset]
labels = torch.LongTensor(labels)
texts = [data['text'] for data in dataset]
return labels, texts
def evaluate():
loader_test = torch.utils.data.DataLoader(dataset=dataset['test'], batch_size=300, collate_fn=batch_process, shuffle=True, drop_last=True)
all_pred_labels = []
all_correct_labels = []
for i, batch in enumerate(loader_test):
text_batch= batch[1]
label_batch = batch[0]
output = sa_pipeline(text_batch, truncation = True, max_length=500, padding = 'max_length')
pred_output = [pred['label'] for pred in output]
all_pred_labels.extend(pred_output)
correct_label = label_batch.tolist()
all_correct_labels.extend(correct_label)
all_pred_labels = np.array(all_pred_labels)
all_correct_labels = np.array(all_correct_labels)
accuracy = np.mean(all_pred_labels == all_correct_labels)
print(accuracy)
evaluate()
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I wrote a code based on 02_Adapter_Inference.ipynb to apply the "AdapterHub/roberta-base-pf-imdb" adapter for IMDB sentimental analysis. But I found the prediction results are always around ~0.5, which is like random selection.
I have 2 questions and would like to ask for help:
Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions