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

# Model Parameters
sigmoid_ammount = 25
m = 450 # размер сеток обучения
M = 10 # количество выходных нейронов(базисных функций)
a = -10
b = 10
hidden_ammount = 35
# Сетка для обучения(пока нету оптимизатора, устройчивого к стохастической замене)
train_xi = np.linspace(a, b, m, endpoint=True).reshape(1, m) 
colloc_xi = np.linspace(a/2.0 -1, b/2.0 + 1, M, endpoint=True).reshape(1, M)
#obs_xi = np.linspace(a, b, m, endpoint=True).reshape(1, m) 
%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_ammount, hidden_ammount)
# Выражение, определяющеие образ выходов сети при действии гамильтонианом. 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)


1320

In [3]:
colloc_matrix = net.calc(net_output, {net.x : colloc_xi})
diag_colloc_matrix = np.eye(M)
diag_colloc_matrix.flat[:: M + 1] += -1 + colloc_matrix.diagonal()
normal_colloc_matrix = np.matmul(LA.inv(diag_colloc_matrix), colloc_matrix)
np.fill_diagonal(normal_colloc_matrix, 0)
linear_factor = math_util.norm(normal_colloc_matrix)
print('Colloc matrix: \n', colloc_matrix)
print('Diag of colloc matrix: \n', diag_colloc_matrix)
print('Normal colloc matrix: \n', np.matmul(LA.inv(diag_colloc_matrix), colloc_matrix))
print('Normal colloc matrix - diag: \n', normal_colloc_matrix)
print('Measure of linearity: \n', linear_factor)


Colloc matrix: 
 [[-1.61699685 -1.42607898  0.25849026  1.16710479  0.1756076   0.39783751
   3.95269426  3.02100106  1.32849296  0.13606555]
 [-4.69156374 -4.64471256 -4.21079448 -3.63741955 -1.90965472 -7.23300129
  -6.13350335 -4.1067463  -5.31872037 -5.34344425]
 [ 1.60837135  1.67257108  1.78295176  0.79141348  0.71347198  3.81850881
   2.99125162  2.49922621  2.22867793  2.28374954]
 [ 0.31100462  0.35325281  0.53173009  0.12048277 -0.55066767 -2.28814744
  -3.34914638 -3.10986416 -2.58555626 -1.4681181 ]
 [-3.33607852 -3.37089269 -3.77834779 -4.18081533 -1.85633466 -4.28070499
  -4.02833228 -2.28859143 -1.66330996 -2.08169814]
 [ 0.02862072  0.02309424 -0.28155636  0.61997635  0.42452661  3.81250067
   3.08548003  3.11995585  2.70288392  2.11472358]
 [-1.86094194 -2.01713297 -3.55630657 -3.82959546 -3.27752414  1.52489335
  -0.29527052  0.2759431  -0.6045776  -0.67101562]
 [ 7.49766513  7.43448506  7.48586956  8.19517792  9.51423232  9.56557812
   7.10653253  8.48327908  9.11713424  8.11023025]
 [ 4.21021038  4.07943825  3.41432305  1.88423505  1.38338688  2.33610551
   4.05796094  2.54146871  2.34064231  2.89711195]
 [ 7.89822544  7.78931455  7.09676536  7.14408585  3.50834823  4.99678404
   6.24465734  5.4369611   5.04232374  6.13622343]]
Diag of colloc matrix: 
 [[-1.61699685  0.          0.          0.          0.          0.
   0.          0.          0.          0.        ]
 [ 0.         -4.64471256  0.          0.          0.          0.
   0.          0.          0.          0.        ]
 [ 0.          0.          1.78295176  0.          0.          0.
   0.          0.          0.          0.        ]
 [ 0.          0.          0.          0.12048277  0.          0.
   0.          0.          0.          0.        ]
 [ 0.          0.          0.          0.         -1.85633466  0.
   0.          0.          0.          0.        ]
 [ 0.          0.          0.          0.          0.          3.81250067
   0.          0.          0.          0.        ]
 [ 0.          0.          0.          0.          0.          0.
  -0.29527052  0.          0.          0.        ]
 [ 0.          0.          0.          0.          0.          0.
   0.          8.48327908  0.          0.        ]
 [ 0.          0.          0.          0.          0.          0.
   0.          0.          2.34064231  0.        ]
 [ 0.          0.          0.          0.          0.          0.
   0.          0.          0.          6.13622343]]
Normal colloc matrix: 
 [[ 1.00000000e+00  8.81930590e-01 -1.59858236e-01 -7.21773076e-01
  -1.08601076e-01 -2.46034807e-01 -2.44446628e+00 -1.86827888e+00
  -8.21580429e-01 -8.41470710e-02]
 [ 1.01008699e+00  1.00000000e+00  9.06578057e-01  7.83131249e-01
   4.11145942e-01  1.55725488e+00  1.32053454e+00  8.84176631e-01
   1.14511292e+00  1.15043594e+00]
 [ 9.02083495e-01  9.38091044e-01  1.00000000e+00  4.43878235e-01
   4.00163369e-01  2.14167814e+00  1.67769633e+00  1.40173519e+00
   1.24999340e+00  1.28088129e+00]
 [ 2.58132038e+00  2.93197788e+00  4.41332899e+00  1.00000000e+00
  -4.57050980e+00 -1.89914914e+01 -2.77977212e+01 -2.58116927e+01
  -2.14599674e+01 -1.21852953e+01]
 [ 1.79713205e+00  1.81588630e+00  2.03538072e+00  2.25218836e+00
   1.00000000e+00  2.30599852e+00  2.17004636e+00  1.23285498e+00
   8.96018372e-01  1.12140239e+00]
 [ 7.50707187e-03  6.05750516e-03 -7.38508365e-02  1.62616718e-01
   1.11351222e-01  1.00000000e+00  8.09306096e-01  8.18348932e-01
   7.08953035e-01  5.54681497e-01]
 [ 6.30249827e+00  6.83147429e+00  1.20442317e+01  1.29697860e+01
   1.11000724e+01 -5.16439417e+00  1.00000000e+00 -9.34543355e-01
   2.04753796e+00  2.27254525e+00]
 [ 8.83816867e-01  8.76369266e-01  8.82426417e-01  9.66038938e-01
   1.12152768e+00  1.12758027e+00  8.37710568e-01  1.00000000e+00
   1.07471818e+00  9.56025397e-01]
 [ 1.79874146e+00  1.74287127e+00  1.45871201e+00  8.05007684e-01
   5.91028741e-01  9.98061727e-01  1.73369546e+00  1.08579969e+00
   1.00000000e+00  1.23774228e+00]
 [ 1.28714763e+00  1.26939878e+00  1.15653634e+00  1.16424800e+00
   5.71743886e-01  8.14309338e-01  1.01767112e+00  8.86043535e-01
   8.21730792e-01  1.00000000e+00]]
Normal colloc matrix - diag: 
 [[ 0.00000000e+00  8.81930590e-01 -1.59858236e-01 -7.21773076e-01
  -1.08601076e-01 -2.46034807e-01 -2.44446628e+00 -1.86827888e+00
  -8.21580429e-01 -8.41470710e-02]
 [ 1.01008699e+00  0.00000000e+00  9.06578057e-01  7.83131249e-01
   4.11145942e-01  1.55725488e+00  1.32053454e+00  8.84176631e-01
   1.14511292e+00  1.15043594e+00]
 [ 9.02083495e-01  9.38091044e-01  0.00000000e+00  4.43878235e-01
   4.00163369e-01  2.14167814e+00  1.67769633e+00  1.40173519e+00
   1.24999340e+00  1.28088129e+00]
 [ 2.58132038e+00  2.93197788e+00  4.41332899e+00  0.00000000e+00
  -4.57050980e+00 -1.89914914e+01 -2.77977212e+01 -2.58116927e+01
  -2.14599674e+01 -1.21852953e+01]
 [ 1.79713205e+00  1.81588630e+00  2.03538072e+00  2.25218836e+00
   0.00000000e+00  2.30599852e+00  2.17004636e+00  1.23285498e+00
   8.96018372e-01  1.12140239e+00]
 [ 7.50707187e-03  6.05750516e-03 -7.38508365e-02  1.62616718e-01
   1.11351222e-01  0.00000000e+00  8.09306096e-01  8.18348932e-01
   7.08953035e-01  5.54681497e-01]
 [ 6.30249827e+00  6.83147429e+00  1.20442317e+01  1.29697860e+01
   1.11000724e+01 -5.16439417e+00  0.00000000e+00 -9.34543355e-01
   2.04753796e+00  2.27254525e+00]
 [ 8.83816867e-01  8.76369266e-01  8.82426417e-01  9.66038938e-01
   1.12152768e+00  1.12758027e+00  8.37710568e-01  0.00000000e+00
   1.07471818e+00  9.56025397e-01]
 [ 1.79874146e+00  1.74287127e+00  1.45871201e+00  8.05007684e-01
   5.91028741e-01  9.98061727e-01  1.73369546e+00  1.08579969e+00
   0.00000000e+00  1.23774228e+00]
 [ 1.28714763e+00  1.26939878e+00  1.15653634e+00  1.16424800e+00
   5.71743886e-01  8.14309338e-01  1.01767112e+00  8.86043535e-01
   8.21730792e-01  0.00000000e+00]]
