How can I approximate a sine function using neural networks?

167 Views Asked by At

I am trying to replicate the figure 5.9 of the book Machine Learning and Patter recognition. But in the figure the output function is a soft one, in my architecture it is just joined points. How can I obtain the same output? I attach both outputs and my code.

enter image description here

enter image description here

from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from tensorflow import keras
from tensorflow.keras.models import Sequential, load_model
from numpy import asarray
import matplotlib.pyplot as plt
import math as mt
import numpy as np
# define the dataset
x = asarray(np.linspace(0,1,10))
y = asarray(np.sin(np.pi*2*x))
# reshape arrays into into rows and cols
x = x.reshape((len(x), 1))
y = y.reshape((len(y), 1))
x = x.reshape((10,1))
y = y.reshape((10,1))
# separately scale the input and output variables
scale_x = MinMaxScaler()
x = scale_x.fit_transform(x)
scale_y = MinMaxScaler()
y = scale_y.fit_transform(y)

model = Sequential()
model.add(keras.layers.Dense(10, input_dim=1, activation='tanh', kernel_initializer='glorot_uniform'))
model.add(keras.layers.Dense(3, activation='tanh', kernel_initializer='glorot_uniform'))
model.add(keras.layers.Dense(1))
model.compile(loss='mse', optimizer='adam')
model.fit(x, y, epochs=5000, verbose=0)
yhat = model.predict(x)

x_plot = scale_x.inverse_transform(x)
y_plot = scale_y.inverse_transform(y)

yhat_plot = scale_y.inverse_transform(yhat)
fig = plt.figure(figsize=(10,5))
plt.scatter(x_plot,y_plot,c='c',linewidths=0.2 ,label='Actual')

plt.plot(x_plot,yhat_plot,'r',label='Output')

plt.title('Sine Apporximation')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
```
1

There are 1 best solutions below

0
On

I have not looked at the code. I make the assumption that the plot is simply linearly joined samples of x,y pairs. In other words that you only evaluate the network for the training x and y pairs. You will need to evaluate also the points in between the training samples to see what happens there. For example to use x values with finer grained resolution like

  x = asarray(np.linspace(0,1,100))

when you evaluate the model you have trained