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]:
In [5]:
net = NeuralNetPro.NeuralNetPro(-1, 1)
In [6]:
net.add_input_layer(1)
net.add(10, tf.sigmoid)
net.add_output_layer(1, net.linear)
In [7]:
net.compile()
In [8]:
net.set_train_type('prediction')
In [9]:
dim = net.return_unroll_dim()
In [10]:
net.set_random_matrixes()
net.calculate_cost(x_train, y_train)
Out[10]:
In [11]:
def cost(params):
#params = numpy.array(params)
#print(params.shape)
net.roll_matrixes(params)
return net.calculate_cost(x_train, y_train)
In [12]:
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})
In [13]:
net.roll_matrixes(res)
In [14]:
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[14]:
In [15]:
net.calculate_cost(x_train, y_train)
Out[15]: