Given constants $b, n ∈ N$, provide a neural network which implements a function $g_{b,n} : R → R$ such that $|{x ∈ R : g_{b,n}(x) = x^2, 0 ≤ x ≤ b}| > n$, i.e., $g_{b,n}$ intersects the function $f(x) = x^2$ more than $n$ times in the interval $[0, b]$.

My idea is to create a zig zag sort of function split into $b/n$ parts where the start and the end points of each part are at $y=0$ and the midpoint of each part is at $y = b^2$. This ensures that there are 2 intersections in each part.

I know how to create a neural network which makes a linear function, but I'm unsure how to make a zigzag function like this? Is it possible?


1 Answer 1


I like your suggestion for it's simplicity; the network could just learn 2(b/n) + 1 points, and then you can linearly interpolate from there.

Another idea is to create a network to approximate the function $f(x) = x^2(\sin(\frac{2\pi n x}{b}))$. Note that over $[0,b]$, defining $g(x) = \sin(\frac{2\pi n x}{b})$, we have $|g^{-1}(1)| = n$, which implies $f(x) = x^2$ exactly $n$ times.

If you are concerned that a tangental intersection might not be a strong enough condition, you could always consider $\sin(\frac{2\pi n x}{b}) + 1$ instead.

Here is a simple script in pytorch that creates and trains such a network:

import torch
import numpy as np

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = torch.nn.Linear(2, 64)
        self.fc2 = torch.nn.Linear(64, 64)
        self.fc3 = torch.nn.Linear(64, 100)

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

def generate_data(n, b):
    x = np.linspace(0, b, 100)
    y = np.power(x, 2) * (np.sin((2 * np.pi * n * x) / b) + 1)
    return y

input_data = []
target_data = []
for n in range(1, 20):
    for b in np.linspace(1,2,11):

input_data = torch.tensor(input_data, dtype=torch.float32)
target_data = torch.tensor(target_data, dtype=torch.float32)

train = torch.utils.data.TensorDataset(input_data,target_data)
train_loader = torch.utils.data.DataLoader(train, batch_size=32, shuffle=True)

model = Net()
loss_func = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

for epoch in range(1000):
    for i, (inputs, targets) in enumerate(train_loader):

        # Forward pass
        outputs = model(inputs)
        loss = loss_func(outputs, targets)
        # Backward and optimize

And for $n=2$ and $b=2$, the trained network outputs enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.