如何在 Tensorflow (python) 中随机选取和屏蔽张量的一部分

RDl*_*ady 2 python numpy masking tensorflow tensorflow2.0

我正在 Tensorflow 2 中训练一个去噪自动编码器,一部分运行时间用于 CPU 对部分输入数据进行屏蔽,随机选择要屏蔽的索引,然后将它们的值设置为零。这是我的掩蔽函数,该掩蔽在每个纪元开始时以不同的 v 值重复:

import numpy as np

def masking_noise(X, v):

    X_noise = X.copy()

    n_samples = X.shape[0]
    n_features = X.shape[1]
    v = int(np.round(n_features*v))
    for i in range(n_samples):
        mask = np.random.choice(n_features, v, replace=False)

        for m in mask:
            X_noise[i][m] = np.repeat(0.,X.shape[2])

    return X_noise
Run Code Online (Sandbox Code Playgroud)

这是一个玩具示例:

a = np.array([[[1., 0.],
        [1., 0.],
        [1., 0.],
        [1., 0.],
        [0., 1.]],

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

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

masking_noise(a, 0.40)
Run Code Online (Sandbox Code Playgroud)

输出:

array([[[1., 0.],
        [0., 0.],
        [1., 0.],
        [1., 0.],
        [0., 0.]],

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

       [[1., 0.],
        [1., 0.],
        [1., 0.],
        [0., 0.],
        [0., 0.]]])
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何在 Tensorflow 中执行相同的屏蔽操作?

RDl*_*ady 5

我想我终于弄清楚了,用 Tensorflow 2 很容易调试这个问题,所以当我从 TF1 更改为 TF2 时我能够解决这个问题:

def mask_data(y_true, mask_ratio, verbose=0):

    nf = tf.cast(tf.shape(y_true)[1], tf.float32)
    mask_portion = tf.math.round( tf.math.multiply(nf,(1-mask_ratio)) )
    mask_portion = tf.cast(mask_portion, tf.int32)

    z = -tf.math.log(-tf.math.log(tf.random.uniform(tf.shape(y_true)[0:-1],0,1))) 
    _, indices = tf.nn.top_k(z, mask_portion)
    one_hots = tf.one_hot(indices, tf.shape(y_true)[1])
    mask = tf.reduce_max(one_hots, axis=1)
    mask = tf.expand_dims(mask,axis=-1)
    mask_tiles = tf.tile(mask,[1,1,tf.shape(y_true)[-1]]) 
    masked = tf.multiply(mask_tiles,toy_example)
    if(verbose>0):
        print("\nRandomly selected indices:", indices)
        print("\n2D mask (per variant)", mask)
        print("\n3D mask (per allele)", mask_tiles)
        print("\nmasked results", masked)

    return masked
Run Code Online (Sandbox Code Playgroud)

然后我可以像这样运行它:

toy_example = np.array([[[1., 0.],
    [1., 0.],
    [1., 0.],
    [1., 0.],
    [0., 1.]],

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

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

mask_ratio = 0.40
result = mask_data(toy_example, mask_ratio, verbose=0)
print(result)
Run Code Online (Sandbox Code Playgroud)

结果将如下所示:

tf.Tensor(
[[[1. 0.]
  [1. 0.]
  [0. 0.]
  [1. 0.]
  [0. 0.]]

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

 [[0. 0.]
  [0. 0.]
  [1. 0.]
  [1. 0.]
  [1. 1.]]], shape=(3, 5, 2), dtype=float32)
Run Code Online (Sandbox Code Playgroud)