defpreprocess_text(text:str):
text=text.lower()
text=''.join([c for c in text if c.isalnum()or c.isspace()])return text
模型加载的函数
利用transformer库加载一个模型
from transformers import AutoModelForSequenceClassification,AutoTokenizer
defload_model_tokenizer(model_name):
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)return tokenizer, model
text_to_classify="i am your father"
predicted_class=self_pipe_line(text_to_classify)print(f"predict class: {predicted_class}")
Some weights of GPT2ForSequenceClassification were not initialized from the model checkpoint at ./model/gpt2 and are newly initialized: ['score.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
predict class: tensor([1])