In [22]:
# Необходмые команды импорта.
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
import numpy.random as rand
from physlearn.NeuralNet.NeuralNet import NeuralNet
from physlearn.Optimizer.NelderMead.NelderMead import NelderMead
import d1_osc
import ann_constructor
import math_util
from lagaris2d import LagarisSolver2d

from lagaris_mc import LagarisSolver_mc

from visualiser import Visualiser
from mpl_toolkits.mplot3d.axes3d import Axes3D
%matplotlib notebook

def grid_vect(a,b,N):
    x = np.linspace(a, b, N, endpoint=True)
    h = x[1] - x[0]
    return np.mgrid[a:b+h:h, a:b+h:h].reshape(2,-1).T

def get_meshgrid(a,b,N):
    x = np.linspace(a, b, N, endpoint=True)
    return np.meshgrid(x,x)

n_sig = 25
a = -5
b = 5
mc_n = 30000
dim = 1

N = 60
xy = np.linspace(a, b, N, endpoint=True)
xy = xy.reshape(dim,xy.size)
#train_xy = np.transpose(xy)
trapz_xy = train_xy

In [26]:
lagar.calc_H_psi(xy,1)


Out[26]:
array([[ 7.91263926e-10,  4.06871630e-09,  1.96691847e-08,
         8.93789062e-08,  3.81688073e-07,  1.53139610e-06,
         5.77063354e-06,  2.04142306e-05,  6.77623659e-05,
         2.10913933e-04,  6.15065759e-04,  1.67869081e-03,
         4.28193215e-03,  1.01884925e-02,  2.25564332e-02,
         4.63013144e-02,  8.76893420e-02,  1.52161420e-01,
         2.39472875e-01,  3.36574249e-01,  4.11785092e-01,
         4.17521432e-01,  3.08801305e-01,  7.54235835e-02,
        -2.31704195e-01, -5.09475569e-01, -6.58145622e-01,
        -6.53376697e-01, -5.77980569e-01, -5.78276012e-01,
        -7.66037270e-01, -1.13103070e+00, -1.52754958e+00,
        -1.75105775e+00, -1.65813130e+00, -1.24999063e+00,
        -6.65843833e-01, -9.82962084e-02,  3.05408964e-01,
         4.95158313e-01,  5.06898286e-01,  4.16149594e-01,
         2.93750145e-01,  1.84011220e-01,  1.04128009e-01,
         5.38322748e-02,  2.56216251e-02,  1.12885698e-02,
         4.62260710e-03,  1.76465905e-03,  6.29445004e-04,
         2.10158592e-04,  6.57701670e-05,  1.93141815e-05,
         5.32679718e-06,  1.38070792e-06,  3.36534631e-07,
         7.71706596e-08,  1.66546909e-08,  3.38393991e-09]])

In [24]:
lagar = LagarisSolver_mc(dim)
lagar.define_psi(n_sig)
net_x = lagar.get_net_x()
dim = lagar.get_dim()
sess = lagar.get_sess()

V = tf.square(net_x)
lagar.define_H_psi(V)

In [27]:
J_mc = lagar.get_cost_func(a,b, train_xy, mc_n)

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

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


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1277     try:
-> 1278       return fn(*args)
   1279     except errors.OpError as e:

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1262       return self._call_tf_sessionrun(
-> 1263           options, feed_dict, fetch_list, target_list, run_metadata)
   1264 

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1349         self._session, options, feed_dict, fetch_list, target_list,
-> 1350         run_metadata)
   1351 

