Попытка векторизовать мою функцию обратного распространения softmax - PullRequest
0 голосов
/ 27 февраля 2020

Я написал функцию обратного распространения softmax (softmax_backward_propagate) в python, которая работает так, как я намереваюсь, но мне сказали, что я смогу реализовать ее без oop. Я пытался понять это, но я застрял. Может ли кто-нибудь объяснить лучший способ выполнения sh этой же функции без l oop?

http://saitcelebi.com/tut/output/part2.html <- Хорошее объяснение функции softmax и ее производной </p>

Спасибо за ваше время.

def softmax_backward_propagate(dJ_dyhat, cache):

""" Compute the backward propagation through the softmax activations.
        i.e. compute dJ_dz
    Inputs:
        dJ_dyhat: Upstream gradients on the outputs the softmax activations.
                  Shape (n_h, m) where n_h is number of units in the linear 
                  portion of the layer, and m is number of samples
        cache: cached values saved during forward propagate

    Returns:
        dJ_dz: Gradients on the inputs to this layer. Shape (n_h, m)
    """
    for i in range(cache.shape[1]):
        y = np.copy(cache[:,i]).reshape(-1,1)
        matrix = np.matmul(y, np.ones(y.shape).T) * (np.identity(y.size) - np.matmul(np.ones((y.shape)), y.T))
        if i==0:
            m2 = np.matmul(matrix,dJ_dyhat[:,i])
        else:
            m2 = np.column_stack((m2,np.matmul(matrix,dJ_dyhat[:,i])))
    dJ_dz = m2.reshape(cache.shape)
    return dJ_dz

def softmax_forward_propagate(z):

    """ Compute the forward propagation through the softmax activations. 
        i.e. application of softmax on z

    Inputs:
        z: Inputs to this layer (i.e. outputs of the previous linear 
           layer). shape (n_h, m) where n_h is number of units in the 
           previous linear layer, and m is number of samples.

    Returns:
        yhat: softmax applied to z. Shape (n_h, m) 
        cache: Objects to be sent to backpropagation for this layer.
    """
    cache = None


    ex = np.exp(np.copy(z))
    exsum = np.sum(np.copy(ex),0)
    yhat = np.copy(ex)/np.copy(exsum)
    cache = yhat


    return yhat, cache
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...