Hello Miguel!



 migu3l705:

how can I program a multilayer perceptron whose output is the function y = x ^ 2, starting from x = […- 2, -1,0,1,2 …]


Let me speculate a little about what you are asking.
As a learning exercise you might be asking how to train a neural
network to reproduce the function x^2 without building into it by
hand any knowledge of that specific function.
One of the interesting and important features of neural networks
is that their linear layers plus non-linear activations can be used
to reproduce / approximate  many interesting functions.  See, for
example, the “Universal approximation theorem” 1 wikipedia article.
I haven’t experimented with this in particular, but you might try
training a network like (just making some something up):
model = torch.nn.Sequential (
    torch.nn.Linear (1, 50),
    torch.nn.Tanh(),
    torch.nn.Linear (50, 50),
    torch.nn.Tanh(),
    torch.nn.Linear (50, 1)
)

torch.nn.MSELoss would be the appropriate loss function.
(Your inputs would be your various values of x, e.g., your
[…- 2, -1,0,1,2 …].  Your targets would be the corresponding x^2.)
You could also try other activations, e.g., torch.nn.ReLU, and
wider / narrower or more / fewer hidden layers.
If you get such a network working for x^2 it would be informative
to retrain it (from scratch) on something like abs (x)^3.
Good luck.
K. Frank