Apply user defined function to each row (or any dimension)

I’ve bunch of column vectors where I construct a matrix out of them by concatenating them side by side.
That is, if my column vector is of dimension (nx1) and if I have m of them, I end up with a matrix of size (nxm).

On this final matrix, I’d like to apply the same operation to each row. The way I currently do it is simply via iterating over each row manually.

Does PyTorch have a nicer way of doing this?

It depends on the kind of function you want to apply. Give me an example of what function you want to apply.

Depending of the operation you may be able to use einsum or some other operator.

I want to apply linear regression to each row.

Well if the output off your row 1 is needed for the output of your row 2 then you have to loop over them. Other wise go for direct Matrix multiplication

Then I’d say you can use torch. solve
It works with batch, thus, in the worst case, you can reshape your matrix to create a batch and to pose the problem that way.
Obviously it depends on the method you want to use, the best you can do is to code it your self if you have a custom one based on pytorch functions.