你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

收集和散点

Gather () , Scatter ()

根据掩码缩短或延长数据序列。

BS.Sequences.Gather  (maskSequence, dataSequence)
BS.Sequences.Scatter (maskSequence, dataSequence)

parameters

  • maskSequence:静态张量维度 [1]) 的 0 或 (1 个序列,其中 1 个指示要保留的样本。 (任何其他值都会导致未定义的行为,并且将来可能会更改。)
  • dataSequence:要处理的数据序列。 因此 Gather(),它的长度必须与它的 maskSequence长度相同,而对于 Scatter(),它必须等于其中的 maskSequence一个数。

返回值

对于 Gather(),一个包含子集的 dataSequence序列,其中删除了一些样本。 结果序列的长度将等于其中 maskSequence一个数。

对于 Scatter(),一个序列,其中包含 dataSequence 样本和插入零。 结果序列的长度 maskSequence与 .

说明

Gather() 并允许 Scatter() 你根据掩码序列缩短或延长输入序列,例如单词序列。

Gather()返回其maskSequence[n]1所属样本dataSequence[n]的子序列,而掩码为 0 的样本将被删除。

Scatter() 采用子序列的元素,并将其置于更长的序列中,插入零。

例如:

g = Gather  (m, d)
s = Scatter (m, g)

d = A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  # data
m = 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1  # mask
g = A B D G I J M P Q T W X Z
s = A B 0 D 0 0 G 0 I J 0 0 M 0 0 P Q 0 0 T 0 0 W X 0 Z

Gather() 通常通过 Slice(..., axis=-1)BS.Sequences.First()并且 BS.Sequences.Last()间接使用。

示例

循环语言模型

给定窗体 <s> A B C ... </s>的单词序列,语言模型采用 input 窗体 <s> A B C ... 来预测 labels 窗体 A B C ... </s>Gather()允许从文件中读取单个单词序列,然后分别通过删除尾随和前导来创建inputlabels和删除<s></s>

words  = Input {inputDim}
inputMask  = !BS.Sequences.IsFirst (words)
labelsMask = !BS.Sequences.IsLast  (words)
input  =                       Gather (inputMask,  words)          # drop trailing </s>
labels = ReconcileDynamicAxis (Gather (labelsMask, words), input)  # drop leading <s>

lstmLM = Sequential
(
    EmbeddingLayer {300} :
    RecurrentLSTMLayer {512} :
    DenseLayer {inputDim, activation=LogSoftmax}
)

logP = lstmLM (input)
ce = -TransposeTimes (labels, logP)
criterionNodes = (ce)

注意:操作 Gather() 可以更方便地编写如下,在后台调用 Gather()

input  =                       Slice (0, -1, words, axis=-1)          # strip trailing </s>
labels = ReconcileDynamicAxis (Slice (1,  0, words, axis=-1), input)  # strip leading <s>

思想向量

下面选择隐藏状态序列的最后一个示例:

h = RecurrentLSTMLayer {} (x)       # sequence of hidden states
lastMask = BS.Sequences.IsLast (h)  # mask to denote the last step
thoughtVector = BS.Sequences.Gather (lastMask, h)  # select the last

IsFirst () 、IsLast ()

创建一个掩码,以便 Gather() 分别选择序列的第一个和最后一个示例。

BS.Sequences.IsFirst (dataSequence)
BS.Sequences.IsLast  (dataSequence)

parameters

  • dataSequence:任何序列,例如单词序列

返回值

要使用的 Gather() 掩码将分别选择第一项或最后一项,同时删除所有其他样本。

说明

IsFirst()IsLast() 创建一个掩码,以便 Gather() 分别选择序列的第一个和最后一个样本,这在序列到序列模型中通常需要提取“思想向量”, (重复周期) 的最后隐藏状态。

示例

请参阅