PyTorch can't use a float type but only long
I am trying to run this very basic neural network:
import os; os.environ[&quot;KMP_DUPLICATE_LIB_OK&quot;] = &quot;TRUE&quot;
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import time

#####################################################
#             Create the neural network             #
#####################################################

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(1, 10)
        self.fc2 = nn.Linear(10, 10)
        self.fc3 = nn.Linear(10, 1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x


net = Net()

#####################################################
#                   Create the datasets             #
#####################################################


trainset = [torch.tensor([1., 1.**2]), torch.tensor([2., 2.**2]), torch.tensor([3., 3.**2]), torch.tensor([4., 4.**2]), torch.tensor([5., 5.**2]), torch.tensor([6., 6.**2]), torch.tensor([7., 7.**2]), torch.tensor([8., 8.**2])]

testset = [torch.tensor([1.1, 1.1**2]), torch.tensor([2.3, 2.3**2]), torch.tensor([3.1, 3.1**2]), torch.tensor([4.5, 4.5**2]), torch.tensor([5.9, 5.9**2]), torch.tensor([6.1, 6.1**2]), torch.tensor([7.3, 7.3**2]), torch.tensor([8.01, 8.01**2])]

#####################################################
#               Optimize the parameters             #
#####################################################

optimizer = torch.optim.Adam(net.parameters(), lr=0.001)


EPOCHS = 3

for epoch in range(EPOCHS):
    for data in trainset:
        x, y = data
        net.zero_grad()
        output = net(x.view(-1,1))
        loss = F.nll_loss(output, y.view(-1,1)[0])
        loss.backward()
        optimizer.step()
    print(loss)

#####################################################
#           Calculate the accuracy rate             #
#####################################################

correct = 0
total = 0

with torch.no_grad():
    for data in trainset:
        x, y = data
        output = net(x)
        if y - 0.01 &lt; output &lt; y + 0.01:
            correct += 1
        total += 1

print(&quot;Accuracy: %.2f&quot; % (correct / total))

but I get the following error:

Traceback (most recent call last):   File
&quot;C:\Users\Andrea\Desktop\pythonProject\main.py&quot;, line 52, in 
loss = F.nll_loss(output, y.view(-1,1)[0])   File &quot;C:\WinPython\python-3.9.1.amd64\lib\site-packages\torch\nn\functional.py&quot;,
line 2235, in nll_loss
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: expected scalar type Long but found Float

Why can't I use a float type?

The negative log likelihood loss (NLLLoss) is suitable for classification problems, where the output is one out of C classes. Since the classes are discrete, your labels need to be of the long type.
In your case, in a comment, you say:

I want to create a network that simulates a quadratic function with x as input and sth similar to x**2 as output.

This is a regression problem, where the output can have a real, continuous value. For this, you should use a suitable loss function such as the mean squared error loss (MSELoss). So, one way to fix would be changing F.nll_loss in your code to F.mse_loss.
