Filter with probability

Is it possible to filter two same size matrix with probability?
So for example, lets say m1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
and m2 is [[3, 4, 2], [5, 1, 2], [0, 4, 2]]
and I want to combine these two matrix with a probability say 0.3, so element in m2
has a probability of 0.3 to be in the output matrix m3. an example for output would be
[[3, 2, 3], [4, 5, 2], [7, 4, 9]]
is this possible with pytorch?

Hi,

Try the following solution.

import numpy as np
import torch

p = 0.3

m1 = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
m2 = torch.Tensor([[3, 4, 2], [5, 1, 2], [0, 4, 2]])

A = torch.Tensor(np.random.choice([0,1], size=[len(m1),len(m1[0])], p=[1-p, p]))

m3 = torch.mul(1-A,m1) + torch.mul(A,m2)

print(m3)

Thanks