support/models.py
def g_imp(self, w, X, C, lam=0):
        '''
        Implementation of the gradient of the loss function used in
        multi-class logistic regression.
        '''
        
        # Input:
        # w is a (d_para x 1) matrix of weights.
        # X is a (k x d_feat) matrix of k observations.
        # C is a (k x nc) matrix giving a binarized encoding of the
        # class labels for each observation; each row a one-hot vector.
        # lam is a non-negative regularization parameter.
        # NOTE: k can be anything, the training/test sample size.

        # Output:
        # A (k x d_para) matrix of gradients eval'd at k points.

        # Initialize and populate the activations.
        k = X.shape[0]
        
        A = np.zeros(k*self.nc).reshape( (self.nc,k) )
        A[:-1,:] = np.dot(w.reshape((self.nc-1,self.d_feat)), # reshape w.
                          np.transpose(X)) # leave last row as zeros.

        # Compute the conditional label probabilities.
        P = np.exp(A) / np.sum(np.exp(A), axis=0) # (nc x k)
        
        # Initialize a large matrix (k x d_para) to house per-point grads.
        G = np.arange(k*self.d_para).reshape( (k,self.d_para) )

        for i in range(k):
            # A very tall vector (i.e., just one "axis").
            G[i,:] = np.kron(a=(P[:-1,i]-C[i,:-1]), b=X[i,:])
            # NOTE: carefully removing the last elements.

        return G

        Implementation of the gradient of the loss function used in
        multi-class logistic regression.
        
Implementation of the gradient of the loss function used in
multi-class logistic regression.
Implementation of the gradient of the loss function used in multi - class logistic regression .
def g_imp(self, w, X, C, lam=0):
        k = X.shape[0]
        A = np.zeros(k*self.nc).reshape( (self.nc,k) )
        A[:-1,:] = np.dot(w.reshape((self.nc-1,self.d_feat)), 
                          np.transpose(X)) 
        P = np.exp(A) / np.sum(np.exp(A), axis=0) 
        G = np.arange(k*self.d_para).reshape( (k,self.d_para) )
        for i in range(k):
            G[i,:] = np.kron(a=(P[:-1,i]-C[i,:-1]), b=X[i,:])
        return G
def g_imp ( self , w , X , C , lam = 0 ) : k = X . shape [ 0 ] A = np . zeros ( k * self . nc ) . reshape ( ( self . nc , k ) ) A [ : - 1 , : ] = np . dot ( w . reshape ( ( self . nc - 1 , self . d_feat ) ) , np . transpose ( X ) ) P = np . exp ( A ) / np . sum ( np . exp ( A ) , axis = 0 ) G = np . arange ( k * self . d_para ) . reshape ( ( k , self . d_para ) ) for i in range ( k ) : G [ i , : ] = np . kron ( a = ( P [ : - 1 , i ] - C [ i , : - 1 ] ) , b = X [ i , : ] ) return G
Implementation of the gradient of the loss function used in
multi-class logistic regression.
Implementation of the gradient of the loss function used in multi - class logistic regression .
'''
        Implementation of the gradient of the loss function used in
        multi-class logistic regression.
        ''' # Input: # w is a (d_para x 1) matrix of weights. # X is a (k x d_feat) matrix of k observations. # C is a (k x nc) matrix giving a binarized encoding of the # class labels for each observation; each row a one-hot vector. # lam is a non-negative regularization parameter. # NOTE: k can be anything, the training/test sample size. # Output: # A (k x d_para) matrix of gradients eval'd at k points. # Initialize and populate the activations. # reshape w. # leave last row as zeros. # Compute the conditional label probabilities. # (nc x k) # Initialize a large matrix (k x d_para) to house per-point grads. # A very tall vector (i.e., just one "axis"). # NOTE: carefully removing the last elements.
self
w
X
C
lam