I was wondering if there is a way to limit the overhead of arithmetics in pytorch as this lets us code everything in pytorch as compared to only using pytorch when the gradient of the calculation is needed.
This gives much cleaner code in reinforcement learning as often small calculations are needed of arrays which don’t need to be backpropagated.
import numpy as np
import torch
a = np.array([1.,2.,3.,4.,5.])
%timeit 2*a    #703 ns ± 34.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
a = torch.from_numpy(a)
%timeit 2*a    #7.99 µs ± 78.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)