TF2中有一种名为Masking
的图层,声称可以掩盖时态数据。我进行了一些测试,发现接下来的层仍然可以将值添加到首先被掩盖的那些变量中。例如
def compute_valid_entries(x, mask):
n = np.reduce_sum(mask)
print(n.numpy())
for i in range(len(mask.shape)):
if mask.shape[i] == 1:
n *= x.shape[i]
return n
x = np.reshape(np.arange(100), (2, 5, 10)).astype(np.float32)
mask = np.ones(10).reshape((2, 5))
mask[1] = 0
mask = mask[..., None]
x_masked = x*mask
model = Sequential([Masking(), Dense(2, bias_initializer=tf.constant_initializer(1.))])
print(model(x_masked))
# print out a tensor of shape (2, 5, 2) with no zeros
如果是这样,添加Masking
有什么意义? Masking
层有什么好处?我应该怎么用?