hf_mirrors/roberta-large-sst2
Model Introduction
Files & versions
Analysis

roberta-large-sst2

This model is a fine-tuned version of roberta-large on the glue dataset. It achieves the following results on the evaluation set:

  • Loss: 0.1400
  • Accuracy: 0.9644

how to use

from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F
import os
import time

# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_name_or_path",
        type=str,
        help="Path to model",
        default="zhouhui/roberta-large-sst2",
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    model_path = args.model_name_or_path

    if is_torch_npu_available():
        device = "npu:0"
    else:
        device = "cpu"
    #device = "cpu"
    start_time = time.time()
    # 我们想要获取句子嵌入的句子
    sentences = ['This is an example sentence', 'Each sentence is converted']

    # 从openmind_hub加载模型
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModel.from_pretrained(model_path).to(device)
    #model = AutoModel.from_pretrained(model_path).to("cpu")

    # 对句子进行分词
    encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt').to(device)
    #encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt').to("cpu")

    # 计算token嵌入
    with torch.no_grad():
        model_output = model(**encoded_input)

    # 执行池化
    sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])

    # 归一化嵌入
    sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
    end_time = time.time()
    print("Sentence embeddings:")
    print(sentence_embeddings)
    time_taken = end_time - start_time
    print(f"硬件环境:{device},推理执行时间:{time_taken}秒")
   # print(f"硬件环境:cpu,推理执行时间:{time_taken}秒")

if __name__ == "__main__":
    main()

Model description

More information needed

Intended uses & limitations

More information needed

Training and evaluation data

More information needed

Training procedure

Training hyperparameters

The following hyperparameters were used during training:

  • learning_rate: 3e-05
  • train_batch_size: 32
  • eval_batch_size: 32
  • seed: 42
  • distributed_type: sagemaker_data_parallel
  • num_devices: 8
  • total_train_batch_size: 256
  • total_eval_batch_size: 256
  • optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
  • lr_scheduler_type: linear
  • lr_scheduler_warmup_steps: 500
  • num_epochs: 4
  • mixed_precision_training: Native AMP

Training results

Training LossEpochStepValidation LossAccuracy
0.36881.02640.14440.9564
0.15292.05280.15020.9518
0.1073.07920.13880.9530
0.06664.010560.14000.9644

Framework versions

  • Transformers 4.17.0
  • Pytorch 1.10.2+cu113
  • Datasets 1.18.4
  • Tokenizers 0.11.6