In [1]:
from physlearn.NeuralNet.NeuralNetPro import NeuralNetPro
from physlearn.Optimizer import RandomEvolutionStrategy
import numpy
import tensorflow as tf
from math import pi
import matplotlib.pyplot as plt
%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 0x7f73ce653438>]

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]:
def cost(params):
    net.roll_matrixes(params)
    return net.calculate_cost(x_train, y_train)

In [12]:
res = RandomEvolutionStrategy.optimize(cost, dim, 0.35, 10, 100000, 0.001)
cost_list = res[0]
plt.plot(cost_list)


100%|██████████| 10000/10000 [01:04<00:00, 154.34it/s]
Out[12]:
[<matplotlib.lines.Line2D at 0x7f73c8d2c828>]

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


Out[13]:
0.28866396260432092

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