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

标签 python numpy tensorflow tensorflow2.0 masking

我正在 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

这是一个玩具示例:

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)

输出:

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.]]])

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

最佳答案

我想我终于弄清楚了,用 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

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

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)

结果将如下所示:

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)

关于python - 如何在 Tensorflow (python) 中随机选取和屏蔽张量的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61108469/

相关文章:

python - 如何在 python 中迭代这个嵌入的 JSON "list"?

python - 从文本 block 动态生成生成器函数

python - 在迭代python时从列表中删除与另一个列表相比的项目

python - scikit learn 插补 NaN 以外的值

python - 保存 Tensorflow 模型仅用于预测的最有效的空间/内存方法?

python - 可能更改 Mako 的内联开始/结束语法?

python - 将 pandas 矩阵转换为 DataFrame

从列表中提取二维数组的 Pythonic 方法

python - `tf.estimator.train_and_evaluate`函数控制训练和评估周期的机制是什么?

python - 如何在 Tensorflow 2.0 中制作参差不齐的批处理?