Keras Embedding层中的mask_zero是如何工作的?

我以为mask_zero=True会在输入值为0时输出0,这样后续的层可以跳过计算或其他操作。

mask_zero是如何工作的?

示例:

data_in = np.array([  [1, 2, 0, 0]])data_in.shape>>> (1, 4)# modelx = Input(shape=(4,))e = Embedding(5, 5, mask_zero=True)(x)m = Model(inputs=x, outputs=e)p = m.predict(data_in)print(p.shape)print(p)

实际输出是:(数字是随机的)

(1, 4, 5)[[[ 0.02499047  0.04617121  0.01586803  0.0338897   0.009652  ]  [ 0.04782704 -0.04035913 -0.0341589   0.03020919 -0.01157228]  [ 0.00451764 -0.01433611  0.02606953  0.00328832  0.02650392]  [ 0.00451764 -0.01433611  0.02606953  0.00328832  0.02650392]]]

然而,我以为输出会是:

[[[ 0.02499047  0.04617121  0.01586803  0.0338897   0.009652  ]  [ 0.04782704 -0.04035913 -0.0341589   0.03020919 -0.01157228]  [ 0 0 0 0 0]  [ 0 0 0 0 0]]]

回答:

实际上,将Embedding层的mask_zero设置为True并不会返回零向量。相反,Embedding层的行为不会改变,它会返回索引为零的嵌入向量。你可以通过检查Embedding层的权重来确认这一点(即在你提到的示例中是m.layers[0].get_weights())。相反,它会影响后续层的行为,比如RNN层。

如果你查看Embedding层的源代码,你会看到一个名为compute_mask的方法:

def compute_mask(self, inputs, mask=None):    if not self.mask_zero:        return None    output_mask = K.not_equal(inputs, 0)    return output_mask

这个输出掩码将作为mask参数传递给支持掩码的后续层。这在基础层Layer__call__方法中已实现:

# Handle mask propagation.previous_mask = _collect_previous_mask(inputs)user_kwargs = copy.copy(kwargs)if not is_all_none(previous_mask):    # The previous layer generated a mask.    if has_arg(self.call, 'mask'):        if 'mask' not in kwargs:            # If mask is explicitly passed to __call__,            # we should override the default mask.            kwargs['mask'] = previous_mask

这使得后续层忽略(即在计算中不考虑)这些输入步骤。这里是一个最小的示例:

data_in = np.array([  [1, 0, 2, 0]])x = Input(shape=(4,))e = Embedding(5, 5, mask_zero=True)(x)rnn = LSTM(3, return_sequences=True)(e)m = Model(inputs=x, outputs=rnn)m.predict(data_in)array([[[-0.00084503, -0.00413611,  0.00049972],        [-0.00084503, -0.00413611,  0.00049972],        [-0.00144554, -0.00115775, -0.00293898],        [-0.00144554, -0.00115775, -0.00293898]]], dtype=float32)

如你所见,LSTM层的第二和第四时间步的输出分别与第一和第三时间步的输出相同。这意味着这些时间步已经被掩码了。

更新:在计算损失时也会考虑掩码,因为损失函数内部被增强以支持使用weighted_masked_objective的掩码:

def weighted_masked_objective(fn):    """Adds support for masking and sample-weighting to an objective function.    It transforms an objective function `fn(y_true, y_pred)`    into a sample-weighted, cost-masked objective function    `fn(y_true, y_pred, weights, mask)`.    # Arguments        fn: The objective function to wrap,            with signature `fn(y_true, y_pred)`.    # Returns        A function with signature `fn(y_true, y_pred, weights, mask)`.    """

在编译模型时

weighted_losses = [weighted_masked_objective(fn) for fn in loss_functions]

你可以使用以下示例来验证这一点:

data_in = np.array([[1, 2, 0, 0]])data_out = np.arange(12).reshape(1,4,3)x = Input(shape=(4,))e = Embedding(5, 5, mask_zero=True)(x)d = Dense(3)(e)m = Model(inputs=x, outputs=d)m.compile(loss='mse', optimizer='adam')preds = m.predict(data_in)loss = m.evaluate(data_in, data_out, verbose=0)print(preds)print('Computed Loss:', loss)[[[ 0.009682    0.02505393 -0.00632722]  [ 0.01756451  0.05928303  0.0153951 ]  [-0.00146054 -0.02064196 -0.04356086]  [-0.00146054 -0.02064196 -0.04356086]]]Computed Loss: 9.041069030761719# verify that only the first two outputs # have been considered in the computation of lossprint(np.square(preds[0,0:2] - data_out[0,0:2]).mean())9.041070036475277

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注