Day 24 利用transformer自己实作一个翻译程序(六) Masking

Masking

需要把填充的部分标记为0,其余部分标记为1,才不会导致填充的部分被误认为是输入

def create_padding_mask(seq):
  seq = tf.cast(tf.math.equal(seq, 0), tf.float32)

  # add extra dimensions to add the padding
  # to the attention logits.
  return seq[:, tf.newaxis, tf.newaxis, :]  # (batch_size, 1, 1, seq_len)
x = tf.constant([[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]])
create_padding_mask(x)
<tf.Tensor: shape=(3, 1, 1, 5), dtype=float32, numpy=
array([[[[0., 0., 1., 1., 0.]]],

[[[0., 0., 0., 1., 1.]]],

[[[1., 1., 1., 0., 0.]]]], dtype=float32)>
def create_look_ahead_mask(size):
  mask = 1 - tf.linalg.band_part(tf.ones((size, size)), -1, 0)
  return mask  # (seq_len, seq_len)
x = tf.random.uniform((1, 3))
temp = create_look_ahead_mask(x.shape[1])
temp
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[0., 1., 1.],
       [0., 0., 1.],
       [0., 0., 0.]], dtype=float32)>

<<:  Day17-"与字串相关的函式-3"

>>:  Day#09 使用者体验

Day3 First Go application

前言 前面的章节可以建议使用线上IDE进行练习与熟悉。 线上 Go IDE的网址在下方 https:...

【Day25】React Class Component 生命周期简单介绍

在写React的时候其实有分为两种写法 Class Component this.state or ...

Day-27 使用StatefulSet

前言 前面我们先介绍了Pod, 控管Pod的ReplicaSet与管理ReplicaSet的Depl...

虚拟主机是什麽又该如何选择?2020 最新台湾虚拟主机推荐比较

虚拟主机是什麽 虚拟主机(Virtual Hosting)又被称作共享主机(Shared Web H...

【Day12-排序】浅谈python中的资料排序——sort, sorted, natsort, pd.sort_values

前一天我们学会了对资料进行简单的map处理之後,今天来聊一下排序 不论是进行人工的检视或是後续分析的...