前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >TensorFlow支持Unicode,中文NLP终于省心了

TensorFlow支持Unicode,中文NLP终于省心了

作者头像
AI科技大本营
发布于 2018-12-28 07:42:51
发布于 2018-12-28 07:42:51
4.2K00
代码可运行
举报
运行总次数:0
代码可运行

整理 | 非主流 出品 | AI科技大本营

终于,TensorFlow 增加了对 Unicode 的支持。

什么是 Unicode?Unicode 是计算机科学领域里的一项业界标准,包括字符集、编码方案等。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。

在处理自然语言时,了解字符串中字符的编码方式非常重要。因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字。最早的计算机在设计时采用 8 个比特(bit)作为一个字节(byte),所以一个字节能表示的最大的整数就是 255(二进制 11111111 = 十进制 255),0 - 255 被用来表示大小写英文字母、数字和一些符号,这个编码表被称为 ASCII 编码,比如大写字母 A 的编码是 65,小写字母 z 的编码是 122。

如果要表示中文,显然一个字节是不够的,至少需要两个字节,而且还不能和 ASCII 编码冲突,所以,中国制定了 GB2312 编码,用来把中文编进去。

类似的,日文和韩文等其他语言也有这个问题。为了统一所有文字的编码,Unicode 应运而生。Unicode 把所有语言都统一到一套编码里,这样就不会再有乱码问题了。

Unicode 几乎支持所有的语言,是字符编码最常用的标准。Unicode 规定,每个字符使用唯一的整数代码点(code point)表示,其值介于 0 和 0x10FFFF 之间。把代码点按顺序放置,就能得到一个 Unicode 字符串。

因此,TensorFlow 支持 Unicode 对中文 NLP 的研究人员来说绝对算得上是一大利好。与此同时,TensorFlow 社区也推出了新的 Unicode colab 教程,展示了如何在 TensorFlow 中表示 Unicode 字符串。

在使用 TensorFlow 时,有两种标准方式来表示 Unicode 字符串:

  • 作为整数向量,其中每个位置包含一个代码点。
  • 作为字符串,使用字符编码将代码点序列编码到字符串中,包括最常见的 UTF-8、UTF-16 等字符编码。

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a vector of code points.
text_chars = tf.constant([35821, 35328, 22788, 29702])

# Unicode string, represented as a UTF-8-encoded string scalar.
text_utf8 = tf.constant('\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86')

