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]:
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)))
Out[29]:
In [30]:
net.calculate_cost(x_train, y_train)
Out[30]:
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]:
In [32]:
res = net.unroll_matrixes()
In [33]:
res[0]
Out[33]:
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]:
In [ ]: