当前位置:网站首页>tf. sequence_ Mask function explanation case

tf. sequence_ Mask function explanation case

2022-07-05 16:41:00 Modest and prosperous

One  TensorFlow  Print vector values

When printing directly  tf  The contents of variables , Only dimensions can be output , You cannot view the results directly

Method 1 :

import tensorflow as tf
x = tf.constant(1)
with tf.Session() as sess:
	print(sess.run(x))

  Method 2 :

import tensorflow as tf
x = tf.constant(1)
sess = tf.InteractiveSession()
print(x.eval() )

Two  tf.sequence_mask() function

  • lengths  Integral tensor , All its values <= maxlen.
  • maxlen  Scalar integer tensor , Returns the size of the last dimension of the tensor . The default value is  lengths  Maximum of .
  • dtype  The output type of the result tensor .
  • name  Name of operation .

situation 1:length What is passed in is a scalar

import tensorflow as tf
length = 3
seq_mask = tf.sequence_mask(length)
seq_mask
Out[6]: <tf.Tensor 'SequenceMask/Less:0' shape=(?,) dtype=bool>
tf.InteractiveSession()
print(seq_mask.eval())
[ True  True  True]
seq_mask = tf.sequence_mask(lengths=length)
print(seq_mask.eval())
[ True  True  True]
max_mask = tf.sequence_mask(lengths=length, maxlen=5)
max_mask
Out[12]: <tf.Tensor 'SequenceMask_2/Less:0' shape=(5,) dtype=bool>
max_mask.eval()
Out[13]: array([ True,  True,  True, False, False])

situation 2: Incoming one-dimensional list , According to the number of one-dimensional lists  shape

embed_dim = [2, 3, 4]
embed_mask = tf.sequence_mask(lengths=embed_dim)
embed_mask
Out[16]: <tf.Tensor 'SequenceMask_3/Less:0' shape=(3, ?) dtype=bool>
embed_mask.eval()
Out[17]: 
array([[ True,  True, False, False],
       [ True,  True,  True, False],
       [ True,  True,  True,  True]])
max_embed_mask = tf.sequence_mask(lengths=embed_dim, maxlen=6)
max_embed_mask
Out[19]: <tf.Tensor 'SequenceMask_4/Less:0' shape=(3, 6) dtype=bool>
max_embed_mask.eval()
Out[20]: 
array([[ True,  True, False, False, False, False],
       [ True,  True,  True, False, False, False],
       [ True,  True,  True,  True, False, False]]) 
 

If the list element is too long ——

batch_data = [4, 3,  5, 6]
batch_seq = tf.sequence_mask(lengths=batch_data)
batch_seq
Out[72]: <tf.Tensor 'SequenceMask_17/Less:0' shape=(4, ?) dtype=bool>
batch_seq.eval()
Out[73]: 
array([[ True,  True,  True,  True, False, False],
       [ True,  True,  True, False, False, False],
       [ True,  True,  True,  True,  True, False],
       [ True,  True,  True,  True,  True,  True]])
max_batch_mask = tf.sequence_mask(lengths=batch_data, maxlen=7)
max_batch_mask
Out[75]: <tf.Tensor 'SequenceMask_18/Less:0' shape=(4, 7) dtype=bool>
max_batch_mask.eval()
Out[76]: 
array([[ True,  True,  True,  True, False, False, False],
       [ True,  True,  True, False, False, False, False],
       [ True,  True,  True,  True,  True, False, False],
       [ True,  True,  True,  True,  True,  True, False]])

Conclusion : The last dimension is expanded

situation 3:length  Multiple lists are passed in  

length_list = [ [2, 3, 4], [3, 4, 5]]
list_mask = tf.sequence_mask(lengths=length_list)
list_mask
Out[64]: <tf.Tensor 'SequenceMask_15/Less:0' shape=(2, 3, ?) dtype=bool>
list_mask.eval()
Out[65]: 
array([[[ True,  True, False, False, False],
        [ True,  True,  True, False, False],
        [ True,  True,  True,  True, False]],
       [[ True,  True,  True, False, False],
        [ True,  True,  True,  True, False],
        [ True,  True,  True,  True,  True]]])
max_list_mask = tf.sequence_mask(lengths=length_list, maxlen=7)
max_list_mask
Out[68]: <tf.Tensor 'SequenceMask_16/Less:0' shape=(2, 3, 7) dtype=bool>
max_list_mask.eval()
Out[69]: 
array([[[ True,  True, False, False, False, False, False],
        [ True,  True,  True, False, False, False, False],
        [ True,  True,  True,  True, False, False, False]],
       [[ True,  True,  True, False, False, False, False],
        [ True,  True,  True,  True, False, False, False],
        [ True,  True,  True,  True,  True, False, False]]])

Conclusion : Assign values according to the dimensions of the list .

If you exchange the data positions in the list ——

length_list2 = [ [3, 4, 5], [2, 3, 4]]
list_mask2 = tf.sequence_mask(lengths=length_list2)
list_mask2
Out[30]: <tf.Tensor 'SequenceMask_7/Less:0' shape=(2, 3, ?) dtype=bool>
list_mask2.eval()
Out[31]: 
array([[[ True,  True,  True, False, False],
        [ True,  True,  True,  True, False],
        [ True,  True,  True,  True,  True]],
       [[ True,  True, False, False, False],
        [ True,  True,  True, False, False],
        [ True,  True,  True,  True, False]]])
        
max_list_mask2 = tf.sequence_mask(lengths=length_list2, maxlen=7)
max_list_mask2
Out[43]: <tf.Tensor 'SequenceMask_11/Less:0' shape=(2, 3, 7) dtype=bool>
max_list_mask2.eval()
Out[44]: 
array([[[ True,  True,  True, False, False, False, False],
        [ True,  True,  True,  True, False, False, False],
        [ True,  True,  True,  True,  True, False, False]],
       [[ True,  True, False, False, False, False, False],
        [ True,  True,  True, False, False, False, False],
        [ True,  True,  True,  True, False, False, False]]])

A list containing a list ——

length_list3 = [ [2, 3, 4] ]
list_mask3 = tf.sequence_mask(lengths=length_list3)
list_mask3
Out[34]: <tf.Tensor 'SequenceMask_8/Less:0' shape=(1, 3, ?) dtype=bool>
list_mask3.eval()
Out[35]: 
array([[[ True,  True, False, False],
        [ True,  True,  True, False],
        [ True,  True,  True,  True]]])

max_list_mask3 = tf.sequence_mask(lengths=length_list3, maxlen=6)
max_list_mask3
Out[46]: <tf.Tensor 'SequenceMask_12/Less:0' shape=(1, 3, 6) dtype=bool>
max_list_mask3.eval()
Out[47]: 
array([[[ True,  True, False, False, False, False],
        [ True,  True,  True, False, False, False],
        [ True,  True,  True,  True, False, False]]])

 

Reference material :

https://wenku.baidu.com/view/d98d1c1640323968011ca300a6c30c225901f0f2.html

http://www.kaotop.com/it/29711.html 

原网站

版权声明
本文为[Modest and prosperous]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051546329121.html