In [5]:
# %load sin.py
import numpy as np
import src.my_nn as mynn
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline

# Create x data for input and force to be be column vector
x = np.linspace(0,2*np.pi,100)[:,None]
#x2 = np.linspace(.9999,1,100)[:,None]
#x = np.concatenate((x,x2),axis=1)
# Create y data for output
#y = x[:,1] * np.sin(x[:,0])
#y=y[:,None]
y=2+ np.cos(x) + np.random.rand(100,1)*0.4-0.2 + x

# Normalize input and output
xnorm, xmin, xmax = mynn.stdize(x)
ynorm, ymin, ymax = mynn.norm(y)

# Run the neural network
hidden_neurons = 12
num_loops = 10**5
Yout, _ = mynn.run(xnorm, ynorm, hidden_neurons, print_loss=True, num_passes=num_loops)

#Unnormalize model output
y_train = mynn.unnorm(Yout, ymin, ymax)

#Calculate error in real units
rmse = np.sqrt(np.sum(np.square(y - y_train))/y.shape[0])

#Make plot and give error in r
plt.plot(x[:,0],y,label='sin(x)')
plt.plot(x[:,0],y_train,label='NN')
plt.title(('RMSE=%f for %d hidden neurons') %(rmse, hidden_neurons))
plt.legend()
plt.show()


Loss after iteration 0: 0.579537
Loss after iteration 1: 0.157844
Loss after iteration 2: 0.134165
Loss after iteration 3: 0.109960
Loss after iteration 4: 0.085048
Loss after iteration 5: 0.059830
Loss after iteration 6: 0.036326
Loss after iteration 7: 0.019362
Loss after iteration 8: 0.012829
Loss after iteration 9: 0.011760
Loss after iteration 5000: 0.001219
Loss after iteration 10000: 0.001210
Loss after iteration 15000: 0.001206
Loss after iteration 20000: 0.001204
Loss after iteration 25000: 0.001203
Loss after iteration 30000: 0.001203
Loss after iteration 35000: 0.001203
Loss after iteration 40000: 0.001202
Loss after iteration 45000: 0.001202
Loss after iteration 50000: 0.001202
Loss after iteration 55000: 0.001202
Loss after iteration 60000: 0.001202
Loss after iteration 65000: 0.001202
Loss after iteration 70000: 0.001202
Loss after iteration 75000: 0.001202
Loss after iteration 80000: 0.001202
Loss after iteration 85000: 0.001202
Loss after iteration 90000: 0.001202
Loss after iteration 95000: 0.001202

In [ ]: