junnyu/roformer_v2_chinese_char_base

Model

6

stars

15

commits

4

linked in READMEs

May 11, 2022

updated

fill-mask
pytorch
roformer
roformer-v2
tf2.0
transformers

README

介绍

tf版本

https://github.com/ZhuiyiTechnology/roformer-v2

pytorch版本+tf2.0版本

https://github.com/JunnYu/RoFormer_pytorch

安装

  • pip install roformer==0.4.3

评测对比

CLUE-dev榜单分类任务结果,base+large版本。

iflytektnewsafqmccmnliocnliwsccsl
BERT60.0656.8072.4179.5673.9378.6283.93
RoBERTa60.6458.0674.0581.2476.0087.5084.50
RoFormer60.9157.5473.5280.9276.0786.8484.63
RoFormerV2*60.8756.5472.7580.3475.3680.9284.67
GAU-α61.4157.7674.1781.8275.8679.9385.67
RoFormer-pytorch(本仓库代码)60.6057.5174.4480.7975.6786.8484.77
RoFormerV2-pytorch(本仓库代码)62.8759.0376.2080.8579.7387.8291.87
GAU-α-pytorch(Adafactor)61.1857.5273.4280.9175.6980.5985.5
GAU-α-pytorch(AdamW wd0.01 warmup0.1)60.6857.9573.0881.0275.3681.2583.93
RoFormerV2-large-pytorch(本仓库代码)61.7559.2176.1482.3581.7391.4591.5
Chinesebert-large-pytorch61.2558.6774.7082.6579.6387.8384.97

CLUE-1.0-test榜单分类任务结果,base+large版本。

iflytektnewsafqmccmnliocnliwsccsl
RoFormer-pytorch(本仓库代码)59.5457.3474.4680.2373.6780.6984.57
RoFormerV2-pytorch(本仓库代码)63.1558.2475.4280.5974.1783.7983.73
GAU-α-pytorch(Adafactor)61.3857.0874.0580.3773.5374.8385.6
GAU-α-pytorch(AdamW wd0.01 warmup0.1)60.5457.6772.4480.3272.9776.5584.13
RoFormerV2-large-pytorch(本仓库代码)61.8559.1376.3880.9776.2385.8684.33
Chinesebert-large-pytorch61.5458.5774.881.9476.9379.6685.1

注:

  • 其中RoFormerV2*表示的是未进行多任务学习的RoFormerV2模型,该模型苏神并未开源,感谢苏神的提醒。
  • 其中不带有pytorch后缀结果都是从GAU-alpha仓库复制过来的。
  • 其中带有pytorch后缀的结果都是自己训练得出的。
  • 苏神代码中拿了cls标签后直接进行了分类,而本仓库使用了如下的分类头,多了2个dropout,1个dense,1个relu激活。
class RoFormerClassificationHead(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.out_proj = nn.Linear(config.hidden_size, config.num_labels)

        self.config = config

    def forward(self, features, **kwargs):
        x = features[:, 0, :]  # take <s> token (equiv. to [CLS])
        x = self.dropout(x)
        x = self.dense(x)
        x = ACT2FN[self.config.hidden_act](x) # 这里是relu
        x = self.dropout(x)
        x = self.out_proj(x)
        return x

pytorch & tf2.0使用

import torch
import tensorflow as tf
from transformers import BertTokenizer
from roformer import RoFormerForMaskedLM, TFRoFormerForMaskedLM

text = "今天[MASK]很好,我[MASK]去公园玩。"
tokenizer = BertTokenizer.from_pretrained("junnyu/roformer_v2_chinese_char_base")
pt_model = RoFormerForMaskedLM.from_pretrained("junnyu/roformer_v2_chinese_char_base")
tf_model = TFRoFormerForMaskedLM.from_pretrained(
    "junnyu/roformer_v2_chinese_char_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)
# small
# pytorch: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
# tf: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
# base
# pytorch: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
# tf: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
# large
# pytorch: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。
# tf: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。

引用

Bibtex:

@misc{su2021roformer,
      title={RoFormer: Enhanced Transformer with Rotary Position Embedding}, 
      author={Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},
      year={2021},
      eprint={2104.09864},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
@techreport{roformerv2,
  title={RoFormerV2: A Faster and Better RoFormer - ZhuiyiAI},
  author={Jianlin Su, Shengfeng Pan, Bo Wen, Yunfeng Liu},
  year={2022},
  url="https://github.com/ZhuiyiTechnology/roformer-v2",
}

Contributors

junnyu

15 commits

junnyu/roformer_v2_chinese_char_base

Model

6

stars

15

commits

4

linked in READMEs

May 11, 2022

updated

fill-mask
pytorch
roformer
roformer-v2
tf2.0
transformers

README

介绍

tf版本

https://github.com/ZhuiyiTechnology/roformer-v2

pytorch版本+tf2.0版本

https://github.com/JunnYu/RoFormer_pytorch

安装

  • pip install roformer==0.4.3

评测对比

CLUE-dev榜单分类任务结果,base+large版本。

iflytektnewsafqmccmnliocnliwsccsl
BERT60.0656.8072.4179.5673.9378.6283.93
RoBERTa60.6458.0674.0581.2476.0087.5084.50
RoFormer60.9157.5473.5280.9276.0786.8484.63
RoFormerV2*60.8756.5472.7580.3475.3680.9284.67
GAU-α61.4157.7674.1781.8275.8679.9385.67
RoFormer-pytorch(本仓库代码)60.6057.5174.4480.7975.6786.8484.77
RoFormerV2-pytorch(本仓库代码)62.8759.0376.2080.8579.7387.8291.87
GAU-α-pytorch(Adafactor)61.1857.5273.4280.9175.6980.5985.5
GAU-α-pytorch(AdamW wd0.01 warmup0.1)60.6857.9573.0881.0275.3681.2583.93
RoFormerV2-large-pytorch(本仓库代码)61.7559.2176.1482.3581.7391.4591.5
Chinesebert-large-pytorch61.2558.6774.7082.6579.6387.8384.97

CLUE-1.0-test榜单分类任务结果,base+large版本。

iflytektnewsafqmccmnliocnliwsccsl
RoFormer-pytorch(本仓库代码)59.5457.3474.4680.2373.6780.6984.57
RoFormerV2-pytorch(本仓库代码)63.1558.2475.4280.5974.1783.7983.73
GAU-α-pytorch(Adafactor)61.3857.0874.0580.3773.5374.8385.6
GAU-α-pytorch(AdamW wd0.01 warmup0.1)60.5457.6772.4480.3272.9776.5584.13
RoFormerV2-large-pytorch(本仓库代码)61.8559.1376.3880.9776.2385.8684.33
Chinesebert-large-pytorch61.5458.5774.881.9476.9379.6685.1

注:

  • 其中RoFormerV2*表示的是未进行多任务学习的RoFormerV2模型,该模型苏神并未开源,感谢苏神的提醒。
  • 其中不带有pytorch后缀结果都是从GAU-alpha仓库复制过来的。
  • 其中带有pytorch后缀的结果都是自己训练得出的。
  • 苏神代码中拿了cls标签后直接进行了分类,而本仓库使用了如下的分类头,多了2个dropout,1个dense,1个relu激活。
class RoFormerClassificationHead(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.out_proj = nn.Linear(config.hidden_size, config.num_labels)

        self.config = config

    def forward(self, features, **kwargs):
        x = features[:, 0, :]  # take <s> token (equiv. to [CLS])
        x = self.dropout(x)
        x = self.dense(x)
        x = ACT2FN[self.config.hidden_act](x) # 这里是relu
        x = self.dropout(x)
        x = self.out_proj(x)
        return x

pytorch & tf2.0使用

import torch
import tensorflow as tf
from transformers import BertTokenizer
from roformer import RoFormerForMaskedLM, TFRoFormerForMaskedLM

text = "今天[MASK]很好,我[MASK]去公园玩。"
tokenizer = BertTokenizer.from_pretrained("junnyu/roformer_v2_chinese_char_base")
pt_model = RoFormerForMaskedLM.from_pretrained("junnyu/roformer_v2_chinese_char_base")
tf_model = TFRoFormerForMaskedLM.from_pretrained(
    "junnyu/roformer_v2_chinese_char_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)
# small
# pytorch: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
# tf: 今天[的||,||是||很||也]很好,我[要||会||是||想||在]去公园玩。
# base
# pytorch: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
# tf: 今天[我||天||晴||园||玩]很好,我[想||要||会||就||带]去公园玩。
# large
# pytorch: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。
# tf: 今天[天||气||我||空||阳]很好,我[又||想||会||就||爱]去公园玩。

引用

Bibtex:

@misc{su2021roformer,
      title={RoFormer: Enhanced Transformer with Rotary Position Embedding}, 
      author={Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu},
      year={2021},
      eprint={2104.09864},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
@techreport{roformerv2,
  title={RoFormerV2: A Faster and Better RoFormer - ZhuiyiAI},
  author={Jianlin Su, Shengfeng Pan, Bo Wen, Yunfeng Liu},
  year={2022},
  url="https://github.com/ZhuiyiTechnology/roformer-v2",
}

Contributors

junnyu

15 commits