当前位置:网站首页>tf.keras.utils.pad_sequences()
tf.keras.utils.pad_sequences()
2022-07-31 05:34:00 【Erosion_ww】
Action:
Normalize length
This function converts a list (of length num_samples) of sequences (lists of integers) to a shape of (num_samples, num_timesteps) 2D Numpy array.num_timesteps is the maxlen parameter (if provided), or the longest sequence in the listlength.
shorter than num_timestepsThe sequences are filled with values until they are num_timesteps.
Longer than num_timestepsThe sequence will be truncated to fit the desired length.
The position where padding or truncation occurs is determined by parameter padding and truncation, respectively.Prefilling or removing values from the beginning of the sequence is the default.
parameter
tf.keras.utils.pad_sequences(sequences, # sequence lengthmaxlen=None, # optional Int, maximum length of all sequences.If not provided, the sequence will be padded to the length of the longest single sequence.dtype='int32', # optional, defaults to "int32".Type of output sequence.To pad a sequence with variable-length strings, you can use object.padding='pre', # string, "pre" or "post" (optional, defaults to "pre"): padding before or after each sequence.truncating='pre', # string, "pre" or "post" (optional, defaults to "pre"): Remove values from sequences greater than maxlen, whether at the beginning or end of the sequence.value=0.0 # float or string, fill value.(Optional, defaults to 0.))
Return value
Numpy array with shape (len(sequences), maxlen)
Example
import tensorflow as tf # import tensorflowsequence = [[1], [2, 3], [4, 5, 6]] # input sequencetf.keras.preprocessing.sequence.pad_sequences(sequence) # length normalization
array([[0, 0, 1],[0, 2, 3],[4, 5, 6]])
import tensorflow as tf # import tensorflowsequence = [[1], [2, 3], [4, 5, 6]] # input sequencetf.keras.preprocessing.sequence.pad_sequences(sequence, padding='post') # length normalization
array([[1, 0, 0],[2, 3, 0],[4, 5, 6]])
Main reference: tf.keras.utils.pad_sequences | TensorFlow Core v2.9.1 (google.cn)
边栏推荐
猜你喜欢
随机推荐
分布式事务处理方案大 PK!
C语言如何分辨大小端
Flask-based three-party login process
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
初涉C语言
如何将项目部署到服务器上(全套教程)
剑指offer专项突击版 ---第 5 天
面试官问我TCP三次握手和四次挥手,我真的是
“档次法”——用于物品体积分布不均匀的01背包问题的求解方法
实验7 UDP与TCP对比
剑指offer专项突击版 --- 第 4 天
Temporal客户端模型
【一起学Rust】Rust的Hello Rust详细解析
【C语言趣味小游戏——猜数字】
MYSQL一站式学习,看完即学完
C语言实验一 熟悉C程序的环境
Flink sink ES 写入 ES(带密码)
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
剑指offer专项突击版 ---- 第1天
第7章 网络层第3次练习题答案(第三版)