Nevermind XOR - Deep Learning has an issue with Sin
More precisely, even the best neural networks can not be trained to approximate
periodic functions using stochastic gradient descent. (empirically, prove me wrong!)
Contents
Simulating a clock
Simplifying the problem
Why can’t we train the sin?
What to do about it?
Simulating a clock
The problem that led me into this sinkhole is an attempt to model biological
rhythms. People sleep every 24 hours, the nature has a distinct 365 day cycle,
and over a month the Moon goes from new to full and back. I wanted to capture
that repetition relative to the current instant in time, which led
to the Clock Problem:
Given the number of seconds since some moment T0 defined to be 0 days 0:00:00,
train a neural network to approximate the numbers you would see on a digital
clock. For example, -1 (second) would be 23:59:59.
Expecting that to be a dead simple task, I built an infinite dataset, that
would sample a random instant in time from a period of 120 years, and fed it
into a SIREN - neural network with
sinusoidal activations.
To my surprise, despite playing with its frequency scale hyperparameter,
the network, essentially, never converged.
I tried to use regular MLP with GELU activations, and got approximately
the same result.
Research on the topic only brought Neural Networks Fail to Learn Periodic Functions and How to Fix It,
which, as you might have guessed it, did not really work on the Clock Problem.
Their x + sin(x) ** 2 activation only really worked when the number of full cycles
in the dataset was less than the number of paramters of the network, which
completely misses the point.
Simplifying the problem
You can quickly see how inappropriate gradient descent is for the problem if we
just simplify it a little. Let’s try approximating this trivial function:
FREQUENCY_SCALE = 31
def func(x):
return torch.sin(x * FREQUENCY_SCALE)
There is a a PyTorch module, that surely should solve the problem:
class Sin(torch.nn.Module):
def __init__(self):
super().__init__()
self.freq = torch.nn.Parameter(torch.randn(1))
def forward(self, x):
return torch.sin(self.freq * x)
Here, we only need to find the frequency, and the module will match our target
function exactly! Let’s try it out:
net = Sin()
opt = torch.optim.SGD(net.parameters(), lr=0.0001)
BATCH_SIZE = 32
for batch_idx in range(1000*1000):
opt.zero_grad()
batch = (torch.rand(size=[BATCH_SIZE, 1], device=device) * 2 - 1) * 1000
out = net(batch)
expected = func(batch)
loss = ((out - expected) ** 2).mean()
loss.backward()
opt.step()
if batch_idx % 1000 == 0:
print(f
lostmsu.github.io