So, the revised code example will be as follows:
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3,3, 3)
K = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]])
# I think I should make the shape/size like this?
K = torch.unsqueeze(torch.unsqueeze(K,0),0)
#with torch.no_grad():
self.conv1.weight.data = self.conv1.weight.data + K
def forward(self, x):
x = self.conv1(x)
return x
net = Net()
net(torch.randn(4,3,10,10))