In [1]:
# Необходмые команды импорта.
import sys
import os
sys.path.append(os.path.join(sys.path[0], '../'))
sys.path.append(os.path.join(sys.path[0], '../source/'))
import numpy as np
from numpy import linalg as LA
import tensorflow as tf
from matplotlib import pylab as plt
from IPython.display import clear_output
from physlearn.NeuralNet.NeuralNetPro import NeuralNetPro
from physlearn.Optimizer.NelderMead.NelderMead import NelderMead
from CostFunction import CostFunction
import d1_osc
import ann_constructor
import math_util
from visualiser import Visualiser

# Model Parameters
sigmoid_amount = 25
m = 350 # размер сеток обучения
M = 5 # количество выходных нейронов(базисных функций)
a = -8
b = 8
hidden_amount = 25
%matplotlib inline


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]:
# ANN
net, net_output, net_sum, sess = ann_constructor.return_deep_net_expressions(M, sigmoid_amount, hidden_amount)
# Выражение, определяющеие образ выходов сети при действии гамильтонианом. Task-dependant
first_deriative = tf.gradients(net_output, net.x)[0]
net_images = (-(tf.gradients(first_deriative, net.x)[0]) + tf.multiply(tf.square(net.x), net.output))
net_images_sum = tf.reduce_sum(input_tensor = net_images, axis = 0)

def net_outs_value(x):
    return net.calc(net_output, {net.x : x})

def net_sum_value(x):
    return net.calc(net_sum, {net.x : x})

def net_images_value(x):
    return net.calc(net_images, {net.x: x})

def net_images_sum_value(x):
    return net.calc(net_images_sum, {net.x: x})

dim = net.return_unroll_dim()
print(dim)


830

In [3]:
cost_function = CostFunction(net)
cost_function.define_approximation_grid(a, b, m)
cost_function.define_linearity_grid(M)
cost_function.compile()
J = cost_function.get_cost_func()

# Оптимизация
opt_nm = NelderMead(-2.5,2.5)
opt_nm.set_epsilon_and_sd(0.3, 100)

In [4]:
optimisation_result = opt_nm.optimize(J, dim, 10**6, 10**(-2))
print("J after optimisation: ", J(optimisation_result.x))
net.roll_matrixes(optimisation_result.x)
print("Ебала: ", optimisation_result)


100%|█████████████████████████████████████████████████████████████████████| 1000000/1000000 [1:31:57<00:00, 181.25it/s]
.    1000000 (100%) 0.000 it\s
J after optimisation:  0.2332023075496637
Ебала:  Is converge: False
Amount of iterations: 1000000
Total time: 5517.19 s
Reached function value: 0.2332023075496637
Reason of break: Maximum iterations reached


In [59]:
vis = Visualiser(net, net_output, net_sum, M)
vis.plot_four(np.linspace(-5, 5, m, endpoint=True).reshape(1, m) )


<Figure size 432x288 with 0 Axes>

In [6]:
number_col = M

collocation_grid = math_util.calc_hermite_grid_xi(number_col).reshape(1, number_col)
hamiltonian_matrix = net_images_value(collocation_grid)
bind_matrix = net.run(collocation_grid)
from scipy.linalg import eig
eigvals, eigvecs = eig(hamiltonian_matrix, bind_matrix)
for eigval in eigvals:
    print(eigval)


(48.77193623378373+0j)
(3.6774549628751076+0j)
(2.627882048688753+0j)
(-0.016291213551275108+0j)
(0.8697966610360964+0j)

In [7]:
x = np.linspace(0,25,1000)
i = 0

for eigval in eigvals:
    i+=1
    plt.plot(x, eigval+0*x)
    plt.plot(1+i, eigval, 'x')


D:\Anaconda\lib\site-packages\numpy\core\numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

In [27]:
np.matmul((net.run(x_obs)).transpose(), np.expand_dims(eigvecs[:,1], -1)).shape


Out[27]:
(5, 1000)

In [58]:
x_obs = np.linspace(-3, 3, 100).reshape(1, 100)
for i in range(1,M):
    y = (np.matmul((net.run(x_obs)).transpose(), np.expand_dims(eigvecs[:,1], -1)))#*eigvals[i]
    y1 = np.matmul((net_images_value(x_obs)).transpose(), np.expand_dims(eigvecs[:,1], -1))
    plt.plot(np.sum(x_obs, 0), y )#- y1)



In [53]:
eigvecs[:,3]


Out[53]:
array([-0.57310378, -0.49817179, -0.59049586, -0.22033261, -0.16169448])

In [57]:
x_obs = np.linspace(-5, 5, 1000).reshape(1, 1000)
k = 3
y = (np.matmul((net.run(x_obs)).transpose(),eigvecs[:,k]))#*eigvals[i]
plt.plot(np.sum(x_obs, 0), y, "b")
y1 = np.matmul((net_images_value(x_obs)).transpose(),eigvecs[:,k])
plt.plot(np.sum(x_obs, 0), y1, "g")
plt.plot(np.sum(x_obs, 0), y - y1,"r--")


Out[57]:
[<matplotlib.lines.Line2D at 0x1b1ce42fcc0>]

In [11]:
d1_osc.show_wf(1,x_obs)