Navigation

    Gpushare.com

    • Register
    • Login
    • Search
    • Popular
    • Categories
    • Recent
    • Tags

    【3】使用pytorch_lightning+transformers+torchmetric+datasets进行文本分类

    技术交流
    1
    1
    48
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 183****0229
      183****0229 last edited by

      transformers中AutoModelForSequenceClassification的使用

      # 导包
      import torch
      from transformers import AutoModelForSequenceClassification,AutoTokenizer
      
      # huggingface.co上的模型
      pretrained_name_or_path = "junnyu/roformer_chinese_base"
      
      # 加载预训练的tokenizer
      tokenizer = AutoTokenizer.from_pretrained(pretrained_name_or_path)
      
      # 加载预训练的model,并定义进行15分类
      model = AutoModelForSequenceClassification.from_pretrained(pretrained_name_or_path, num_labels=15)
      
      # 输入的文本
      text_list = ["江疏影甜甜圈自拍,迷之角度竟这么好看,美吸引一切事物","军嫂探亲拧包入住,部队家属临时来队房标准有了规定,全面落实!"]
      labels = torch.tensor([1,2])
      # 进行tokenizer
      tokenized_inputs = tokenizer(text_list,padding=True,return_tensors="pt")
      print(tokenized_inputs)
      
      with torch.no_grad():
          outputs = model(**tokenized_inputs,labels=labels)
      print(outputs.loss)
      print(outputs.logits.shape) # shape [2,15]
      
      


      1 Reply Last reply Reply Quote 2
      • First post
        Last post