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
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)
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)
In [59]:
vis = Visualiser(net, net_output, net_sum, M)
vis.plot_four(np.linspace(-5, 5, m, endpoint=True).reshape(1, m) )
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)
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')
In [27]:
np.matmul((net.run(x_obs)).transpose(), np.expand_dims(eigvecs[:,1], -1)).shape
Out[27]:
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]:
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]:
In [11]:
d1_osc.show_wf(1,x_obs)