Introduction
RoBERTa and DistilBERT are two popular models that help computers understand human language better. They make tasks like reading, answering questions, or summarizing text easier and faster.
Jump into concepts and practice - no test required
from transformers import AutoModelForSequenceClassification, AutoTokenizer # Load model and tokenizer model = AutoModelForSequenceClassification.from_pretrained('roberta-base') tokenizer = AutoTokenizer.from_pretrained('roberta-base') # Or for DistilBERT model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased') tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
from transformers import pipeline # Create sentiment analysis pipeline with RoBERTa nlp = pipeline('sentiment-analysis', model='textattack/roberta-base-SST-2') result = nlp('I love learning about AI!')
from transformers import pipeline # Create sentiment analysis pipeline with DistilBERT nlp = pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english') result = nlp('I love learning about AI!')
from transformers import pipeline # Use RoBERTa for sentiment analysis roberta_nlp = pipeline('sentiment-analysis', model='textattack/roberta-base-SST-2') roberta_result = roberta_nlp('I enjoy sunny days and learning new things!') # Use DistilBERT for sentiment analysis distilbert_nlp = pipeline('sentiment-analysis', model='distilbert-base-uncased-finetuned-sst-2-english') distilbert_result = distilbert_nlp('I enjoy sunny days and learning new things!') print('RoBERTa result:', roberta_result) print('DistilBERT result:', distilbert_result)
outputs.last_hidden_state?
from transformers import RobertaModel, RobertaTokenizer
import torch
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
model = RobertaModel.from_pretrained('roberta-base')
inputs = tokenizer('Hello', return_tensors='pt')
outputs = model(**inputs)
print(outputs.last_hidden_state.shape)from transformers import DistilBertModel
model = DistilBertModel.from_pretrained('roberta-base')
What is the main issue causing the error?