In [1]:
# Необходмые команды импорта.
import sys
#import os
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
from IPython.display import clear_output
from physlearn.NeuralNet.NeuralNet import NeuralNet
from physlearn.Optimizer.NelderMead.NelderMead import NelderMead
from ann_solver import AnnSolver
import d1_osc
import ann_constructor
import math_util
from visualiser import Visualiser

# Model Parameters
n_hid1 = 15
m = 450 # размер сеток обучения
M = 6 # количество выходных нейронов(базисных функций)
a = -10
b = 10
n_hid2 = 15
%matplotlib inline

# ANN
net, net_output, net_sum, sess = ann_constructor.return_separated_deep_net_expressions(M, n_hid1, n_hid2)
# Выражение, определяющеие образ выходов сети при действии гамильтонианом. Task-dependant
dim = net.return_unroll_dim()
print(dim)



herm_grid = [0.0, -0.70710678, 1.22474487e+00, -1.65068012, 9.58572465e-01, 2.02018287e+00]
herm_grid = np.array(herm_grid)
solver = AnnSolver(net, M, 'gaus')
solver.define_approximation_grid(a, b, m)
solver.set_linearity_grid(herm_grid)
solver.compile()
J = solver.get_cost_func()

trial_func = solver.get_trial_func()
func_sum = tf.reduce_sum(input_tensor=trial_func, axis=0)
images = solver.get_images()
images_sum = tf.reduce_sum(input_tensor=images, axis=0)


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

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


1716

In [3]:
net.calc(trial_func, {net.x: np.linspace(a, b, 1000, endpoint=True).reshape(1, 1000)}).shape


Out[3]:
(6, 1000)

In [2]:
optimisation_result = opt(J, dim, int(3e6), 1e-3)
print("J after optimisation: ", J(optimisation_result.x))
print("Информация: ", optimisation_result)


.    2999911 (99%) 77.098 it\s
J after optimisation:  9.056776442855
Информация:  Is converge: False
Amount of iterations: 3000000
Total time: 38911.44 s
Reached function value: 9.056776442855
Reason of break: Maximum iterations reached

...  3000000 (100%) 77.098 it\s

In [3]:
xi_obs = np.linspace(a, b, 1000, endpoint=True)
vis = Visualiser(solver)
vis.plot_four(xi_obs.reshape(1, 1000))


<Figure size 432x288 with 0 Axes>

In [4]:
y1 = net.calc(trial_func, {net.x : xi_obs.reshape(1, 1000)})
for i in range(M):
    func_i = y1[i,:]
    plt.plot(xi_obs.reshape(1, 1000)[0], func_i)



In [5]:
from scipy.linalg import eig
#coll_grid = np.linspace(-2,2,M).reshape(1, M)
coll_grid = math_util.calc_hermite_grid_xi(M).reshape(1, M)
ham_matrix = net.calc(images, {net.x:coll_grid})
eigvals, eigvecs = eig(ham_matrix)
print(eigvals)
func_matrix = net.calc(trial_func, {net.x:xi_obs.reshape(1, 1000)})
for i in range(M):
    y = np.matmul(np.transpose(func_matrix), eigvecs[:,i])
    plt.plot(xi_obs, y)


[-8.48563494e+00+0.j          3.31841518e+00+0.j
  3.68890251e-01+0.j          4.98151033e-02+0.08969315j
  4.98151033e-02-0.08969315j -1.10354575e-02+0.j
  2.58356703e-03+0.j        ]
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 [6]:
def calc_i_eigfunc(i, x_in):
    func_matrix = net.calc(trial_func, {net.x:x_in.reshape(1, 1000)})
    return np.matmul(np.transpose(func_matrix), eigvecs[:,i])

def show_i_eigfunc(i, x_in):
    plt.plot(x_in, calc_i_eigfunc(i, x_in))

In [7]:
d1_osc.show_wf(1, xi_obs.reshape(1, 1000))
show_i_eigfunc(0, xi_obs)


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 [2]:
y_true = d1_osc.wf(1, xi_obs)
y_hyp = calc_i_eigfunc(0, xi_obs)
print(y_true.shape, ' ', y_hyp.shape)
print(y_true/ y_hyp)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-57ca538f5eaa> in <module>()
      1 
----> 2 y_true = d1_osc.wf(1, xi_obs)
      3 y_hyp = calc_i_eigfunc(0, xi_obs)
      4 print(y_true.shape, ' ', y_hyp.shape)
      5 print(y_true/ y_hyp)

NameError: name 'xi_obs' is not defined

In [ ]:
xi_obs = np.linspace(-3, 3, 1000, endpoint=True)

from scipy.linalg import eig
#coll_grid = np.linspace(-2,2,M).reshape(1, M)
coll_grid = math_util.calc_hermite_grid_xi(M).reshape(1, M)
ham_matrix = np.transpose(net.calc(images, {net.x:coll_grid}))
eigvals, eigvecs = eig(ham_matrix)
print(eigvals)
func_matrix = net.calc(trial_func, {net.x:xi_obs.reshape(1, 1000)})
for i in range(M):
    y = np.matmul(np.transpose(func_matrix), eigvecs[:,i])
    plt.plot(xi_obs, y)

In [ ]:
d1_osc.show_wf(0, xi_obs.reshape(1, 1000))