# Unicode string, represented as a UTF-16-BE-encoded string scalar.
text_utf16be = tf.constant('\x8b\xed\x8a\x00Y\x04t\x06')```

当然,你可能经常需要在不同的表示之间进行转换,而 TensorFlow 1.13 已添加了执行此操作的函数:

  • tf.strings.unicode_decode:将编码的字符串标量转换为代码点的向量。
  • tf.strings.unicode_encode:将代码点向量转换为编码的字符串标量。
  • tf.strings.unicode_transcode:将编码的字符串标量转换为不同的编码。

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> tf.strings.unicode_decode(text_utf8, input_encoding='UTF-8')
tf.Tensor([35821 35328 22788 29702], shape=(4,), dtype=int32)

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> hello = [b"Hello", b"你好", b"こんにちは", b"Bonjour"]
>>> tf.strings.unicode_decode(hello, input_encoding='UTF-8')
<tf.RaggedTensor [[72, 101, 108, 108, 111],
                  [20320, 22909],
                  [12371, 12435, 12395, 12385, 12399],
                  [66, 111, 110, 106, 111, 117, 114]]>

以下是 Unicode 使用方法的详细介绍,希望对大家能有所帮助。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
!pip install -q tf-nightly
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from __future__ import absolute_import, division, print_function
import tensorflow as tf

tf.enable_eager_execution()

tf.string

通过基本的 TensorFlow tf.string dtype,你可以构建字节字符串的张量(tensor)。Unicode 字符串默认为 utf-8 编码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.constant(u"Thanks ?")
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=0, shape=(), dtype=string, numpy=b'Thanks \xf0\x9f\x98\x8a'>

tf.string 张量可以保存不同长度的字节串,因为字节串被视为原子单位。字符串长度不包括在张量尺寸中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
f.constant([u"You're", u"welcome!"]).shape
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
TensorShape([Dimension(2)])

Unicode 表示

在 TensorFlow 中有两种表示 Unicode 字符串的标准方法:

字符串标量,使用已知字符编码对代码点序列进行编码。 int32 vector,每个位置包含一个代码点。

例如,以下三个值都表示 Unicode 字符串“语言处理”。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a UTF-8 encoded string scalar.
text_utf8 = tf.constant(u"语言处理")
text_utf8

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=3, shape=(), dtype=string, numpy=b'\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86'>

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a UTF-16-BE encoded string scalar.
text_utf16be = tf.constant(u"语言处理".encode("UTF-16-BE"))
text_utf16be
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=5, shape=(), dtype=string, numpy=b'\x8b\xed\x8a\x00Y\x04t\x06'>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a vector of Unicode code points.
text_chars = tf.constant([ord(char) for char in u"语言处理"])
text_chars
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=7, shape=(4,), dtype=int32, numpy=array([35821, 35328, 22788, 29702], dtype=int32)>

表示之间的转换

TensorFlow 提供了在这些不同表示之间进行转换的操作:

  • tf.strings.unicode_decode:将编码的字符串标量转换为代码点的向量。
  • tf.strings.unicode_encode:将代码点向量转换为编码的字符串标量。
  • tf.strings.unicode_transcode:将编码的字符串标量转换为不同的编码。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_decode(text_utf8,
                          input_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=12, shape=(4,), dtype=int32, numpy=array([35821, 35328, 22788, 29702], dtype=int32)>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(text_chars,
                          output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=23, shape=(), dtype=string, numpy=b'\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86'>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_transcode(text_utf8,
                             input_encoding='UTF8',
                             output_encoding='UTF-16-BE')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=25, shape=(), dtype=string, numpy=b'\x8b\xed\x8a\x00Y\x04t\x06'>

Batch dimensions

解码多个字符串时,每个字符串中的字符数可能不相等。 其返回结果是 tf.RaggedTensor,其中最里面的维度的长度根据每个字符串中的字符数而变化。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# A batch of Unicode strings, each represented as a UTF8-encoded string.
batch_utf8 = [s.encode('UTF-8') for s in
              [u'hÃllo',  u'What is the weather tomorrow', u'Göödnight', u'?']]
              batch_chars_ragged = tf.strings.unicode_decode(batch_utf8,
                          input_encoding='UTF-8')
for sentence_chars in batch_chars_ragged.to_list():
print(sentence_chars)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[104, 195, 108, 108, 111]
[87, 104, 97, 116, 32, 105, 115, 32, 116, 104, 101, 32, 119, 101, 97, 116, 104, 101, 114, 32, 116, 111, 109, 111, 114, 114, 111, 119]
[71, 246, 246, 100, 110, 105, 103, 104, 116]
[128522]

你既可以直接使用 tf.RaggedTensor,也可以用 tf.RaggedTensor.to_tensor 将其转换为有填充的密集 tf.Tensor,或者用 tf.RaggedTensor.to_sparse 将其转换为 tf.SparseTensor。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
batch_chars_padded = batch_chars_ragged.to_tensor(default_value=-1)
print(batch_chars_padded.numpy())
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[[   104    195    108    108    111     -1     -1     -1     -1                       -1   -1     -1     -1     -1     -1     -1     -1     -1     -1     -1  -1     -1     -1     -1     -1     -1     -1     -1]
 [    87    104     97    116     32    105    115     32    116    104  101     32    119    101     97    116    104    101    114     32   116    111    109    111    114    114    111    119]
  [    71    246    246    100    110    105    103    104    116     -1  -1     -1     -1     -1     -1     -1     -1     -1     -1     -1  -1     -1     -1     -1     -1     -1     -1     -1]
  [128522     -1     -1     -1     -1     -1     -1     -1     -1     -1   -1     -1     -1     -1     -1     -1     -1     -1     -1     -1  -1     -1     -1     -1     -1     -1     -1     -1]]
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
batch_chars_sparse = batch_chars_ragged.to_sparse()

编码多个相同长度的字符串时,可以使用 tf.Tensor 作为输入:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode([[99, 97, 116], [100, 111, 103], [ 99, 111, 119]], output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=129, shape=(3,), dtype=string, numpy=array([b'cat', b'dog', b'cow'], dtype=object)>

编码多个不同长度的字符串时,应使用 tf.RaggedTensor 作为输入:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(batch_chars_ragged, output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=131, shape=(4,), dtype=string, numpy=array([b'h\xc3\x83llo', b'What is the weather tomorrow', b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>

如果你有一个带填充或稀疏格式的多个字符串的张量,那么在调用 unicode_encode 之前应将其转换为 tf.RaggedTensor:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(tf.RaggedTensor.from_sparse(batch_chars_sparse), output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=214, shape=(4,), dtype=string, numpy=array([b'h\xc3\x83llo', b'What is the weather tomorrow', b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(tf.RaggedTensor.from_tensor(batch_chars_padded, padding=-1), output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=288, shape=(4,), dtype=string, numpy=array([b'h\xc3\x83llo', b'What is the weather tomorrow', b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>

Unicode操作

字符长度

tf.strings.length 操作有一个参数单元,它指示应该如何计算长度。 unit 默认为“BYTE”,但可以设置为其他值,例如“UTF8_CHAR”或“UTF16_CHAR”,以确定每个编码字符串中的 Unicode代码点数。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Note that the final character takes up 4 bytes in UTF8.
thanks = u'Thanks ?'.encode('UTF-8')
num_bytes = tf.strings.length(thanks).numpy()
num_chars = tf.strings.length(thanks, unit='UTF8_CHAR').numpy()
print('{} bytes; {} UTF-8 characters'.format(num_bytes, num_chars))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
11 bytes; 8 UTF-8 characters

字符子串

类似地,tf.strings.substr 操作接受“unit”参数,并使用它来确定“pos”和“len”参数包含哪种偏移。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# default: unit='BYTE'. With len=1, we return a single byte.
tf.strings.substr(thanks, pos=7, len=1).numpy()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
b'\xf0'
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Specifying unit='UTF8_CHAR', we return a single character, which in this case
# is 4 bytes.
print(tf.strings.substr(thanks, pos=7, len=1, unit='UTF8_CHAR').numpy())
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
b'\xf0\x9f\x98\x8a'

拆分 Unicode 字符串

tf.strings.unicode_split 操作将 unicode 字符串拆分为单个字符的子字符串:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_split(thanks, 'UTF-8').numpy()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
array([b'T', b'h', b'a', b'n', b'k', b's', b' ', b'\xf0\x9f\x98\x8a'], dtype=object)

字符的字节偏移量

要将 tf.strings.unicode_decode 生成的字符张量与原始字符串对齐,了解每个字符开始位置的偏移量很非常有用。除了会返回包含每个字符的起始偏移量的第二张量,方法 tf.strings.unicode_decode_with_offsets 与 unicode_decode 非常类似。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
codepoints, offsets = tf.strings.unicode_decode_with_offsets(u"???", 'UTF-8')
for (codepoint, offset) in zip(codepoints.numpy(), offsets.numpy()):
print("At byte offset {}: codepoint {}".format(offset, codepoint))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
At byte offset 0: codepoint 127880
At byte offset 4: codepoint 127881
At byte offset 8: codepoint 127882

Unicode 脚本

每个 Unicode 代码点都属于一个称为脚本的代码点集合。字符的脚本有助于确定字符可能属于哪种语言。例如,我们知道'Б'是西里尔文字,那就表示包含该字符的现代文本可能来自斯拉夫语言,如俄语或乌克兰语。

TensorFlow 提供 tf.strings.unicode_script 操作以确定给定代码点使用哪个脚本。 脚本代码是对应于 International Components for Unicode(ICU)UScriptCode 值的 int32 值。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
uscript = tf.strings.unicode_script([33464, 1041])  # ['芸', 'Б']

print(uscript.numpy())  # [17, 8] == [USCRIPT_HAN, USCRIPT_CYRILLIC]
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[17  8]

tf.strings.unicode_script 操作也可以应用于代码点的多维 tf.Tensors 或 tf.RaggedTensors:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
print(tf.strings.unicode_script(batch_chars_ragged))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.RaggedTensor [[25, 25, 25, 25, 25], [25, 25, 25, 25, 0, 25, 25, 0, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 25], [25, 25, 25, 25, 25, 25, 25, 25, 25], [0]]>

参考链接: https://medium.com/tensorflow/adding-unicode-support-in-tensorflow-6a04fb983b63 https://www.tensorflow.org/tutorials/representation/unicode

(*本文由AI科技大本营整理,转载请联系微信1092722531)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-12-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI科技大本营 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
Unicode strings
处理自然语言的模型通常使用不同的字符集处理不同的语言。Unicode是一种标准编码系统,用于表示几乎所有语言的字符。每个字符都使用0到0x10FFFF之间的唯一整数编码点进行编码。Unicode字符串是由零个或多个代码点组成的序列。本教程展示了如何在TensorFlow中表示Unicode字符串,并使用标准字符串操作的Unicode等效项来操作它们。它基于脚本检测将Unicode字符串分隔为令牌。
狼啸风云
2022/09/30
2.5K0
Tensorflow2——Eager模式简介以及运用
使用过TensorFlow的大家都会知道, TF通过计算图将计算的定义和执行分隔开, 这是一种声明式(declaretive)的编程模型. 确实, 这种静态图的执行模式优点很多,但是在debug时确实非常不方便(类似于对编译好的C语言程序调用,此时是我们无法对其进行内部的调试), 因此有了Eager Execution, 这在TensorFlow v1.5首次引入. 引入的Eager Execution模式后, TensorFlow就拥有了类似于Pytorch一样动态图模型能力, 我们可以不必再等到see.run(*)才能看到执行结果, 可以方便在IDE随时调试代码,查看OPs执行结果. tf.keras封装的太好了 。不利于适用于自定义的循环与训练,添加自定义的循环 是一个命令式的编程环境,它使得我们可以立即评估操作产生的结果,而无需构建计算图。
Albert_xiong
2021/06/21
1.2K0
Tensorflow2——Eager模式简介以及运用
TensorFlow Plot (tfplot)
原文链接:https://tensorflow-plot.readthedocs.io/en/latest/api/index.html
狼啸风云
2019/08/18
1.5K0
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(九)
¹ Jasper Snoek 等人,“机器学习算法的实用贝叶斯优化”,《第 25 届国际神经信息处理系统会议论文集》2(2012):2951–2959。
ApacheCN_飞龙
2024/05/24
2090
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(九)
让Tensorflow直接输入字符串,无需额外词表的3种方法
tf.strings是很早就加入到tensorflow的内容,不过一直都很边缘,而且支持也不好,直到2.1/2.2版本才开始有越来越好的支持。
段清华DEAN
2020/06/10
1.3K0
tf_text
在文字的建模实践中,一般需要把原始文字拆解成单字、单词或者词组,然后将这些拆分的要素进行索引,标记化供机器学习算法使用。这种预处理叫做标注(Tokenize)。虽然这些功能都可以用python实现,但是Keras提供了现成的方法。
润森
2019/11/24
9560
让Tensorflow直接输入字符串,无需额外词表的3种方法
tf.strings是很早就加入到tensorflow的内容,不过一直都很边缘,而且支持也不好,直到2.1/2.2版本才开始有越来越好的支持。
段清华DEAN
2020/04/10
1.4K0
让Tensorflow直接输入字符串,无需额外词表的3种方法
Tensorflow2.0常用基础API
tensorflow2.0改进之后已经非常像numpy形式了,不用像之前的session那样操作,一些基本的操作如下。需要注意的店以及部分数据均写在代码注释中。
Mirza Zhao
2023/06/26
7880
【tensorflow2.0】张量数据结构
Tensorflow的基本数据结构是张量Tensor。张量即多维数组。Tensorflow的张量和numpy中的array很类似。
西西嘛呦
2020/08/26
5160
TensorFlow2 一小时学会基本操作 1
https://iamarookie.blog.csdn.net/article/details/117591977
润森
2022/09/22
3780
TensorFlow2 一小时学会基本操作 1
一看就懂的Tensorflow实战(Tensorflow入门)
数字手写体识别数据集,常用来作为Deep Learning入门的基础数据集。它有60000个训练样本集和10000个测试样本集,每个样本图像的宽高为 28 * 28。此数据集是以二进制存储的,不能直接以图像格式查看。
AI异构
2020/07/29
4160
TensorFlow 2.0 快速入门指南:第一部分
在本部分中,我们将介绍 TensorFlow 2.00 alpha。 我们将首先概述该机器学习生态系统的主要功能,并查看其使用示例。 然后我们将介绍 TensorFlow 的高级 Keras API。 我们将在本节结尾处研究人工神经网络技术。
ApacheCN_飞龙
2023/04/23
4.5K0
模型蒸馏-学习笔记
知识蒸馏(Knowledge Distillation)最早是Hinton 2014年在论文Dislillation the Knowledge in a Neural Network中提出的概念,主要思想是通过教师模型(teacher)来指导学生模型(student)的训练,将复杂、学习能力强的教师模型学到的特征表示“知识蒸馏”出来,传递给参数小、学习能力弱的学生模型,从而得到一个速度快、表达能力强的学生模型。
Johns
2022/04/26
8.5K0
模型蒸馏-学习笔记
TensorFlow2.0(6):利用data模块进行数据预处理
在整个机器学习过程中,除了训练模型外,应该就属数据预处理过程消耗的精力最多,数据预处理过程需要完成的任务包括数据读取、过滤、转换等等。为了将用户从繁杂的预处理操作中解放处理,更多地将精力放在算法建模上,TensorFlow中提供了data模块,这一模块以多种方式提供了数据读取、数据处理、数据保存等功能。本文重点是data模块中的Dataset对象。
统计学家
2019/12/23
2K0
Tensorflow技术点整理(二)
这里跟PyTorch不同的是序号定义的不同,PyTorch是上下定义位置,而Tensorflow是左右定义位置。
算法之名
2022/03/24
4150
Tensorflow技术点整理(二)
Tensorflow使用TFRecords和tf.Example
参考 tf.python_io.TFRecordWriter() - 云+社区 - 腾讯云
狼啸风云
2022/09/28
8820
Tensorflow使用TFRecords和tf.Example
TensorFlow2.X学习笔记(5)--TensorFlow中阶API之数据管道
Dataset数据结构应用非常灵活,因为它本质上是一个Sequece序列,其每个元素可以是各种类型,例如可以是张量,列表,字典,也可以是Dataset。
MiChong
2020/09/24
1.5K0
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第13章 使用TensorFlow加载和预处理数据
Data API还可以从现成的文件(比如CSV文件)、固定大小的二进制文件、使用TensorFlow的TFRecord格式的文件(支持大小可变的记录)读取数据。TFRecord是一个灵活高效的二进制格式,基于Protocol Buffers(一个开源二进制格式)。Data API还支持从SQL数据库读取数据。另外,许多开源插件也可以用来从各种数据源读取数据,包括谷歌的BigQuery。
SeanCheney
2019/12/16
3.4K0
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第13章 使用TensorFlow加载和预处理数据
TensorFlow2.0(5):张量限幅
maximum()用于限制最小值,也即是说,将一个tensor中小于指定值的元素替换为指定值:
统计学家
2019/12/23
1K0
TensorFlow2.0(2):数学运算
可以看出,对于基本运算加(+)、减(-)、点乘(*)、除(/)、地板除法(//)、取余(%),都是对应元素进行运算。
统计学家
2019/12/23
2K0
相关推荐
Unicode strings
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文