The filter weights can be accessed using the weight
parameter of the Conv2d
object. For example,
>>> c = torch.nn.Conv2d(in_channels=2, out_channels=2, kernel_size=3)
>>> c.weight
Parameter containing:
tensor([[[[ 0.2156, 0.0930, -0.2319],
[ 0.1333, -0.0846, 0.1848],
[ 0.0765, -0.1799, -0.1273]],
[[ 0.1173, 0.1650, -0.0876],
[-0.1353, 0.0616, -0.1136],
[-0.2326, -0.1509, 0.0651]]],
[[[-0.2026, 0.2210, 0.0409],
[-0.0818, 0.0793, 0.1074],
[-0.1430, -0.0118, -0.2100]],
[[-0.2025, -0.0508, -0.1731],
[ 0.0217, -0.1616, 0.0702],
[ 0.1903, -0.1864, 0.1523]]]], requires_grad=True)
The weights are initialized by default by sampling from a uniform distribution. You can also initialize weights using various weight initialization schemes.
If you want to manually change the weights, you can do it by modifying the weight
parameter directly. For example, to set all the weights to 1, use,
>>> c.weight.data = torch.ones_like(c.weight)
>>> c.weight
Parameter containing:
tensor([[[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]],
[[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]], requires_grad=True)
Note that during training, the convolutional layers are typically a part of the computational graph, and their weights get automatically updated when making a backward()
call.