In [3]:
from physlearn.NeuralNet.NeuralNet import NeuralNet
import numpy
import tensorflow as tf
from math import pi
import matplotlib.pyplot as plt
from physlearn.Optimizer import NelderMead
from scipy.optimize import minimize
%matplotlib inline
In [ ]:
x_train = numpy.linspace(0, 2 * pi, 30).reshape(1, 30)
y_train = numpy.sin(x_train[0]).reshape(1, 30)
In [ ]:
x_cv = numpy.linspace(0, 2 * pi, 1000).reshape(1, 1000)
y_cv = numpy.sin(x_cv[0]).reshape(1, 1000)
In [ ]:
plt.plot(x_train[0], y_train[0], 'x', color='red')
In [ ]:
net = NeuralNet.NeuralNet(-1, 1, 1)
In [ ]:
net.add_input_layer(1)
net.add(10, tf.sigmoid)
In [ ]:
net.compile()
In [ ]:
dim = net.return_unroll_dim()
In [ ]:
def cost(params):
params = numpy.array(params)
#print(params.shape)
net.roll_matrixes(params)
return net.calculate_cost(x_train, y_train)
In [ ]:
#res = OptimizerLib.NelderMeadLib.NelderMead.optimize(cost, dim, 5000, end_method='max_iter')
#res = OptimizerLib.NelderMeadLib.NelderMead.optimize(cost, dim, 1e-9)
x0 = net.unroll_matrixes()[2]
res = minimize(cost, x0, method='nelder-mead', options={'xtol': 1e-8, 'disp': True})
In [ ]:
net.roll_matrixes(res.x)
In [ ]:
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])
In [ ]:
net.calculate_cost(x_train, y_train)
In [ ]:
net.unroll_matrixes()[2]
In [ ]: