In [19]:
# Необходмые команды импорта.
import sys
sys.path.append('../')
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 import Optimizer
from physlearn.Optimizer.Optimizer import Optimizer

import math_util
import math

from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib notebook

def make_all_variants(A):
    d = A.shape[0]
    m = A.shape[1]
    res  = np.zeros((d, m ** d))
    for i in range(d):
        for index, item in enumerate(A[i]):
            for j in range(i + 2):
                left_break = (m ** (d - i - 1)) * index + j * m ** (d - i)
                right_break = (m ** (d - i - 1)) * (index + 1) + j * m ** (d - i) 
                res[i, left_break:right_break] = item
    return res

def make_data(a,b, m_l, dim):
    x = np.empty((dim, m_l))
    for i in range(dim):
        x[i] = np.linspace(a, b, m_l)
    x_train = make_all_variants(x)
    return x_train

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

x = make_data(a,b,m_1,space_dim)
m =x[0].size

In [20]:
x.shape


Out[20]:
(2, 900)

In [21]:
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)))


def getCost():
    y = net.return_graph()
    return (tf.reduce_sum(tf.square(f - tf.reduce_sum(y, axis = 0))))*(1/m) 

def COST(params):
    net.roll_matrixes(params)
    res = getCost()
    return res

In [22]:
print(dim)


101

In [23]:
opt = Optimizer()
opt.params_dict['number_of_individuals'] = dim
opt.params_dict['sess'] = net.sess
opt.params_dict['placeholder'] = net.x
opt.params_dict['x'] = x

In [24]:
optimisation_result = opt.optimize(COST, dim, optimizer=opt.diff_evolution_tf, end_cond = 25000, max_time=18000, min_func_value=1e-12)
net.roll_matrixes(optimisation_result.x)
print("Информация: ", optimisation_result)


Информация:  Is converge: False
Amount of iterations: 25000
Total time: 1867.52 s
Reached function value: 6.811903259078767e-05
Reason of break: Maximum iteration reached


In [83]:
NN_grid = net.run(x).reshape(m)
f_true = net.calc(f, {net.x:x}).reshape(m)
error = NN_grid - f_true

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


6.811903259078767e-05   6.811903259078767e-05   0.008258014567430493

In [84]:
x[0].shape
NN_grid = net.run(x).reshape(m)
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(x[0], x[1], NN_grid.reshape(1,m))


Out[84]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x2918aa867f0>

In [ ]:


In [ ]:


In [115]:
x = np.linspace(a,b,6)
ggg = [x for i in range(2)]
GRIDS = np.meshgrid(*ggg)
GRIDS[0].shape


Out[115]:
(6, 6)

In [109]:



Out[109]:
1

In [ ]:


In [124]:
def ARGH(a,b,m, dim):
    x = np.linspace(a,b,m)
    g = np.meshgrid(*[x for i in range(dim)])
    vect = np.zeros((dim, m ** dim))
    for i in range(g[0].size):
        temp = []
        for j in range(dim):
            temp.append(g[j].flat[i]) # ТУТ К i обращение по единичному индексу в матрице, а не по строка-столбец
        vect[:,i] = np.array(temp)
    return vect, g

In [136]:
c = 15
v, gr = ARGH(a,b,c,2)
NN_grid = net.calc(f, {net.x:v}).reshape(c**2)
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(v[0], v[1], NN_grid.reshape(1,c**2))


Out[136]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x2918da0f748>

In [134]:
v[0].shape


Out[134]:
(225,)

In [129]:
gr[0].shape


Out[129]:
(5, 5)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [58]:
def make_data1(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

xy, xgrid,ygrid = make_data(a,b,m_1)

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


Out[59]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x291884d5128>

In [60]:
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)


Out[60]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x29188f03b70>

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


Out[45]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x29183facdd8>

In [47]:
math_util.MSE(error)


Out[47]:
2.1219057467990288