Measure of linearity: 
 3126.0769226315083

In [4]:
cost_function = CostFunction(net)

J = cost_function.get


# Оптимизация
iter_number_de = 10000
iter_number_nm = 1000000
#opt_de = DifferentialEvolution(amount_of_individuals = 6*dim, end_cond = iter_number_de, f=0.55 )
opt_nm = NelderMead(end_cond = iter_number_nm)

In [5]:
optimisation_result = opt_nm.optimize(func=J, dim=dim)
print("J after optimisation: ", J(optimisation_result))
net.roll_matrixes(optimisation_result)


100%|█████████████████████████████████████████████████████████████████████| 1000000/1000000 [2:14:40<00:00, 123.76it/s]
J after optimisation:  7.291782581513275

In [ ]:


In [6]:
print("J after optimisation: ", J(optimisation_result))
net.roll_matrixes(optimisation_result)


J after optimisation:  7.291782581513275

In [7]:
x_obss = np.linspace(-2, 2, 100).reshape(1, 100)
plt.plot(np.sum(x_obss, 0), net_images_value(x_obss)[3,:] )
plt.plot(np.sum(x_obss, 0), net_outs_value(x_obss)[3,:] )


Out[7]:
[<matplotlib.lines.Line2D at 0x2f8b6bf3710>]

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


<Figure size 432x288 with 0 Axes>

In [10]:
#outs_matrix = net.calc(net_output, {net.x : train_xi})
#outs_matrix_file = open("outs_matrix1.txt", "wb")
#np.savetxt(outs_matrix_file, outs_matrix, delimiter=' ')
#outs_matrix_file.close()

In [11]:
number_col = M
#net.roll_matrixes(optimisation_result)
#collocation_grid = rand.random((1,200)) * (np.abs(x_0_max) + np.abs(x_0_min)) - np.abs(x_0_min)
#collocation_grid = np.linspace(x_0_min, x_0_max, number_col, endpoint=True).reshape(1, number_col)
collocation_grid = np.linspace(a, b, number_col, endpoint=True).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(np.matmul(np.transpose(bind_matrix), hamiltonian_matrix), np.matmul(np.transpose(bind_matrix), bind_matrix))
eigvals, eigvecs = eig(hamiltonian_matrix, bind_matrix)
eigvals_norm = np.abs(eigvals)
eigvals_real = np.real(eigvals)
for eigval in eigvals:
    print(eigval)
#print(eigvecs[2,:])


(99.99999999999963+0j)
(97.85337451651172+0j)
(66.3825046458315+0j)
(37.561720321941614+0j)
(-0.07870756619589384+0j)
(11.618516960077384+0j)
(60.49382716049399+0j)
(30.864197530864015+0j)
(1.234567901234585+0j)
(11.11111111111107+0j)

In [49]:
d1_osc.show_wf(4,train_xi)



In [12]:
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 [50]:
x_obs = np.linspace(-5, 5, 100).reshape(1, 100)
for i in range(M):
    y = eigvals[i]*((net.run(x_obs)).transpose()*eigvecs[i,:])
    y = np.sum(y,1)
    y1 = net_images_value(x_obs)[i,:]
    plt.plot(np.sum(x_obs, 0), y)# - y1))


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 [35]:
eigvals[k]


Out[35]:
(11.618516960077384+0j)

In [14]:
a = -10

In [30]:
x_obs = np.linspace(-5, 5, 1000).reshape(1, 1000)
k = 5

y = eigvals[k]*(net.run(x_obs)).transpose()*eigvecs[k,:]
plt.plot(np.sum(x_obs, 0), np.sum(y,1))

h = net_images_value(x_obs).transpose()*eigvecs[k,:]
plt.plot(np.sum(x_obs, 0), np.sum(h,1))


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)
Out[30]:
[<matplotlib.lines.Line2D at 0x2f8ba639668>]

In [ ]: