Skip to Content
《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版
book

《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

by Aurélien Géron
May 2025
Intermediate to advanced
864 pages
12h 32m
Chinese
O'Reilly Media, Inc.
Content preview from 《使用 Scikit-Learn、Keras 和 TensorFlow 进行实践机器学习》第三版

附录 C. 特殊数据结构

在本附录中,除了常规的浮点或整数张量外,我们将快速了解 TensorFlow 支持的数据结构。其中包括字符串、锯齿张量、稀疏张量、张量数组、集合和队列。

弦乐

张量可以保存字节字符串,,这对自然语言处理尤其有用(见第 16 章):

>>> tf.constant(b"hello world")
<tf.Tensor: shape=(), dtype=string, numpy=b'hello world'>

如果尝试使用 Unicode 字符串构建张量,TensorFlow 会自动将其编码为 UTF-8:

>>> tf.constant("café")
<tf.Tensor: shape=(), dtype=string, numpy=b'caf\xc3\xa9'>

还可以创建代表 Unicode 字符串的张量。只需创建一个 32 位整数数组,每个整数代表一个 Unicode 代码点。1

>>> u = tf.constant([ord(c) for c in "café"])
>>> u
<tf.Tensor: shape=(4,), [...], numpy=array([ 99,  97, 102, 233], dtype=int32)>
备注

在tf.string 类型的张量中,字符串长度不是张量形状的一部分。换句话说,字符串被视为原子值。但是,在 Unicode 字符串张量(即 int32 张量)中,字符串的长度是张量形状的一部分。

tf.strings 软件包包含多个用于处理字符串张量的函数,例如length() 用于计算字节字符串中的字节数(如果设置了unit="UTF8_CHAR" ,则计算代码点数),unicode_encode() 用于将 Unicode 字符串张量(即 int32 张量)转换为字节字符串张量,以及unicode_decode() 用于反向操作:

>>> b = tf.strings.unicode_encode(u, "UTF-8")
>>> b
<tf.Tensor: shape=(), dtype=string, numpy=b'caf\xc3\xa9'>
>>> tf.strings.length(b, unit="UTF8_CHAR")
<tf.Tensor: shape=(), dtype=int32, numpy=4>
>>> tf.strings.unicode_decode(b, "UTF-8")
<tf.Tensor: shape=(4,), [...], numpy=array([ 99,  97, 102, 233], dtype=int32)>

您还可以操作包含多个字符串的张量:

>>> p = tf.constant(["Café", "Coffee", "caffè", "咖啡"])
>>> tf.strings.length(p, unit="UTF8_CHAR")
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([4, 6, 5, 2], dtype=int32)>
>>> r = tf.strings.unicode_decode(p, "UTF8")
>>> r
<tf.RaggedTensor [[67, 97, 102, 233], [67, 111, 102, 102, 101, 101], [99, 97,
102, 102, 232], [21654, 21857]]>

请注意,解码后的字符串存储在 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
学习 API 风格

学习 API 风格

Lukasz Dynowski, Marcin Dulak
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656598