I have a matrix where I want to transform each row element-wise with a different function. It’s not a simple linear function, but a non-linear one. Is this possible in pytorch?
My set of functions is not that small (it depends on the dimensionality of the input).
I think you could achieve this by using
torch.unfold on the last dimension and perform your operations using the python’s
map function. You would need to the concatenate the output of the mapping.
funcs = [torch.cos, torch.sin, torch.tan, torch.log]
x = torch.tensor(np.arange(20).astype('f')).view(4,5)
xu = torch.unbind(x, dim=0)
torch.stack([funcs[i](x) for i, x in enumerate(xu)], dim=0)
If you have some functions that need more than one arguments, you could always wrap them up inside lambda functions (Not always the best thing to do), but that depends on what kinds of functions you’re going to be using.
All the functions are calculations for some density (in a statistical sense). It might be that I have to go higher-order if the functions take more than a scalar as an input.
Thanks! Interesting Idea