In [1]:
# Необходмые команды импорта.
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 = 1
iterations = int(1e4)
max_eps = 1e-6
max_t = 1800

#x, grids = get_data(a, b, m_1, space_dim)
#m = x[0].size
x = np.linspace(a,b,m_1).reshape(1,m_1)
m=m_1


D:\Anaconda\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [2]:
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.enable_numpy_mode()
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.return_graph(), axis = 0))))*(1/m) 
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_TFDE(params):
    net.roll_matrixes(params)
    res = getCost()
    return res

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


76

In [3]:
x.shape


Out[3]:
(1, 10)

In [4]:
net.run(x)


Out[4]:
array([[-1.90053654, -1.82540099, -1.76814087, -1.72541088, -1.69524947,
        -1.67604174, -1.66505771, -1.65894085, -1.65474699, -1.65046009]])

In [ ]:


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


Информация:  Is converge: False
Amount of iterations: 10000
Total time: 20.37 s
Reached function value: 0.00013849486192237526
Reason of break: Maximum iterations reached


In [7]:
if (space_dim == 2):
    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)
    
    f_true = net.calc(f, {net.x:x}).reshape((m_1, m_1))
    fig1 = plt.figure()
    ax = Axes3D(fig1)
    ax.plot_surface(grids[0], grids[1], f_true)
    
    error = NN_grid - f_true
    fig2 = plt.figure()
    ax = Axes3D(fig2)
    ax.plot_surface(grids[0], grids[1], error)