How to set up non-linear (sinusoidal) multiple variable regression problems for tensorflow?

How to set up non-linear (sinusoidal) multiple variable regression problems for tensorflow?
typescript
Ethan Jackson

I have some parameters: A1, A2, A3, f1, f2, f3. These parameters are then used to generate a set of sinusoidal data, something like

$y = A1 * sin(f1 * x) + A2 * sin(f2 * x) + A3 * sin(f3 * x)$

From this, I have generated n samples of data for x so that I have y @ x=0, x=end/n, x = 2*end/n ... etc.

I have then made multiple rows of these, so that each row has a series of n y-values from x=0 to x=end, and also the values of A1, A2, A3, f1, f2, f3 used to generate those y values.

The idea is that I will create a neural net in Tensorflow, so that using the same n inputs of y, the neural net will be able to predict the values of the parameters A1, A2, A3, f1, f2, f3. To do this, I have made a way to generate an arbitrary number of these rows, which I can then split into training and testing data for the neural net.

However, I'm having some trouble understanding how I should be setting up this data for the neural net to understand. Do I need to somehow pass the list of values of x that I used to generate the y values to the model? How do I go about telling it to use the parameters as the x-values (as it were) to test against? How do I go about effectively passing the data? Once I've managed all that, how do I actually set up the model to translate from the inputs to the outputs?

I've tried reading through some linear regression examples and also some non-linear regression examples.

As one such example, this tutorial seemed like it might be a good example of the sort of thing I was trying to do, where it's trying to solve for multiple outputs at the same time. However, while that example is solving for multiple coefficients on multiple variables, it's always the same coefficient in the equation. They are using the equation $2x1+3x2+x3$, and looking to solve for those fixed coefficients of [2,3,1], while I would like to be able to pass a sampled line segment and then have my model extract what variable coefficients were used to construct that line.

Answer

Here’s a high-level example of how you could set up your model:

import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Input # Example shapes n = 100 # Number of y-values (sample points) model = Sequential([ Input(shape=(n,)), Dense(128, activation='relu'), Dense(128, activation='relu'), Dense(6) # One for each parameter ]) model.compile(optimizer='adam', loss='mse', metrics=['mae']) model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=50)

Related Articles