In [14]:
# Необходмые команды импорта.
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.Optimizer import Optimizer

import math_util
import math

from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib notebook

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

a = 0
b = 2*math.pi
k = 1
sigmoid_ammount = 25
m_1 = 10
space_dim = 2
iterations = int(1e4)
max_eps = 1e-6
max_t = 1800

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


Out[14]:
'\ndef make_data(N):\n    x = np.linspace(a, b, m_1)\n    y = np.linspace(a, b, m_1)\n    #x_train = numpy.array([x, y])\n    x_list = []\n    y_list = []\n    for cur_x in x:\n        for cur_y in y:\n            x_list.append(cur_x)\n            y_list.append(cur_y)\n    x_train = np.array([x_list, y_list])\n    xgrid, ygrid = np.meshgrid(x, y)\n\n    #zgrid = rosenbrock((xgrid, ygrid), N)\n    return x_train, xgrid, ygrid\n\n\nx2, xg, yg = make_data(1)\n\n'

In [15]:
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()
sess = net.return_session()
dim = net.return_unroll_dim()
f = tf.cos(k*tf.sqrt(tf.reduce_sum(tf.square(net.x), axis = 0)))
print(dim)

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


101

In [16]:
NN_grid = net.run(x).reshape((m_1,m_1))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(grids[0], grids[1], NN_grid)


Out[16]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x12c802155f8>

In [ ]:


In [ ]:


In [ ]:
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 [ ]:
optimisation_result = opt.optimize(COST, dim, optimizer=opt.diff_evolution_tf, end_cond = iterations, max_time=max_t, min_func_value=max_eps)
net.roll_matrixes(optimisation_result.x)
print("Информация: ", optimisation_result)

In [ ]:
if (space_dim == 2):
    NN_grid = net.run(x).reshape((m_1, m_1))
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.plot_surface(xgrid, ygrid, NN_grid)
    
    f_true = net.calc(f, {net.x:x}).reshape((m_1, m_1))
    fig1 = plt.figure()
    ax = Axes3D(fig1)
    ax.plot_surface(xgrid, ygrid, f_true)
    
    error = NN_grid - f_true
    fig2 = plt.figure()
    ax = Axes3D(fig2)
    ax.plot_surface(xgrid, ygrid, error)