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]:
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)
Out[12]:
In [13]:
net.calculate_cost(x_train, y_train)
Out[13]:
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]: