hw3/hw3_submission.py
def loss(self, x_batch, y_batch, reg=1e-3):
        """
            Implement feed-forward computation to calculate the loss function.
            And then compute corresponding back-propagation to get the derivatives. 

            Inputs:
            - X_batch: A numpy array of shape (N, D) containing a minibatch of N
              data points; each point has dimension D.
            - y_batch: A numpy array of shape (N,) containing labels for the minibatch.
            - reg: hyperparameter which is weight of the regularizer.

            Returns: A tuple containing:
            - loss as a single float
            - gradient dictionary with four keys : 'dW1', 'db1', 'dW2', and 'db2'
            """
        gradient = {'dW1': None, 'db1': None, 'dW2': None, 'db2': None}

        ############################################################
        ############################################################
        # BEGIN_YOUR_CODE
        # Calculate y_hat which is probability of the instance is y = 0.
        g1 = x_batch.dot(self.W1) + self.b1
        h1 = self.activation(g1)
        g2 = h1.dot(self.W2) + self.b2
        y_hat = self.sigmoid(g2)
        pass
        # END_YOUR_CODE
        ############################################################
        ############################################################

        ############################################################
        ############################################################
        # BEGIN_YOUR_CODE
        # Calculate loss and gradient
        n = y_hat.shape[0]
        loss = -np.sum(y_batch * np.log(y_hat + 1e-5)) / n

        gradient['dW1'] = x_batch.transpose().dot(y_hat-y_batch)*self.W2.transpose() / (2*n)
        gradient['db1'] = np.mean((y_hat-y_batch)*self.W2) / n
        gradient['dW2'] = h1.transpose().dot(y_hat-y_batch) / (2*n)
        gradient['db2'] = np.mean(y_hat-y_batch) / n
        pass
        # END_YOUR_CODE
        ############################################################
        ############################################################
        return loss, gradient

            Implement feed-forward computation to calculate the loss function.
            And then compute corresponding back-propagation to get the derivatives. 

            Inputs:
            - X_batch: A numpy array of shape (N, D) containing a minibatch of N
              data points; each point has dimension D.
            - y_batch: A numpy array of shape (N,) containing labels for the minibatch.
            - reg: hyperparameter which is weight of the regularizer.

            Returns: A tuple containing:
            - loss as a single float
            - gradient dictionary with four keys : 'dW1', 'db1', 'dW2', and 'db2'
            
Implement feed-forward computation to calculate the loss function.
And then compute corresponding back-propagation to get the derivatives.

A numpy array of shape (N, D) containing a minibatch of N
data points; each point has dimension D.
y_batch: A numpy array of shape (N,) containing labels for the minibatch.
reg: hyperparameter which is weight of the regularizer.


Implement feed - forward computation to calculate the loss function . And then compute corresponding back - propagation to get the derivatives . A numpy array of shape ( N D ) containing a minibatch of N data points ; each point has dimension D . y_batch : A numpy array of shape ( N ) containing labels for the minibatch . reg : hyperparameter which is weight of the regularizer .
def loss(self, x_batch, y_batch, reg=1e-3):
        gradient = {'dW1': None, 'db1': None, 'dW2': None, 'db2': None}
        g1 = x_batch.dot(self.W1) + self.b1
        h1 = self.activation(g1)
        g2 = h1.dot(self.W2) + self.b2
        y_hat = self.sigmoid(g2)
        pass
        n = y_hat.shape[0]
        loss = -np.sum(y_batch * np.log(y_hat + 1e-5)) / n
        gradient['dW1'] = x_batch.transpose().dot(y_hat-y_batch)*self.W2.transpose() / (2*n)
        gradient['db1'] = np.mean((y_hat-y_batch)*self.W2) / n
        gradient['dW2'] = h1.transpose().dot(y_hat-y_batch) / (2*n)
        gradient['db2'] = np.mean(y_hat-y_batch) / n
        pass
        return loss, gradient
def loss ( self , x_batch , y_batch , reg = 1e-3 ) : gradient = { 'dW1' : None , 'db1' : None , 'dW2' : None , 'db2' : None } g1 = x_batch . dot ( self . W1 ) + self . b1 h1 = self . activation ( g1 ) g2 = h1 . dot ( self . W2 ) + self . b2 y_hat = self . sigmoid ( g2 ) pass n = y_hat . shape [ 0 ] loss = - np . sum ( y_batch * np . log ( y_hat + 1e-5 ) ) / n gradient [ 'dW1' ] = x_batch . transpose ( ) . dot ( y_hat - y_batch ) * self . W2 . transpose ( ) / ( 2 * n ) gradient [ 'db1' ] = np . mean ( ( y_hat - y_batch ) * self . W2 ) / n gradient [ 'dW2' ] = h1 . transpose ( ) . dot ( y_hat - y_batch ) / ( 2 * n ) gradient [ 'db2' ] = np . mean ( y_hat - y_batch ) / n pass return loss , gradient
Implement feed-forward computation to calculate the loss function.
Implement feed - forward computation to calculate the loss function .
"""
            Implement feed-forward computation to calculate the loss function.
            And then compute corresponding back-propagation to get the derivatives. 

            Inputs:
            - X_batch: A numpy array of shape (N, D) containing a minibatch of N
              data points; each point has dimension D.
            - y_batch: A numpy array of shape (N,) containing labels for the minibatch.
            - reg: hyperparameter which is weight of the regularizer.

            Returns: A tuple containing:
            - loss as a single float
            - gradient dictionary with four keys : 'dW1', 'db1', 'dW2', and 'db2'
            """ ############################################################ ############################################################ # BEGIN_YOUR_CODE # Calculate y_hat which is probability of the instance is y = 0. # END_YOUR_CODE ############################################################ ############################################################ ############################################################ ############################################################ # BEGIN_YOUR_CODE # Calculate loss and gradient # END_YOUR_CODE ############################################################ ############################################################
self
x_batch
y_batch
reg