# How to create a neural network from a set of equations?

Say I have these equations:

$$x_1 = x_2 + 2y_1 + b$$ $$x_2 = y_2 + c$$ $$y_1 = z + a$$ $$y_2 = y_3 + d$$ $$z = z_1 + e$$

$$x_1$$ depends on $$x_2$$ (depends on $$y_2$$ (depends on $$y_3$$)) and $$y_1$$ (depends on $$z$$ (depends on $$z_1$$)).

$$x_1$$ is my final equation and $$y_3$$ and $$z_1$$ are my initial variables.

How do I represent them in a neural network? My final aim is to backtrack from $$x_1$$, and see what change of an amount $$n$$ in $$x_1$$ resulted from which of $$y_3$$ or $$z_1$$.

All these variables are item prices in the real world.

My inputs are $$z_1$$ and $$y_3$$ and my output is $$x_1. z_1$$ and $$y_3$$ are prices and the final output $$x_1$$ is also a price.

• Do you have a particular reason to prefer a neural network solution as opposed to a traditional solver for a linear system? If you're not particularly set on neural networks, there are efficient ways to solve linear systems in a much simpler way.
– htl
Jul 27 at 14:34
• I want a neural network solution so that I can trace back any delta change in x1 to a major change in either z1 or y3. This neural network will train on historic price data, like last 10 years, and will be used to backtrack changes in x1 thereafter. Do you think a neural network is good for this or is there anything else that is better? Jul 27 at 15:40

## 1 Answer

All modern frameworks for deep learning (PyTorch, Jax, Tensorflow) support automatic differentation. These operations can be easily implemented. Here I write, how it would look like in PyTorch:

class Net(nn.Module):

def __init__(self):
super().__init__()

self.a = nn.Parameter(torch.randn(1))
self.b = nn.Parameter(torch.randn(1))
self.c = nn.Parameter(torch.randn(1))
self.d = nn.Parameter(torch.randn(1))
self.e = nn.Parameter(torch.randn(1))

def forward(self, z1, y3):
z = z1 + self.e
y2 = y3 + self.d
y1 = z + self.a
x2 = y2 + self.c
x1 = x2 + 2 * y1 + self.b
return x1


And the use case is the following, say:

net = Net()
net(torch.ones(1), 2 * torch.ones(1))