TensorFlow 支持 Unicode 编码

2018 年 12 月 26 日 引力空间站


文 / Laurence Moroney, Google TensorFlow 团队;Edward Loper, Google Research 团队





TensorFlow 现在可以支持 Unicode,这是一种标准编码系统,可以表示几乎所有语言的字符。处理自然语言时,了解字符的编码方式非常重要。在像英语这样的小字符集的语言中,每个字符都可以使用 ASCII 进行表示。但是这种方法对于其他语言来说并不实用,例如中文,这些语言有数千个字符。即使处理英文文本,Emojis 等特殊字符也不能用 ASCII 表示。


定义字符及其编码的最常用标准是 Unicode,它几乎支持所有语言。对于 Unicode,每个字符使用唯一的整数 code point 表示,其值介于 0 和 0x10FFFF 之间。当按顺序放置 code point 时,将形成 Unicode 字符串。


Unicode tutorial colab 展示了如何在 TensorFlow 中表示 Unicode 字符串。使用 TensorFlow 时,有两种标准方式来表示 Unicode 字符串:

  • 作为整数向量,其中每个位置包含单个 code point

  • 作为字符串,使用字符编码将 code point 序列编码到字符串中。有许多字符编码,其中一些最常见的是 UTF-8,UTF-16 等


以下代码分别使用 code point、UTF-8 和 UTF-16 显示字符串 “语言处理” 的编码。



当然,您可能需要在各种表示方式之间进行转换,而 TensorFlow 1.13 已添加了执行此操作的函数

  • tf.strings.unicode_decode: 将字符串标量转换为 code point 的向量(https://www.tensorflow.org/versions/r1.13/api_docs/python/tf/strings/unicode_decode)

  • tf.strings.unicode_encode: 将 code point 向量转换为字符串标量(https://www.tensorflow.org/versions/r1.13/api_docs/python/tf/strings/unicode_decode)

  • tf.strings.unicode_transcode: 将字符串标量转换为不同的编码(https://www.tensorflow.org/versions/r1.13/api_docs/python/tf/strings/unicode_transcode)


因此,如果要将上述示例中的 UTF-8 解码为 code point 向量,则可以执行以下操作:



当解码包含多个字符串的 Tensor 时,字符串可能具有不同的长度。 unicode_decode 将结果作为 RaggedTensor 返回,其中内部维度的长度根据每个字符串中的字符数而变化。



要了解有关 TensorFlow 中 Unicode 支持的更多信息,请查看 Unicode tutorial colab 并浏览 tf.strings 文档(https://www.tensorflow.org/tutorials/representation/unicode)




登录查看更多
2

相关内容

Google发布的第二代深度学习系统TensorFlow
Python导论,476页pdf,现代Python计算
专知会员服务
253+阅读 · 2020年5月17日
【Google】利用AUTOML实现加速感知神经网络设计
专知会员服务
28+阅读 · 2020年3月5日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
KGCN:使用TensorFlow进行知识图谱的机器学习
专知会员服务
80+阅读 · 2020年1月13日
深度神经网络模型压缩与加速综述
专知会员服务
127+阅读 · 2019年10月12日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
用 TensorFlow hub 在 Keras 中做 ELMo 嵌入
AI研习社
5+阅读 · 2019年5月12日
TensorFlow 2.0新特性之Ragged Tensor
深度学习每日摘要
18+阅读 · 2019年4月5日
Tensorflow框架是如何支持分布式训练的?
AI100
9+阅读 · 2019年3月26日
快使用TensorFlow,哼哼哈嘿
图灵教育
5+阅读 · 2018年3月16日
使用 TensorFlow 做文本情感分析
Datartisan数据工匠
15+阅读 · 2017年11月16日
A Probe into Understanding GAN and VAE models
Arxiv
9+阅读 · 2018年12月13日
Arxiv
4+阅读 · 2018年3月23日
Arxiv
3+阅读 · 2018年1月31日
VIP会员
相关VIP内容
Python导论,476页pdf,现代Python计算
专知会员服务
253+阅读 · 2020年5月17日
【Google】利用AUTOML实现加速感知神经网络设计
专知会员服务
28+阅读 · 2020年3月5日
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
KGCN:使用TensorFlow进行知识图谱的机器学习
专知会员服务
80+阅读 · 2020年1月13日
深度神经网络模型压缩与加速综述
专知会员服务
127+阅读 · 2019年10月12日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
相关资讯
用 TensorFlow hub 在 Keras 中做 ELMo 嵌入
AI研习社
5+阅读 · 2019年5月12日
TensorFlow 2.0新特性之Ragged Tensor
深度学习每日摘要
18+阅读 · 2019年4月5日
Tensorflow框架是如何支持分布式训练的?
AI100
9+阅读 · 2019年3月26日
快使用TensorFlow,哼哼哈嘿
图灵教育
5+阅读 · 2018年3月16日
使用 TensorFlow 做文本情感分析
Datartisan数据工匠
15+阅读 · 2017年11月16日
Top
微信扫码咨询专知VIP会员