InvalidArgumentError: In[0] mismatch In[1] shape: 1 vs. 60: [25,1] [60,1] 0 0
	 [[Node: MatMul_6 = BatchMatMul[T=DT_DOUBLE, adj_x=false, adj_y=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_23_0_1, _arg_Placeholder_21_0_0)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-28-3af693b8ce24> in <module>()
----> 1 optimisation_result = opt(J_mc, dim, int(9e3), 1e-6)
      2 print("J after optimisation: ", J_mc(optimisation_result.x))
      3 print("Информация: ", optimisation_result)

<ipython-input-27-4355fcf9837a> in opt(J, dim, n_it, eps)
      4 opt_nm.set_epsilon_and_sd(0.3, 100)
      5 def opt(J, dim, n_it, eps):
----> 6     optimisation_result = opt_nm.optimize(J, dim+1, n_it, eps)
      7     return optimisation_result

~\GitKraken\SPBU_COMP_PHYS_NN_QM\physlearn\physlearn\Optimizer\NelderMead\NelderMeadAbstract.py in optimize(self, func, dim, end_cond, min_cost)
    139         self.y_points = numpy.zeros(self.dim + 1)
    140         for index, x in enumerate(self.x_points):
--> 141             self.y_points[index] = self.func(x)
    142 
    143         self.method_types = [0, 0, 0, 0]

~\GitKraken\SPBU_COMP_PHYS_NN_QM\source\lagaris_mc.py in J(self, params)
     88         self.net.roll_matrixes(params[0:-1])
     89         beta_loc = params[-1]
---> 90         psi = self.calc_psi(self.train_x, beta_loc)
     91         H_psi = self.calc_H_psi(self.train_x, beta_loc)
     92         mean_E = self.integrate_mc(self.psi_H_psi_integrand, self.a, self.b, self.mc_n, beta_loc)

~\GitKraken\SPBU_COMP_PHYS_NN_QM\source\lagaris_mc.py in calc_psi(self, x, beta_loc)
     58 
     59     def calc_psi(self, x, beta_loc):
---> 60         return self.net.calc(self.psi, {self.net.x : x, self.beta : beta_loc})
     61 
     62 

~\GitKraken\SPBU_COMP_PHYS_NN_QM\physlearn\physlearn\NeuralNet\NeuralNet.py in calc(self, calc_var, d)
    311         """
    312         d.update(self.placeholders_dict)  # Добавляем в словарь d placeholder для матриц весов
--> 313         return self.sess.run(calc_var, d)
    314 
    315     def run(self, inputs):

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    875     try:
    876       result = self._run(None, fetches, feed_dict, options_ptr,
--> 877                          run_metadata_ptr)
    878       if run_metadata:
    879         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1098     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1099       results = self._do_run(handle, final_targets, final_fetches,
-> 1100                              feed_dict_tensor, options, run_metadata)
   1101     else:
   1102       results = []

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1270     if handle is None:
   1271       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1272                            run_metadata)
   1273     else:
   1274       return self._do_call(_prun_fn, handle, feeds, fetches)

D:\Anaconda\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1289         except KeyError:
   1290           pass
-> 1291       raise type(e)(node_def, op, message)
   1292 
   1293   def _extend_graph(self):

InvalidArgumentError: In[0] mismatch In[1] shape: 1 vs. 60: [25,1] [60,1] 0 0
	 [[Node: MatMul_6 = BatchMatMul[T=DT_DOUBLE, adj_x=false, adj_y=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_23_0_1, _arg_Placeholder_21_0_0)]]

Caused by op 'MatMul_6', defined at:
  File "D:\Anaconda\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\Anaconda\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Anaconda\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "D:\Anaconda\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
    app.start()
  File "D:\Anaconda\lib\site-packages\ipykernel\kernelapp.py", line 486, in start
    self.io_loop.start()
  File "D:\Anaconda\lib\site-packages\tornado\platform\asyncio.py", line 127, in start
    self.asyncio_loop.run_forever()
  File "D:\Anaconda\lib\asyncio\base_events.py", line 422, in run_forever
    self._run_once()
  File "D:\Anaconda\lib\asyncio\base_events.py", line 1432, in _run_once
    handle._run()
  File "D:\Anaconda\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "D:\Anaconda\lib\site-packages\tornado\platform\asyncio.py", line 117, in _handle_events
    handler_func(fileobj, events)
  File "D:\Anaconda\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda\lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "D:\Anaconda\lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "D:\Anaconda\lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "D:\Anaconda\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "D:\Anaconda\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "D:\Anaconda\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "D:\Anaconda\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "D:\Anaconda\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "D:\Anaconda\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2903, in run_ast_nodes
    if self.run_code(code, result):
  File "D:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-24-f9f29cf8b4c7>", line 2, in <module>
    lagar.define_psi(n_sig)
  File "../source\lagaris_mc.py", line 39, in define_psi
    self.net, self.net_output, net_sum, self.sess = ann_constructor.return_net_expressions_multidim(self.space_dim, 1, sigmoid_ammount)
  File "../source\ann_constructor.py", line 24, in return_net_expressions_multidim
    net.compile()
  File "../physlearn\physlearn\NeuralNet\NeuralNet.py", line 249, in compile
    current_layer = layer.activation_func(layer * self.x)
  File "../physlearn\physlearn\NeuralNet\Layer\Layer.py", line 30, in __mul__
    return self.weight_matrix * other + self.bias_vector
  File "../physlearn\physlearn\NeuralNet\Matrixes\MatrixGen.py", line 9, in __mul__
    return tf.matmul(self.matrix, x)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1980, in matmul
    a, b, adj_x=adjoint_a, adj_y=adjoint_b, name=name)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1235, in batch_mat_mul
    "BatchMatMul", x=x, y=y, adj_x=adj_x, adj_y=adj_y, name=name)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
    op_def=op_def)
  File "D:\Anaconda\lib\site-packages\tensorflow\python\framework\ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): In[0] mismatch In[1] shape: 1 vs. 60: [25,1] [60,1] 0 0
	 [[Node: MatMul_6 = BatchMatMul[T=DT_DOUBLE, adj_x=false, adj_y=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_Placeholder_23_0_1, _arg_Placeholder_21_0_0)]]

In [29]:
Z = lagar.calc_psi(train_xy, optimisation_result.x[-1])
#print(Z)
print(Z.shape)
fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(xy[:,0], xy[:,1], Z)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-29-b9976d82fbe3> in <module>()
----> 1 Z = lagar.calc_psi(train_xy, optimisation_result.x[-1])
      2 #print(Z)
      3 print(Z.shape)
      4 fig = plt.figure(figsize=(14,6))
      5 ax = fig.add_subplot(1, 2, 1, projection='3d')

NameError: name 'optimisation_result' is not defined

In [6]:
Z.shape


Out[6]:
(1, 3721)

In [ ]:


In [12]:
%matplotlib notebook

In [7]:
Z2 = d1_osc.wf_nd([0,0], xy)
Z2 = Z2.reshape(1,Z2.size)
math_util.std_err(Z-Z2)


Out[7]:
0.03286335384456467

In [13]:
fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(xy[:,0], xy[:,1], Z-Z2)


Out[13]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x13e6eb8a3c8>

In [9]:
fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(xy[:,0], xy[:,1], Z2)


Out[9]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x13e6e64ada0>

In [10]:
fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.plot_wireframe(xy[:,0], xy[:,1], Z)


Out[10]:
<mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x13e6ea10c18>