# Path: hamaa/utils/np_utils.py
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
#     it = np.nditer(x, flags=['multi_index'])
#     grad = np.zeros_like(x)
#     while not it.finished:
#         xi = it.multi_index
#         old_val = x[xi]
# 
#         x[xi] = old_val + h
#         y_top = f(x)
# 
#         x[xi] = old_val - h
#         y_bottom = f(x)
# 
#         if df is not None:
#             grad[xi] = np.sum((y_top - y_bottom) * df) / (2 * h)
#         else:
#             grad[xi] = np.sum(y_top - y_bottom) / (2 * h)
# 
#         if verbose:
#             print it.multi_index, grad[xi]
#         x[xi] = old_val
#         it.iternext()
# 
#     return grad
# 
# def sum_abs_err(u, v):
#     return np.sum(np.abs(u - v))


from hamaa.gates import *
from hamaa.layers import *
from hamaa.utils.np_utils import eval_numerical_gradient_array, sum_abs_err
# encoding: utf-8
"""
@author: monitor1379
@contact: yy4f5da2@hotmail.com
@site: www.monitor1379.com

@version: 1.0
@license: GNU General Public License(Version 3)
@file: check_gradient.py
@time: 2016/10/4 9:59


"""



def check_LinearGate():
    x = np.random.randn(10, 4).astype(np.double)
    dy = np.random.randn(10, 4).astype(np.double)

# Path: hamaa/utils/np_utils.py
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
#     it = np.nditer(x, flags=['multi_index'])
#     grad = np.zeros_like(x)
#     while not it.finished:
#         xi = it.multi_index
#         old_val = x[xi]
# 
#         x[xi] = old_val + h
#         y_top = f(x)
# 
#         x[xi] = old_val - h
#         y_bottom = f(x)
# 
#         if df is not None:
#             grad[xi] = np.sum((y_top - y_bottom) * df) / (2 * h)
#         else:
#             grad[xi] = np.sum(y_top - y_bottom) / (2 * h)
# 
#         if verbose:
#             print it.multi_index, grad[xi]
#         x[xi] = old_val
#         it.iternext()
# 
#     return grad
# 
# def sum_abs_err(u, v):
#     return np.sum(np.abs(u - v))


# Path: hamaa/tests/check_gradient.py
from hamaa.gates import *
from hamaa.layers import *
from hamaa.utils.np_utils import eval_numerical_gradient_array, sum_abs_err

# encoding: utf-8
"""
@author: monitor1379
@contact: yy4f5da2@hotmail.com
@site: www.monitor1379.com

@version: 1.0
@license: GNU General Public License(Version 3)
@file: check_gradient.py
@time: 2016/10/4 9:59


"""



def check_LinearGate():
    x = np.random.randn(10, 4).astype(np.double)
    dy = np.random.randn(10, 4).astype(np.double)

    grad_x = eval_numerical_gradient_array(LinearGate.forward, x, dy)