In [40]:
# Необходмые команды импорта.
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
import math_util
%matplotlib inline
# Model Parameters

m = 1500 # размер сеток обучения
M = 4 # количество выходных нейронов(базисных функций)
a = -15
b = 15
x_grid = np.linspace(a, b, m, endpoint=True)#.reshape(1, m)

sess = tf.Session()
x = tf.placeholder(tf.double)
w = tf.Variable([1,1,1,1], dtype=tf.double)
trial_func = [w[0]*tf.sin(x), w[1]*tf.cos(x), w[2]*tf.sin(2*x), w[3]*tf.cos(5*x)]
y_set = [1*tf.sin(x), 0*tf.cos(x), 2.5*tf.sin(2*x), -7.1*tf.cos(5*x)]
#func_sum = tf.reduce_sum(input_tensor=trial_func, axis=0)
sess.run(tf.initializers.global_variables(), {x:x_grid})




#first_deriative = tf.gradients(trial_func, x)[0]

#images = tf.gradients(first_deriative, x)[0]
#images_sum = tf.reduce_sum(input_tensor=images, axis=0)

In [41]:
bas_mat = sess.run(trial_func, {x:np.linspace(a, b, m, endpoint=True)})

In [42]:
np.array(bas_mat).shape


Out[42]:
(4, 1500)

In [44]:
plt.plot(x_grid, bas_mat[1])


Out[44]:
[<matplotlib.lines.Line2D at 0x2a7ec7815f8>]

In [52]:
np.array(sess.run(y, {x:x_grid})).shape


Out[52]:
(1500, 1)

In [51]:
A = tf.transpose(trial_func)
A_T = trial_func

y = tf.reduce_sum(input_tensor=y_set, axis=0)
y = tf.expand_dims(y, -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 [53]:
regression_res = sess.run(omega, {x:x_grid})

In [55]:
regression_res


Out[55]:
array([[ 1.0000000e+00],
       [ 3.6429193e-17],
       [ 2.5000000e+00],
       [-7.1000000e+00]])

In [56]:
sess.run(noninvariance_factor, {x:x_grid})


Out[56]:
7.101325868799547e-30

In [ ]: