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]:
[<matplotlib.lines.Line2D at 0x7f2f87fd4a90>]

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]:
0.82819698289912891

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)


100%|██████████| 300/300 [00:55<00:00,  5.51it/s]

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]:
[<matplotlib.lines.Line2D at 0x7f2f88000a20>]

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


Out[16]:
0.02390859990915839

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)


100%|██████████| 30000/30000 [11:54<00:00, 42.18it/s]
Out[21]:
[<matplotlib.lines.Line2D at 0x7f2f800d0f98>]

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]:
[<matplotlib.lines.Line2D at 0x7f2f8264eb00>]

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


Out[24]:
0.0059302775082353178

In [ ]: