I’m not sure why this is a libtorch question?
I would suggest rewriting it in PyTorch first using torch.nn.functional (with linear, sigmoid, and tanh). If you have to, you can refine that afterwards.
np.sum seems wrong, as you want to do reduction over one axis only.

Best regards
Thomas