How can i implement the __getitem__ method in PyTorch for the sinus
I am starting with PyTorch and i am trying to create a Network that is predicting the sinus of x. I tried to create a DataSet like this:
 class SinusDataset(Dataset):

    def __init__(self, size: int = 1000):
        self.size = size

    def __len__(self):
        return self.size


    def __getitem__(self, idx: int)-&gt;Tensor:
        if idx &gt; self.size:
            raise ValueError
        
        return idx, math.sin(idx)

I do not think that is the proper way to implement this. How should I Implemented the `__get__´ method?

You could initialize your input and labels on init and save those in lists. Then, in your __getitem__ function, pick instances from those two using the provided idx integer. Something like:
class SinusDataset(Dataset):
    def __init__(self, size: int = 1000):
        self.x = torch.linspace(0, 1, size)
        self.y = torch.sin(self.x)

    def __len__(self) -&gt; int:
        return len(self.x)

    def __getitem__(self, idx: int):
        return return self.x[idx][None], self.y[idx][None]

Then you can use the dataset by wrapping a torch.utils.data.DataLoader:
&gt;&gt;&gt; dl = DataLoader(SinusDataset(100), batch_size=4, shuffle=True)

&gt;&gt;&gt; for x, y in dl:
...     print(x, y)
...     break
tensor([0.2452, 0.6116, 0.0791, 0.6667]) tensor([0.2428, 0.5742, 0.0790, 0.6184])


In this case it would be more appropriate to inherit from torch.utils.data.TensorDataset directly. This comes with both __len__ and __getitem__ implemented for you (see source):
class SinusDataset(TensorDataset):
    def __init__(self, size: int = 1000):
        x = torch.linspace(0, 1, 1000)[:,None]
        y = torch.sin(x)[:,None]
        super().__init__(x, y)

This is slightly more advanced but it is considered best practice to inherit from the closest built-in torch.utils.data.Dataset class instead of writing the same methods yourself.

Inference example:
&gt;&gt;&gt; model = nn.Sequential(nn.Linear(1, 4), 
                          nn.ReLU(), 
                          nn.Linear(4, 1))

&gt;&gt;&gt; x, y = next(iter(dl))
&gt;&gt;&gt; model(x)
tensor([[-0.0640],
        [ 0.1461],
        [-0.0882],
        [ 0.2259]], grad_fn=&lt;AddmmBackward&gt;)

