<p>The problem is that <code>x*x</code> is a very different beast than <code>a*x</code>.</p>

<p>Please note what a usual "neural network" does: it stacks <code>y = f(W*x + b)</code> a few times, never multiplying <code>x</code> with itself. Therefore, you'll never get perfect reconstruction of <code>x*x</code>. Unless you set <code>f(x) = x*x</code> or similar.</p>

<p>What you can get is an approximation in the range of values presented during training (and perhaps a very little bit of extrapolation). Anyway, I'd recommend you to work with a smaller range of values, it will be easier to optimize the problem.</p>

<p>And on a philosophical note: In machine learning, I find it more useful to think of good/bad, rather than correct/wrong. Especially with regression, you cannot get the result "right" unless you have the exact model. In which case there is nothing to learn.</p>

<hr>

<p>There actually are some NN architectures multiplying <code>f(x)</code> with <code>g(x)</code>, most notably <a href="http://colah.github.io/posts/2015-08-Understanding-LSTMs/" rel="noreferrer">LSTMs</a> and <a href="https://arxiv.org/pdf/1505.00387.pdf" rel="noreferrer">Highway networks</a>. But even these have one or both of <code>f(x)</code>, <code>g(s)</code> bounded (by logistic sigmoid or tanh), thus are unable to model <code>x*x</code> fully.</p>

<hr>

<p>Since there is some misunderstanding expressed in comments, let me emphasize a few points:</p>

<ol>
<li>You <strong>can approximate</strong> your data.</li>
<li>To do well in any sense, you do need a <strong>hidden layer</strong>.</li>
<li>But <strong>no more data</strong> is necessary, though if you cover the space, the model will fit more closely, see <a href="https://stackoverflow.com/a/55203161/9703830">desernaut's answer</a>.</li>
</ol>

<p>As an example, here is a result from a model with a single hidden layer of 10 units with tanh activation, trained by SGD with learning rate 1e-3 for 15k iterations to minimize the MSE of your data. Best of five runs:</p>

<p><a href="https://i.stack.imgur.com/3gMZ4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3gMZ4.png" alt="Performance of a simple NN trained on OP&#39;s data"></a></p>

<p>Here is the full code to reproduce the result. Unfortunately, I cannot install Keras/TF in my current environment, but I hope that the PyTorch code is accessible :-)</p>

<pre><code>#!/usr/bin/env python
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

X = torch.tensor([range(-10,11)]).float().view(-1, 1)
Y = X*X

model = nn.Sequential(
    nn.Linear(1, 10),
    nn.Tanh(),
    nn.Linear(10, 1)
)

optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
loss_func = nn.MSELoss()
for _ in range(15000):
    optimizer.zero_grad()
    pred = model(X)
    loss = loss_func(pred, Y)
    loss.backward()
    optimizer.step()

x = torch.linspace(-12, 12, steps=200).view(-1, 1)
y = model(x)
f = x*x

plt.plot(x.detach().view(-1).numpy(), y.detach().view(-1).numpy(), 'r.', linestyle='None')
plt.plot(x.detach().view(-1).numpy(), f.detach().view(-1).numpy(), 'b')
plt.show()
</code></pre>
Neural network for square (x^2) approximation
<p>I made a simple module that should figure out the relationship between input and output numbers, in this case, x and x squared. The code in Python:</p>
<pre><code>import numpy as np
import tensorflow as tf

# TensorFlow only log error messages.
tf.logging.set_verbosity(tf.logging.ERROR)

features = np.array([-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
                    9, 10], dtype = float)
labels = np.array([100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 64,
                    81, 100], dtype = float)

model = tf.keras.Sequential([
    tf.keras.layers.Dense(units = 1, input_shape = [1])
])

model.compile(loss = &quot;mean_squared_error&quot;, optimizer = tf.keras.optimizers.Adam(0.0001))
model.fit(features, labels, epochs = 50000, verbose = False)
print(model.predict([4, 11, 20]))
</code></pre>
<p>I tried a different number of units, and adding more layers, and even using the <code>relu</code> activation function, but the results were always wrong.
It works with other relationships like x and 2x. What is the problem here?</p>