In [18]:
# Необходмые команды импорта.
import sys
sys.path.append('../physlearn/')
sys.path.append('../source')
import numpy as np
from numpy import linalg as LA
import tensorflow as tf
from matplotlib import pylab as plt
import numpy.random as rand
from physlearn.NeuralNet.NeuralNet import NeuralNet
from physlearn.Optimizer.NelderMead.NelderMead import NelderMead
import math_util
import math

from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib notebook

def make_data(a,b,m):
    x = np.linspace(a, b, m)
    y = np.linspace(a, b, m)
    x_list = []
    y_list = []
    for cur_x in x:
        for cur_y in y:
            x_list.append(cur_x)
            y_list.append(cur_y)
    x_train = np.array([x_list, y_list])
    xgrid, ygrid = np.meshgrid(x, y)
    return x_train, xgrid, ygrid

a = -1.5*math.pi
b = 1.5*math.pi
k = 1
sigmoid_ammount = 50
m_1 = 75
space_dim = 2
iterations = int(5e5)
max_eps = 1e-12

xy, xgrid, ygrid = make_data(a,b,m_1)
m = xy[0].size

In [19]:
net = NeuralNet(-2,2)
net.add_input_layer(space_dim)
net.add(sigmoid_ammount, tf.sigmoid)
net.add_output_layer(1, net.linear)
net.compile()
net.set_random_matrixes()
net_out = net.return_graph()
sess = net.return_session()
dim = net.return_unroll_dim()

f = tf.cos(k*tf.sqrt(tf.reduce_sum(tf.square(net.x), axis = 0)))
J = (tf.reduce_sum(tf.square(f - tf.reduce_sum(net_out, axis = 0))))*(1/m) 

#f = tf.cos(k*x_tf)

def COST(params):
    net.roll_matrixes(params)
    res = net.calc(J, {net.x:xy})
    return res


opt_nm = NelderMead(-2.5,2.5, progress_bar='tqdm')
opt_nm.set_epsilon_and_sd(0.3, 100)

def opt(J, dim, n_it, eps):
    optimisation_result = opt_nm.optimize(J, dim, n_it, eps)
    return optimisation_result

In [20]:
NN_grid = net.run(xy).reshape((m_1, m_1))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xgrid, ygrid, NN_grid)


Out[20]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x2028871ba20>

In [46]:
optimisation_result = opt(COST, dim, iterations, max_eps)
print("J after optimisation: ", COST(optimisation_result.x))
print("Информация: ", optimisation_result)


.... 100000 (100%) 438.287 it\s
J after optimisation:  1.350438245196349e-05
Информация:  Is converge: False
Amount of iterations: 100000
Total time: 228.19 s
Reached function value: 1.350438245196349e-05
Reason of break: Maximum iterations reached


In [47]:
NN_grid = net.run(xy).reshape((m_1, m_1))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xgrid, ygrid, NN_grid)


(1, 5625)
Out[47]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x1bdb56cb710>

In [48]:
f_true = net.calc(f, {net.x:xy})
f_true = net.calc(f, {net.x:xy}).reshape((m_1, m_1))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xgrid, ygrid, f_true)


(1, 5625)
Out[48]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x1bdb0e64588>

In [49]:
error = NN_grid - f_true
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(xgrid, ygrid, f_true)


(1, 5625)
Out[49]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x1bdb0e94fd0>

In [50]:
J_fin = optimisation_result.cost_function
mse = math_util.MSE(error)
std_err = math_util.std_err(error)
print(J_fin, ' ', mse, ' ', std_err)


1.350438245196349e-05   0.07596215129229461   0.2756368227867107

In [51]:
COST(optimisation_result.x)


Out[51]:
1.350438245196349e-05