In [1]:
from physlearn.NeuralNet.NeuralNetPro import NeuralNetPro
import numpy
import copy
import tensorflow as tf
from math import pi
import matplotlib.pyplot as plt
from physlearn.Optimizer import DifferentialEvolution
from physlearn.Optimizer import RandomEvolutionStrategy
%matplotlib inline
In [2]:
x_train = numpy.linspace(0, 2 * pi, 15).reshape(1, 15)
y_train = numpy.sin(x_train[0]).reshape(1, 15)
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(-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()
In [11]:
net.calculate_cost(x_train, y_train)
Out[11]:
In [12]:
def cost(params):
net.roll_matrixes(params)
return net.calculate_cost(x_train, y_train)
In [13]:
res = DifferentialEvolution.optimize(cost, 500, dim, 300, f=0.3, p=0.9, min_element=-1, max_element=1)
In [14]:
net.roll_matrixes(res)
In [15]:
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[15]:
In [16]:
net.calculate_cost(x_train, y_train)
Out[16]:
In [21]:
net.roll_matrixes(res)
res_new = RandomEvolutionStrategy.optimize(cost, dim, 0.01, 40, 30000, 0.005, x0 = copy.deepcopy(res))
cost_list = res_new[0]
params = res_new[1]
plt.plot(cost_list)
Out[21]:
In [22]:
net.roll_matrixes(params)
In [23]:
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[23]:
In [24]:
net.calculate_cost(x_train, y_train)
Out[24]:
In [ ]: