In [21]:
from physlearn.NeuralNet.NeuralNet import NeuralNet
import numpy
import tensorflow as tf
from math import pi
import matplotlib.pyplot as plt
%matplotlib inline

In [22]:
x_train = numpy.linspace(0, 2 * pi, 30).reshape(1, 30)
y_train = numpy.sin(x_train[0]).reshape(1, 30)

In [23]:
x_cv = numpy.linspace(0, 2 * pi, 1000).reshape(1, 1000)
y_cv = numpy.sin(x_cv[0]).reshape(1, 1000)

In [24]:
plt.plot(x_train[0], y_train[0], 'x', color='red')


Out[24]:
[<matplotlib.lines.Line2D at 0x21d58a48940>]

In [25]:
net = NeuralNet(-1, 1)

In [26]:
net.add_input_layer(1)
net.add(10, tf.sigmoid)
net.add_output_layer(1, net.linear)

In [27]:
net.compile()

In [28]:
net.set_train_type('prediction')

In [29]:
cost_list = net.train(x_train, y_train, 5, 30000, 0.1)
plt.plot(list(map(lambda item: item ** (-1), cost_list)))


100%|██████████████████████████████████████████████████████████████████████████| 30000/30000 [00:10<00:00, 2952.58it/s]
Out[29]:
[<matplotlib.lines.Line2D at 0x21d58d1f400>]

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


Out[30]:
0.001500068960113422

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

In [32]:
res = net.unroll_matrixes()

In [33]:
res[0]


Out[33]:
[(array([[-1.15091717],
         [ 0.22875304],
         [ 0.37095079],
         [-0.88138196],
         [-0.45491523],
         [-0.96166073],
         [-2.16725196],
         [-0.45716576],
         [-0.35028395],
         [-1.77260216]]), array([[ 1.66324539],
         [-0.12660553],
         [ 0.3926052 ],
         [-0.92717115],
         [ 1.79901331],
         [ 5.69109085],
         [ 1.01958073],
         [ 1.82154574],
         [-0.02795594],
         [ 6.17501362]])),
 (array([[ 1.89630988,  0.14896772,  0.08176532,  0.42900751, -0.84548297,
          -2.52183042, -2.32210254, -0.86795163, -0.80078559,  3.00372402]]),
  array([[1.26520015]]))]

In [34]:
unroll_vector = res[2]

In [35]:
net.roll_matrixes(unroll_vector)

In [36]:
sess = net.return_session()
output = net.return_graph()

In [37]:
y_pred = sess.run(output, {net.x: x_cv})
plt.plot(x_train[0], y_train[0], 'x', color='red')
plt.plot(x_cv[0], y_pred[0])


Out[37]:
[<matplotlib.lines.Line2D at 0x21d58c77a20>]

In [ ]: