1、tf.reduce_max函數的作用:計算張量的各個維度上的元素的最大值。例子:
import tensorflow as tf
max_value = tf.reduce_max([1, 3, 2])
with tf.Session() as sess:
max_value = sess.run(max_value)
print(max_value)
結果為3
2、tf.sequence_mask的作用是構建序列長度的mask標志 。 例子:
import tensorflow as tf
mask = tf.sequence_mask([1, 3, 2], 5)
with tf.Session() as sess:
mask = sess.run(mask)
print(mask)
結果是:
[[ True False False False False]
[ True True True False False]
[ True True False False False]]
3、兩個函數結合使用:
# 根據目標序列長度,選出其中最大值,然后使用該值構建序列長度的mask標志,代碼:
import tensorflow as tf
max_value = tf.reduce_max([1, 3, 2])
mask = tf.sequence_mask([1, 3, 2], max_value)
with tf.Session() as sess:
mask = sess.run(mask)
print(mask)
結果是:
[[ True False False]
[ True True True]
[ True True False]]
---------------------
作者:qq_28808697
來源:CSDN
原文:https://blog.csdn.net/qq_28808697/article/details/80648657
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!