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 = 200 # размер сеток обучения
M = 6 # количество выходных нейронов(базисных функций)
a = -10
b = 10
#n_hid2 = 9
%matplotlib inline

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

solver = AnnSolver(ann = net, ground_method='gaus')
solver.define_approximation_grid(a, b, m)
solver.define_linearity_grid(M)
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


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
276

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


.    1745185 (58%) 313.114 it\s
J after optimisation:  0.2638686724325454
Информация:  Is converge: True
Amount of iterations: 1745186
Total time: 5574.50 s
Reached function value: 0.2638686724325454
Reason of break: Local minimum reached

..   1745186 (58%) 313.114 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 [7]:
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)


[-2.03085570e+00+0.j  1.37350854e+00+0.j -1.34177490e-01+0.j
  4.69622920e-02+0.j -7.59795075e-03+0.j -6.09298003e-05+0.j]

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



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

In [6]:
y_true = d1_osc.wf(0, 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-6-3e7a32a5dc56> in <module>()
      1 
      2 y_true = d1_osc.wf(0, 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 'calc_i_eigfunc' is not defined