Trying to Implement Fixed Filters after each Layer

Mert_Dunver: <p>Hello in our project we are trying to implement several fixed filters<br>
to each convolutional layer.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/3/53a4267ae910c632a9d9ed9ad9e849330d34c829.png" data-download-href="https://discuss.pytorch.org/uploads/default/53a4267ae910c632a9d9ed9ad9e849330d34c829" title="image_2022-01-26_153425"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/3/53a4267ae910c632a9d9ed9ad9e849330d34c829.png" alt="image_2022-01-26_153425" data-base62-sha1="bVVjRTau6L8WMXth1CA8pNIbllT" width="690" height="480" data-dominant-color="232423"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image_2022-01-26_153425</span><span class="informations">951×662 31.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
As shown in the code we have two filters that we want to add to each layer but weren’t able to grasp how we add these filters. If you could show us a way to do so we would be very grateful.</p>

Mert_Dunver: <p>Forgot to add the code aswell<br>
def get_grad_kernel(channels):</p>
<pre><code>  Iy = [[1,-1]]        #We want to implement these two filters

  Ix = [[1],[-1]]      #This part of code was also taken from another answer which we werent able to implement

  return torch.Tensor([[Ix]*channels,[Iy]*channels])/4

class Net(nn.Module):

    def __init__(self):

        super().__init__()

        self.conv1 = nn.Conv2d(3, 64,5,1,2)

        self.pool = nn.MaxPool2d(2, 2)

        self.conv2 = nn.Conv2d(64, 128,5,1,2)

        self.conv3 = nn.Conv2d(128,128,5,1,2)

        self.conv4 = nn.Conv2d(128, 128,5,1,2)

        self.conv5 = nn.Conv2d(128, 128,5,1,2)

        self.fc1 = nn.Linear(128, 120)

        self.fc2 = nn.Linear(120, 64)

        self.fc3 = nn.Linear(64,32)

        self.fc4 = nn.Linear(32,16)

        self.fc5 = nn.Linear(16, 100)

        weights1 = torch.tensor([[0,-1],])

    def forward(self, x):

        x = self.pool(F.relu(self.conv1(x)))

        x = self.pool(F.relu(self.conv2(x)))

        x = self.pool(F.relu(self.conv3(x)))

        x = self.pool(F.relu(self.conv4(x)))

        x = self.pool(F.relu(self.conv5(x)))

        x = torch.flatten(x, 1)  # flatten all dimensions except batch

        x = F.relu(self.fc1(x))

        x = F.relu(self.fc2(x))

        x = F.relu(self.fc3(x))

        x = F.relu(self.fc4(x))

        x = self.fc5(x)

        return x

net = Net()</code></pre>

ptrblck: <p>Assuming <code>Iy</code> and <code>Ix</code> should be used as filters in a conv layer you could assign them as the <code>weight</code> attribute as seen <a href="https://discuss.pytorch.org/t/gaussian-filter-for-images/20597/2">here</a>.</p>

Mert_Dunver: <p>From some of the similar questions i did find that solution but when I tried to make it into a fixed filter I got the following errors<br>
Heres the code</p>
<pre><code class="lang-auto">batch_size = 10
channels = 3
h, w = 24, 24
x = torch.randn(batch_size, channels, h, w)

# View approach
conv = nn.Conv2d(1, 1, 2, 2, bias=False)
with torch.no_grad():
    conv.weight = nn.Parameter(torch.tensor([[[[1, -1],
                                               [-1, 1]]]]))
output = conv(x.view(-1, 1, h, w)).view(batch_size, channels, h//2, w//2)

Error: Only Tensors of floating point and complex dtype can require gradients

When I write the values between 0 and 1 it works just fine.</code></pre>

ptrblck: <p>You are creating the <code>weight</code> tensor as a <code>LongTensor</code>, so either call <code>.float()</code> on it or use floating point values via:</p>
<pre><code class="lang-python">with torch.no_grad():
    conv.weight = nn.Parameter(torch.tensor([[[[1., -1.],
                                               [-1., 1.]]]]))
</code></pre>

Mert_Dunver: <p>Thanks a lot for your help sire. This solved my issue which now I have to solve the issue of size but I’ll try fixing that on my own.</p>