Remove connections between layers

This is almost certainly algorithmically not quite ideal. Some comments

  • I would recommend using net[1] (if your model is nn.Sequential) or net.name_of_your_layer instead of enumerating the children and picking one. Similar for the parameters (you want .weight?).
  • If you want to do something in all 200 rows (mind you randint is exclusive the upper limit), you can just enumerate over them in the rows and the pick columns at random. You can use advanced indexing to speed this up. (I use torch.no_grad instead of .data, because I got an error when using .data with indexing and assignment.) Here is a toy example with a random matrix instead of net[1].weight.grad:
a = torch.randn(5,5)
print(a)
i = torch.arange(5)
j = torch.randint_like(i, 0, 5)
with torch.no_grad():
  a[i,j] = 0
print(a)

If you want to have distinct rows, you can use torch.randperm for j instead.

Best regards

Thomas

1 Like