In [1]:
#import NeuralNet
from physlearn.NeuralNet import NeuralNetPro
import numpy
import tensorflow as tf
from math import pi
import matplotlib.pyplot as plt
#import OptimizerLib.NelderMeadLib.NelderMead
from physlearn.Optimizer import NelderMead
from scipy.optimize import minimize
%matplotlib inline

In [2]:
x_train = numpy.linspace(0, 2 * pi, 30).reshape(1, 30)
y_train = numpy.sin(x_train[0]).reshape(1, 30)

In [3]:
x_cv = numpy.linspace(0, 2 * pi, 1000).reshape(1, 1000)
y_cv = numpy.sin(x_cv[0]).reshape(1, 1000)

In [4]:
plt.plot(x_train[0], y_train[0], 'x', color='red')


Out[4]:
[<matplotlib.lines.Line2D at 0x7f9770746c50>]

In [5]:
net = NeuralNetPro.NeuralNetPro(-1, 1)

In [6]:
net.add_input_layer(1)
net.add(10, tf.sigmoid)
net.add_output_layer(1)

In [7]:
net.compile()

In [8]:
dim = net.return_unroll_dim()

In [9]:
net.set_random_matrixes()
net.calculate_cost(x_train, y_train)


Out[9]:
0.92004457758449854

In [10]:
def cost(params):
    #params = numpy.array(params)
    #print(params.shape)
    net.roll_matrixes(params)
    return net.calculate_cost(x_train, y_train)

In [11]:
res = NelderMead.optimize(cost, dim, 30000, end_method='max_iter', min_element=-1, 
                                                     max_element=1)
#res = OptimizerLib.NelderMeadLib.NelderMead.optimize(cost, dim, 1e-15)
#x0 = net.unroll_matrixes()[2]
#res = minimize(cost, x0, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})


100%|██████████| 30000/30000 [00:23<00:00, 1298.26it/s]

In [12]:
net.roll_matrixes(res)


Out[12]:
[(array([[ 3.3416322 ],
         [-0.65975633],
         [-7.15284785],
         [ 0.82321242],
         [-2.23277694],
         [ 3.50849477],
         [ 1.49898424],
         [ 1.16733075],
         [ 0.8651943 ],
         [ 2.4517623 ]]), array([[-4.81324085],
         [ 3.59967198],
         [-1.44880909],
         [-0.7100268 ],
         [-0.9568158 ],
         [-1.95109373],
         [ 3.63115557],
         [-4.54506181],
         [-4.54993401],
         [-1.10251821]])),
 (array([[ 1.04900447, -0.21281679, -1.11898258, -3.2377911 , -1.67826279,
           2.71335996, -3.26590409, -5.89409537,  7.17629675, -2.05502357]]),
  array([[ 5.28866039]]))]

In [13]:
y_pred = net.run(x_cv)
plt.plot(x_train[0], y_train[0], 'x', color='red')
plt.plot(x_cv[0], y_pred[0])


Out[13]:
[<matplotlib.lines.Line2D at 0x7f97719170f0>]

In [14]:
net.calculate_cost(x_train, y_train)


Out[14]:
8.6131534670842464e-05