Sine activation missing in pytorch.nn

The sine activation function, which becomes useful recently, is missing in pytorch.nn. How to remedy this?

Hi @robinho,

You can find the sine function here: torch.sin — PyTorch 2.1 documentation

Hi @AlphaBetaGamma96 thanks a lot, but can this be used as an activation function directly by writing torch.sin in torch.nn?

You can just do,

torch.sin(x)

you don’t have to initialize from torch.nn, autograd will track the gradient.

but in the following code:

import torch.nn as nn
nn.Sequential(nn.Linear(1,10),nn.Sigmoid(),nn.Linear(10,1,bias=False))

it doesn’t seem possible to replace nn.Sigmoid() with torch.sin(x) or sth similar directly?

If you want to define your model within a nn.Sequential object, you can either

  1. re-define your model as an nn.Module object and you can use torch.sin(x) like I stated above
  2. Define an nn.Module object for torch.sin and pass it to the nn.Sequential object.
class Sin(nn.Module):
  def __init__(self):
    pass
  def forward(self, x):
    return torch.sin(x)

Then replace nn.Sigmoid() with Sin()

1 Like

many thanks! I tried the 2nd method writing the same class only to get the following error message:
‘Sin’ object has no attribute ‘_parameters’

Try deleting the __init__.

class Sin(Module):
    def forward(self, input: Tensor) -> Tensor:
        return torch.sin(input)
2 Likes