# Path: hamaa/gates.py
# class SoftmaxGate:
#     """softmax单元"""
# 
#     @staticmethod
#     def forward(x):
#         ex = np.exp(x - np.max(x, axis=1, keepdims=True))
#         return ex / np.sum(ex, axis=1, keepdims=True)
# 
#     @staticmethod
#     def backward(x, z, d_z):
#         # Because softmax is usually used together with crossentropy cost function,
#         # so the backpropagation part of softmax is calculated in the BP of crossentropy.
#         # Here just pass through the derivatives.
#         return np.array(d_z)
# 
#     @staticmethod
#     def naive_backward(x, z, d_z):
#         import warnings
#         warnings.warn('Do not use SoftmaxGate.backward() when backpropagation!')
#         n, m = x.shape
#         d_x = np.empty_like(x)
#         eyemat = np.eye(m)
#         for i in xrange(n):
#             z_i = z[i].reshape(1, m)
#             d_x[i] = np.dot(d_z[i], (eyemat - z_i) * z_i.T)
#         return d_x
# 
# Path: hamaa/utils/np_utils.py
# def to_one_hot(y, nb_class):
# def to_categorical(y):
# def split_training_data(data, nb_validation=None, split_ratio=None):
# def padding(images, pad_size):
# def rot180(images):
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
# def sum_abs_err(u, v):


import numpy as np
from .gates import SoftmaxGate
from .utils import np_utils
"""




class MeanSquareError:

    @staticmethod
    def loss(y_real, y_pred):
        return np.mean(np.square(y_real - y_pred))

    @staticmethod
    def diff(y_real, y_pred):
        return - (y_real - y_pred) / y_real.shape[0]



class CategoricalCrossEntropy:

    @staticmethod
    def loss(y_real, y_pred):
        probs = y_pred
        categorical_y_real = np_utils.to_categorical(y_real)
        n = categorical_y_real.shape[0]
        cost = np.sum(-np.log(probs[range(n), categorical_y_real])) / n
        return cost

    @staticmethod
    def diff(y_real, y_pred):
        probs = y_pred

# Path: hamaa/gates.py
# class SoftmaxGate:
#     """softmax单元"""
# 
#     @staticmethod
#     def forward(x):
#         ex = np.exp(x - np.max(x, axis=1, keepdims=True))
#         return ex / np.sum(ex, axis=1, keepdims=True)
# 
#     @staticmethod
#     def backward(x, z, d_z):
#         # Because softmax is usually used together with crossentropy cost function,
#         # so the backpropagation part of softmax is calculated in the BP of crossentropy.
#         # Here just pass through the derivatives.
#         return np.array(d_z)
# 
#     @staticmethod
#     def naive_backward(x, z, d_z):
#         import warnings
#         warnings.warn('Do not use SoftmaxGate.backward() when backpropagation!')
#         n, m = x.shape
#         d_x = np.empty_like(x)
#         eyemat = np.eye(m)
#         for i in xrange(n):
#             z_i = z[i].reshape(1, m)
#             d_x[i] = np.dot(d_z[i], (eyemat - z_i) * z_i.T)
#         return d_x
# 
# Path: hamaa/utils/np_utils.py
# def to_one_hot(y, nb_class):
# def to_categorical(y):
# def split_training_data(data, nb_validation=None, split_ratio=None):
# def padding(images, pad_size):
# def rot180(images):
# def eval_numerical_gradient_array(f, x, df=None, verbose=False, h=1e-4):
# def sum_abs_err(u, v):


# Path: hamaa/objectives.py
import numpy as np
from .gates import SoftmaxGate
from .utils import np_utils

"""




class MeanSquareError:

    @staticmethod
    def loss(y_real, y_pred):
        return np.mean(np.square(y_real - y_pred))

    @staticmethod
    def diff(y_real, y_pred):
        return - (y_real - y_pred) / y_real.shape[0]



class CategoricalCrossEntropy:

    @staticmethod
    def loss(y_real, y_pred):
        probs = y_pred
        categorical_y_real = np_utils.to_categorical(y_real)
        n = categorical_y_real.shape[0]
        cost = np.sum(-np.log(probs[range(n), categorical_y_real])) / n
        return cost

    @staticmethod
    def diff(y_real, y_pred):
        probs = y_pred

        categorical_y_real = np_utils.to_categorical(y_real)