Coder Social home page Coder Social logo

cpm-tf2transformer's Introduction

CPM-TF2Transformer

CPM Fot Transformer

参考了 qhduan/CPM-LM-TF2 的转换代码, 转化成 huggingface 社区 transformersTFGPT2LMHeadModel

CPM 原REPO:https://github.com/TsinghuaAI/CPM-Generate

原项目首页:https://cpm.baai.ac.cn/

或者可以直接在colab上运行试试效果 Build

例子

1. 依赖


pip install transformers
pip install jieba #因为原作者在做sentenceprice对文本进行了jieba分词

2. 初始化模型

模型有 FP32FP16 两个版本, FP16占用内存小但是在CPU上会比较慢

  • (FP32)
from transformers import TFGPT2LMHeadModel

model = TFGPT2LMHeadModel.from_pretrained("mymusise/CPM-GPT2")
  • (FP16)
from transformers import TFGPT2LMHeadModel

model = TFGPT2LMHeadModel.from_pretrained("mymusise/CPM-GPT2-FP16")

3. 文本预处理(仿照原作的处理)

原repo对encode和decode方法做了些特殊处理, 下面仿照原来的方法对 transformers.XLNetTokenizer 修改.

from transformers import XLNetTokenizer

class XLNetTokenizer(XLNetTokenizer):
    translator = str.maketrans(" \n", "\u2582\u2583")

    def _tokenize(self, text, *args, **kwargs):
        text = [x.translate(self.translator) for x in jieba.cut(text, cut_all=False)]
        text = " ".join(text)
        return super()._tokenize(text, *args, **kwargs)

    def _decode(self, *args, **kwargs):
        text = super()._decode(*args, **kwargs)
        text = text.replace(' ', '').replace('\u2582', ' ').replace('\u2583', '\n')
        return text
  • FP32
tokenizer = XLNetTokenizer.from_pretrained('mymusise/CPM-GPT2')
  • FP16
tokenizer = XLNetTokenizer.from_pretrained('mymusise/CPM-GPT2-FP16')

4. 文本生成

from transformers import TextGenerationPipeline
import jieba

text_generater = TextGenerationPipeline(model, tokenizer)

texts = [
    '今天天气不错',
    '天下武功, 唯快不',
    """
    我们在火星上发现了大量的神奇物种。有神奇的海星兽,身上是粉色的,有5条腿;有胆小的猫猫兽,橘色,有4条腿;有令人恐惧的蜈蚣兽,全身漆黑,36条腿;有纯洁的天使兽,全身洁白无瑕,有3条腿;有贪吃的汪汪兽,银色的毛发,有5条腿;有蛋蛋兽,紫色,8条腿。

    请根据上文,列出一个表格,包含物种名、颜色、腿数量。
    |物种名|颜色|腿数量|
    |亚古兽|金黄|2|
    |海星兽|粉色|5|
    |猫猫兽|橘色|4|
    |蜈蚣兽|漆黑|36|
    """
]

for text in texts:
    token_len = len(tokenizer._tokenize(text))
    print(text_generater(text, max_length=token_len + 15, top_k=1, use_cache=True, prefix='')[0]['generated_text'])
    print(text_generater(text, max_length=token_len + 15, do_sample=True, top_k=5)[0]['generated_text'])

5. 效果:

avatar

Changelog

  • 2021-02-24: 修复模型转换问题, 更新decode和encode方法

cpm-tf2transformer's People

Contributors

mymusise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cpm-tf2transformer's Issues

FP32 和 FP16模型转化

您好,请问FP32 和 FP16两个版本的模型是怎样得到的呢?利用transfor_CPM.py文件得到的模型对应哪个文件呢?
请指点下如何分别得到不同版本模型

请问还有原版的distill模型嘛

虽然转换的模型的权重和原版应该不会相差太大,但还是希望拿原版的也做一下测试,但是官方的链接失效了,不知道大佬有没有留存的原版模型文件可以分享?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.