In [1]:
from physlearn.NeuralNet.NeuralNet import NeuralNet
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 = NeuralNet(-1, 1)
In [6]:
net.add_input_layer(1)
net.add(10, tf.sigmoid)
net.add_output_layer(1)
In [7]:
net.compile()
In [8]:
cost_list = net.train('prediction', x_train, y_train, 5, 3000, 0.1)
plt.plot(list(map(lambda item: item ** (-1), cost_list)))
Out[8]:
In [9]:
net.calculate_cost(x_train, y_train)
Out[9]:
In [10]:
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[10]:
In [11]:
res = net.unroll_matrixes()
In [12]:
res[0]
Out[12]:
In [13]:
unroll_vector = res[2]
In [14]:
net.roll_matrixes(unroll_vector)
Out[14]: