← 开源
JunnYu

RoFormer_pytorch

RoFormer V1 & V2 pytorch

Model DevelopmentArchitecturePython
在 GitHub 打开
增长势头
+024 小时新增 Star0.0%
527
Star
43
Fork
+1
本周
1
贡献者
创建于 2021-03-25 · 更新于 2026-10-12 · 今日第 17693 名
主要开发者

JunnYu/RoFormer_pytorch 排名第 17,693,有 527 个星标。

同类别比较

JunnYu/RoFormer_pytorch 排名第 17,693,此处有 8 个同类条目;znxlwm/pytorch-MNIST-CelebA-GAN-DCGAN 排名第 17,691。

同类别比较
名称排名指标
aamini/evidential-deep-learning17,677
语言:
Python
星标数:
528
分叉数:
99
简介:
Learn fast, scalable, and calibrated measures of uncertainty using neural networks!
hsahovic/poke-env17,682
语言:
Python
星标数:
528
分叉数:
147
简介:
Poke-env: Python Interface for Pokemon Showdown Bots
kevinlin311tw/caffe-cvprw1517,684
语言:
C++
星标数:
528
分叉数:
215
简介:
:heart::coffee: Deep Learning of Binary Hash Codes for Fast Image Retrieval (CVPRW15)
znxlwm/pytorch-MNIST-CelebA-GAN-DCGAN17,691
语言:
Python
星标数:
528
分叉数:
145
简介:
Pytorch implementation of Generative Adversarial Networks (GAN) and Deep Convolutional Generative Adversarial Networks (DCGAN) for MNIST and CelebA datasets
alibaba/gym-starcraft17,696
语言:
Python
星标数:
527
分叉数:
138
简介:
StarCraft environment for OpenAI Gym, based on Facebook's TorchCraft. (In progress)
anuragranj/cc17,697
语言:
Python
星标数:
527
分叉数:
62
简介:
Competitive Collaboration: Joint Unsupervised Learning of Depth, Camera Motion, Optical Flow and Motion Segmentation
castorini/honk17,699
语言:
Python
星标数:
527
分叉数:
120
简介:
PyTorch implementations of neural network models for keyword spotting
openai/iaf17,708
语言:
Python
星标数:
527
分叉数:
139
简介:
Code for reproducing key results in the paper "Improving Variational Inference with Inverse Autoregressive Flow"
关键参数与规格
关键参数与规格
参数值
语言Python
类别Model Development
子类别Architecture
星标数527
分叉数43
贡献者1
创建日期2021-03-25
更新日期2026-10-12
1 天星标数0
7 天星标数1
排名17,693
上次排名17,472
排名变化-221
7 天星标排名11,496
上次 7 天星标排名11,552
7 天星标排名变化56
总星标排名18,578
上次总星标排名18,554
总星标排名变化-24
国家China
README 摘录

PyTorch RoFormer 原版Tensorflow权重( chinese_roformer_L-12_H-768_A-12. zip (提取码:xy9x) chinese_roformer_L-6_H-384_A-6. zip (提取码:gy97) 已经转化为PyTorch权重 chinese_roformer_small.

常见问题

记录了多少星标和分叉?

JunnYu/RoFormer_pytorch 有 527 个星标和 43 个分叉。

仓库排名是多少?

JunnYu/RoFormer_pytorch 排名第 17,693。

记录的语言和类别是什么?

JunnYu/RoFormer_pytorch 有 527 个星标,语言为 Python,类别为 Model Development。

哪个仓库排名最接近?

JunnYu/RoFormer_pytorch 与 znxlwm/pytorch-MNIST-CelebA-GAN-DCGAN 比较,后者排名第 17,691,有 528 个星标。

README

PyTorch RoFormer

原版Tensorflow权重(https://github.com/ZhuiyiTechnology/roformer)

已经转化为PyTorch权重

安装

pip install roformer
或者
pip install git+https://github.com/JunnYu/RoFormer_pytorch.git

huggingface.co

https://huggingface.co/junnyu/roformer_chinese_small https://huggingface.co/junnyu/roformer_chinese_base

使用

import torch
from roformer import RoFormerModel, RoFormerTokenizer, TFRoFormerModel
tokenizer = RoFormerTokenizer.from_pretrained("junnyu/roformer_chinese_base")
pt_model = RoFormerModel.from_pretrained("junnyu/roformer_chinese_base")
tf_model = TFRoFormerModel.from_pretrained("junnyu/roformer_chinese_base",
                                           from_pt=True)
text = "这里基本保留了唐宋遗留下来的坊巷格局和大量明清古建筑,其中各级文保单位29处,被誉为“里坊制度的活化石”“明清建筑博物馆”!"
pt_inputs = tokenizer(text, return_tensors="pt")
tf_inputs = tokenizer(text, return_tensors="tf")
with torch.no_grad():
    pt_outputs = pt_model(**pt_inputs).last_hidden_state
print(pt_outputs.shape)
tf_outputs = tf_model(**tf_inputs, training=False).last_hidden_state
print(tf_outputs.shape)

MLM测试

import torch
import tensorflow as tf
from roformer import RoFormerForMaskedLM, RoFormerTokenizer, TFRoFormerForMaskedLM
text = "今天[MASK]很好,我[MASK]去公园玩。"
tokenizer = RoFormerTokenizer.from_pretrained("junnyu/roformer_chinese_base")
pt_model = RoFormerForMaskedLM.from_pretrained("junnyu/roformer_chinese_base")
tf_model = TFRoFormerForMaskedLM.from_pretrained(
    "junnyu/roformer_chinese_base", from_pt=True)
pt_inputs = tokenizer(text, return_tensors="pt")
tf_inputs = tokenizer(text, return_tensors="tf")
# pytorch
with torch.no_grad():
    pt_outputs = pt_model(**pt_inputs).logits[0]
pt_outputs_sentence = "pytorch: "
for i, id in enumerate(tokenizer.encode(text)):
    if id == tokenizer.mask_token_id:
        tokens = tokenizer.convert_ids_to_tokens(pt_outputs[i].topk(k=5)[1])
        pt_outputs_sentence += "[" + "||".join(tokens) + "]"
    else:
        pt_outputs_sentence += "".join(
            tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True))
print(pt_outputs_sentence)
# tf
tf_outputs = tf_model(**tf_inputs, training=False).logits[0]
tf_outputs_sentence = "tf: "
for i, id in enumerate(tokenizer.encode(text)):
    if id == tokenizer.mask_token_id:
        tokens = tokenizer.convert_ids_to_tokens(
            tf.math.top_k(tf_outputs[i], k=5)[1])
        tf_outputs_sentence += "[" + "||".join(tokens) + "]"
    else:
        tf_outputs_sentence += "".join(
            tokenizer.convert_ids_to_tokens([id], skip_special_tokens=True))
print(tf_outputs_sentence)
# pytorch: 今天[天气||天||心情||阳光||空气]很好,我[想||要||打算||准备||喜欢]去公园玩。
# tf:      今天[天气||天||心情||阳光||空气]很好,我[想||要||打算||准备||喜欢]去公园玩。

手动权重转换

python convert_roformer_original_tf_checkpoint_to_pytorch.py \
    --tf_checkpoint_path=xxxxxx/chinese_roformer_L-12_H-768_A-12/bert_model.ckpt \
    --roformer_config_file=pretrained_models/chinese_roformer_base/config.json \
    --pytorch_dump_path=pretrained_models/chinese_roformer_base/pytorch_model.bin

tf与pytorch精度对齐

small版本
bert4keras vs pytorch
mean diff : tensor(5.9108e-07)
max diff : tensor(5.7220e-06)
bert4keras vs tf2.0
mean diff : tensor(4.5976e-07)
max diff : tensor(3.5763e-06)

base版本
python compare_model.py
bert4keras vs pytorch
mean diff : tensor(4.3925e-07)
max diff : tensor(7.6294e-06)
bert4keras vs tf2.0
mean diff : tensor(3.4151e-07)
max diff : tensor(3.8147e-06)

中文情感分类(chnsenti)

 ![](figure/loss.png) 

结果

model chnsenti
tensorflow-NEZHA(base-wwm) 94.75
pytorch-NEZHA(base-wwm) 94.92
pytorch-RoFormer(base) 95.08

参考

https://github.com/pengming617/bert_classification

https://github.com/bojone/bert4keras

https://github.com/ZhuiyiTechnology/roformer

https://github.com/lonePatient/NeZha_Chinese_PyTorch

https://github.com/lonePatient/TorchBlocks

https://github.com/huggingface/transformers

排名相邻
同类项目