TensorFlow 2.0新特性之Ragged Tensor

2019 年 4 月 5 日 深度学习每日摘要

在处理很多机器学习现实任务的时候,我们常常会面临形状分布不固定的Tensor,比如,在处理文本分类任务中,一句话中包含的单词的数目是可变的,在语音识别中,输入的音频的长度是可变的,在语音合成中,输入的文本的长度是可变的。一般这个时候,我们需要设定一个固定的长度,对于过长的输入,就进行截断;而对于过短的输入,就用特殊数值补齐。

在TF 2.0中,我们迎来了一个新的解决方法,那就是今天要介绍的Ragged Tensor,例如对于一段文本,我们可以按照如下方式来定义:

1
2
3
speech = tf.ragged.constant(
[[‘今', ‘天', ‘天', ‘气', '很', ‘好'],
[‘我’, ‘很’, ‘好']])


在这里,speech是一个RaggedTensor,那么大家可能会比较疑惑,这种变量是否支持加减乘除等一系列数学运算呢?好消息是,RaggedTensor支持非常多常用的数学运算,具体的可以参见https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/ragged?hl=en。除此之外,RaggedTensor还支持Python风格的索引,例如,当我们取speech的第一个元素的时候speech[0],是这样的:

1
tf.Tensor([‘今', ‘天', ‘天', ‘气', '很', ‘好’], shape=(6,), dtype=string)


除此之外,tf.ragged包还包含了一些针对RaggedTensor特别进行的操作,例如tf.ragged.map_flat_values可以对RaggedTensor中的变量进行操作,并且返回值的形状还是输入RaggedTensor的形状。

需要说明的是,RaggedTensor可能听起来和SparseTensor无异,但是其实是有很大区别的,SparseTensor在计算的时候需要照顾到它的Dense Tensor的坐标轴,而RaggedTensor则只需考虑自己的行列即可,这一点在各自的加法上面可以看出来。

最后看一段RaggedTensor的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import math
import tensorflow as tf
tf.enable_eager_execution()
# Set up the embeddingss
num_buckets = 1024
embedding_size = 16
embedding_table =
tf.Variable(
tf.truncated_normal([num_buckets, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)),
name="embedding_table")
# Input tensor.
queries = tf.ragged.constant([
['Who', 'is', 'Dan', 'Smith']
['Pause'],
['Will', 'it', 'rain', 'later', 'today']])
# Look up embedding for each word. map_flat_values applies an operation to each value in a RaggedTensor.
word_buckets = tf.strings.to_hash_bucket_fast(queries, num_buckets)
word_embeddings = tf.ragged.map_flat_values(
tf.nn.embedding_lookup, embedding_table, word_buckets) # ①
# Add markers to the beginning and end of each sentence.
marker = tf.fill([queries.nrows()), 1], '#')
padded = tf.concat([marker, queries, marker], axis=1) # ②
# Build word bigrams & look up embeddings.
bigrams = tf.string_join(
[padded[:, :-1], padded[:, 1:]], separator='+') # ③
bigram_buckets =
tf.strings.to_hash_bucket_fast(bigrams, num_buckets)
bigram_embeddings = tf.ragged.map_flat_values(
tf.nn.embedding_lookup, embedding_table, bigram_buckets) # ④
# Find the average embedding for each sentence
all_embeddings =
tf.concat([word_embeddings, bigram_embeddings], axis=1) # ⑤
avg_embedding = tf.reduce_mean(all_embeddings, axis=1) # ⑥
print(word_embeddings)
print(bigram_embeddings)
print(all_embeddings)
print(avg_embedding)


登录查看更多
18

相关内容

TensorFlow 2.0中有多处更改,让用户使用更高效。TensorFlow 2.0删除冗余 APIs,使API更加一致(统一 RNNs,统一优化器),并通过Eager execution模式更好地与Python运行时集成。
TensorFlow Lite指南实战《TensorFlow Lite A primer》,附48页PPT
专知会员服务
68+阅读 · 2020年1月17日
KGCN:使用TensorFlow进行知识图谱的机器学习
专知会员服务
80+阅读 · 2020年1月13日
【模型泛化教程】标签平滑与Keras, TensorFlow,和深度学习
专知会员服务
20+阅读 · 2019年12月31日
【干货】谷歌Joshua Gordon 《TensorFlow 2.0讲解》,63页PPT
专知会员服务
24+阅读 · 2019年11月2日
TensorFlow 2.0 学习资源汇总
专知会员服务
66+阅读 · 2019年10月9日
社区分享 | Spark 玩转 TensorFlow 2.0
TensorFlow
15+阅读 · 2020年3月18日
TF - GAN入门:TensorFlow 2.0 的轻量级 GAN 库
新智元
5+阅读 · 2019年10月8日
官方解读:TensorFlow 2.0 新的功能特性
云头条
3+阅读 · 2019年1月23日
终于!TensorFlow引入了动态图机制Eager Execution
深度学习世界
5+阅读 · 2017年11月1日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
干货| PyTorch相比TensorFlow,存在哪些自身优势?
全球人工智能
15+阅读 · 2017年10月4日
教程 | 如何从TensorFlow转入PyTorch
机器之心
7+阅读 · 2017年9月30日
Self-Attention Graph Pooling
Arxiv
13+阅读 · 2019年6月13日
Arxiv
8+阅读 · 2018年11月27日
Arxiv
3+阅读 · 2018年5月20日
Arxiv
3+阅读 · 2018年1月31日
VIP会员
相关资讯
社区分享 | Spark 玩转 TensorFlow 2.0
TensorFlow
15+阅读 · 2020年3月18日
TF - GAN入门:TensorFlow 2.0 的轻量级 GAN 库
新智元
5+阅读 · 2019年10月8日
官方解读:TensorFlow 2.0 新的功能特性
云头条
3+阅读 · 2019年1月23日
终于!TensorFlow引入了动态图机制Eager Execution
深度学习世界
5+阅读 · 2017年11月1日
手把手教TensorFlow(附代码)
深度学习世界
15+阅读 · 2017年10月17日
干货| PyTorch相比TensorFlow,存在哪些自身优势?
全球人工智能
15+阅读 · 2017年10月4日
教程 | 如何从TensorFlow转入PyTorch
机器之心
7+阅读 · 2017年9月30日
Top
微信扫码咨询专知VIP会员