In [16]:
# Необходмые команды импорта.
import sys
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
import math_util
%matplotlib inline
# Model Parameters
m = 4500 # размер сеток обучения
M = 4 # количество выходных нейронов(базисных функций)
a = -1000
b = 1000
x_grid = np.linspace(a, b, m, endpoint=True)#.reshape(1, m)
sess = tf.Session()
x = tf.placeholder(tf.double)
trial_func = [tf.sin(x), tf.cos(x), tf.sin(2*x), tf.cos(2*x)]
alpha = tf.Variable(1, dtype=tf.double)
sess.run(tf.initializers.global_variables(), {x:x_grid})
alpha_loc = tf.placeholder(tf.double)
ass_alpha = tf.assign(alpha, alpha_loc)
y_set = [1*tf.sin(x), 0.1*tf.cos(x), alpha*tf.sigmoid(x), -3.1*tf.cos(2*x)]
A = tf.transpose(trial_func)
A_T = trial_func
y_0 = tf.reduce_sum(input_tensor=y_set, axis=0)
y = tf.expand_dims(y_0, -1)
omega = tf.matmul(tf.matmul(tf.matrix_inverse(tf.matmul(A_T, A)), A_T), y)
regression_fit = tf.matmul(tf.transpose(trial_func), omega)
noninvariance_factor = (1 / m) * tf.reduce_sum(tf.square(y - regression_fit))
In [17]:
Out[17]:
In [3]:
def plot_all(x_in, funcs, y, fit):
fig = plt.figure(figsize=(20,10))
func_set_matrix = sess.run(funcs, {x:x_in})
for i in range(M):
plt.plot(x_in, func_set_matrix[i])
plt.plot(x_in, sess.run(y, {x:x_in}), '--')
plt.plot(x_in, sess.run(fit, {x:x_in}), '--')
In [ ]:
noninv_list = []
i_list = []
local_alpha = 1e-5
x_grid_obs = np.linspace(a-5, b+5, 10000, endpoint=True)
for i in range(15):
sess.run(ass_alpha, {alpha_loc:local_alpha})
noninv_list.append(sess.run(noninvariance_factor, {x:x_grid}))
i_list.append(local_alpha)
if i==14:
plot_all(x_grid_obs, trial_func, y, fit)
if i==0:
plot_all(x_grid_obs, trial_func, y, fit)
local_alpha += 5
In [7]:
#plot_all(np.linspace(-1000, 1000, 1000, endpoint=True), y_0)
#regression_res = sess.run(omega, {x:x_grid})
#print(np.sum(regression_res, axis=-1))
#print('Nonivariance term: ', sess.run(noninvariance_factor, {x:x_grid}))
In [ ]:
In [ ]:
In [ ]:
In [ ]: