当前位置: 首页 > news >正文

昇思25天学习打卡营第17天|文本解码原理--以MindNLP为例

文本解码就是根据当前已经输入的内容不断地预测下一个词,前期通过大量的文本文章等输入,让模型学习好以后,根据已学习的内容,不断预测下一个词。就像鹦鹉学舌一样你不断的叫他说你好大帅哥,你好大帅哥。后面某一天,当你说你好的时候,他会自然的接着说大帅哥。文本解码同理。
不过内容量会大很多,除了会说你好大帅哥,也会说你好大美女。那AI是怎么知道应该说哪个。他会看前文,因为我们喂给他文章里面,“女”这个词总是关联出现大美女,所以当前面出现女,接着说你好的时候,他就知道大美女的概率高于大帅哥,就是优先出现大帅哥。

import mindspore
from mindnlp.transformers import GPT2Tokenizer, GPT2LMHeadModeltokenizer = GPT2Tokenizer.from_pretrained("iiBcai/gpt2", mirror='modelscope')# add the EOS token as PAD token to avoid warnings
model = GPT2LMHeadModel.from_pretrained("iiBcai/gpt2", pad_token_id=tokenizer.eos_token_id, mirror='modelscope')# encode context the generation is conditioned on
input_ids = tokenizer.encode('I enjoy walking with my cute dog', return_tensors='ms')mindspore.set_seed(0)
# set top_k = 50 and set top_p = 0.95 and num_return_sequences = 3
sample_outputs = model.generate(input_ids,do_sample=True,max_length=50,top_k=5,top_p=0.95,num_return_sequences=3
)print("Output:\n" + 100 * '-')
for i, sample_output in enumerate(sample_outputs):print("{}: {}".format(i, tokenizer.decode(sample_output, skip_special_tokens=True)))
Output:
----------------------------------------------------------------------------------------------------
0: I enjoy walking with my cute dog."My dog loves the smell of the dog. I'm so happy that she's happy with me."I love to walk with my dog. I'm so happy that she's happy
1: I enjoy walking with my cute dog. I'm a big fan of my cat and her dog, but I don't have the same enthusiasm for her. It's hard not to like her because it is my dog.My husband, who
2: I enjoy walking with my cute dog, but I'm also not sure I would want my dog to walk alone with me."She also told The Daily Beast that the dog is very protective."I think she's very protective of

类似 这个示例,当输入I enjoy walking with my cute dog的时候,AI会一直续写下去,总体看上去,效果还是很不错的。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 迅狐抖音机构号授权矩阵系统源码
  • 从数字化营销与运营视角:看流量效果的数据分析
  • 重读AI金典算法模型-GPT系列
  • 【C++】C++中struct结构体和class类的区别
  • 解决:Flink向kafka写数据使用Producer精准一次(EXACTLY_ONCE)异常
  • 初学者必看的 3 个 Python 小项目
  • Oracle PL/SQL 循环批量执行存储过程
  • LT7911UX 国产原装 一拖三 edp 转LVDS 可旋转 可缩放
  • 汽车零配件行业看板管理系统应用
  • 算法简介:什么是算法?——定义、历史与应用详解
  • 使用 Vue.js 和 Element Plus 实现自动完成搜索功能
  • 【MySQL】6.表的增删查改(CURD)
  • Git协作
  • 预测算法面试
  • 【ARMv8/v9 GIC 系列 5.6 -- GIC 超优先级中断详细介绍】
  • android 一些 utils
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • flutter的key在widget list的作用以及必要性
  • github指令
  • JS 面试题总结
  • LeetCode29.两数相除 JavaScript
  • mockjs让前端开发独立于后端
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • php中curl和soap方式请求服务超时问题
  • Promise初体验
  • React-redux的原理以及使用
  • Redash本地开发环境搭建
  • SpringBoot几种定时任务的实现方式
  • 产品三维模型在线预览
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 利用DataURL技术在网页上显示图片
  • 如何进阶一名有竞争力的程序员?
  • 实战|智能家居行业移动应用性能分析
  • 使用common-codec进行md5加密
  • 走向全栈之MongoDB的使用
  • ​Spring Boot 分片上传文件
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • #Linux(Source Insight安装及工程建立)
  • $.ajax()方法详解
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (20050108)又读《平凡的世界》
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (附源码)springboot教学评价 毕业设计 641310
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (十三)Flask之特殊装饰器详解
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (转)h264中avc和flv数据的解析
  • (转)Sql Server 保留几位小数的两种做法
  • .naturalWidth 和naturalHeight属性,
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET 漏洞分析 | 某ERP系统存在SQL注入
  • .netcore 6.0/7.0项目迁移至.netcore 8.0 注意事项
  • .NetCore+vue3上传图片 Multipart body length limit 16384 exceeded.
  • /usr/bin/perl:bad interpreter:No such file or directory 的解决办法