Hi NPC!



 NPC:

Lets say i have a kernel like [a, b, c].
But i want  a == c (or more precisely I want the optim. to threat it as one parameter).


To me the conceptually cleanest approach would be to build your
constrained kernel from a separate two-element Parameter that
you actually optimize.  Something like:
import torch
from itertools import chain

# initialize  a = 1.1, b = 2.2
k = torch.nn.Parameter (torch.tensor ([1.1, 2.2]))
opt = torch.optim.SGD (chain (my_model.parameters(), (k, )), lr = 0.1)
# ...
# then in your forward pass
    # x = apply_some_model_layers (x)
    ker = torch.cat ((k[:2], k[0:1]))
    x = torch.nn.functional.conv1d (x, ker)
    # x = apply_some_more_model_layers (x)
    return x

I believe that the weight of a torch.nn.Conv1d has to
be a leaf tensor, so we have to use the functional form,
torch.nn.functional.conv1d ().  By using differentiable
pytorch tensor operations to build ker from k, you will be able
to backpropagate through the construction of ker and properly
optimize the two elements of k.
Best.
K. Frank