hw3/hw3_submission.py
def fit(self, x, y, batch_size=64, iteration=2000, learning_rate=1e-3):
        """
        Train this 2 layered neural network classifier using mini-batch stochastic gradient descent.
        Inputs:
        - X: A numpy array of shape (N, D) containing training data; there are N
          training samples each of dimension D.
        - y: A numpy array of shape (N,) containing training labels; y[i] = c
          means that X[i] has label 0 <= c < C for C classes.
        - learning_rate: (float) learning rate for optimization.
        - iteration: (integer) number of steps to take when optimizing
        - batch_size: (integer) number of training examples to use at each step.
        
        Use the given learning_rate, iteration, or batch_size for this homework problem.

        Returns:
        None
        """
        dim = x.shape[1]
        num_train = x.shape[0]

        # initialize W
        if self.W1 is None:
            self.W1 = 0.001 * np.random.randn(dim, self.hidden_size)
            self.b1 = 0

            self.W2 = 0.001 * np.random.randn(self.hidden_size, self.output_size)
            self.b2 = 0

        for it in range(iteration):
            batch_ind = np.random.choice(num_train, batch_size)

            x_batch = x[batch_ind]
            y_batch = y[batch_ind]

            loss, gradient = self.loss(x_batch, y_batch)

            ############################################################
            ############################################################
            # BEGIN_YOUR_CODE
            # Update parameters with mini-batch stochastic gradient descent method
            self.W1 = self.W1 - learning_rate * gradient["dW1"]
            self.W2 = self.W2 - learning_rate * gradient["dW2"]
            self.b1 = self.b1 - learning_rate * gradient["db1"]
            self.b2 = self.b2 - learning_rate * gradient["db2"]
            pass
            # END_YOUR_CODE
            ############################################################
            ############################################################

            y_pred = self.predict(x_batch)
            acc = np.mean(y_pred == y_batch)

            if it % 50 == 0:
                print('iteration %d / %d: accuracy : %f: loss : %f' % (it, iteration, acc, loss))

        Train this 2 layered neural network classifier using mini-batch stochastic gradient descent.
        Inputs:
        - X: A numpy array of shape (N, D) containing training data; there are N
          training samples each of dimension D.
        - y: A numpy array of shape (N,) containing training labels; y[i] = c
          means that X[i] has label 0 <= c < C for C classes.
        - learning_rate: (float) learning rate for optimization.
        - iteration: (integer) number of steps to take when optimizing
        - batch_size: (integer) number of training examples to use at each step.
        
        Use the given learning_rate, iteration, or batch_size for this homework problem.

        Returns:
        None
        
Train this 2 layered neural network classifier using mini-batch stochastic gradient descent.
Inputs:
X: A numpy array of shape (N, D) containing training data; there are N
training samples each of dimension D.
y: A numpy array of shape (N,) containing training labels; y[i] = c
means that X[i] has label 0 <= c < C for C classes.
learning_rate: (float) learning rate for optimization.
iteration: (integer) number of steps to take when optimizing
batch_size: (integer) number of training examples to use at each step.

Use the given learning_rate, iteration, or batch_size for this homework problem.
Train this 2 layered neural network classifier using mini - batch stochastic gradient descent . Inputs : X : A numpy array of shape ( N D ) containing training data ; there are N training samples each of dimension D . y : A numpy array of shape ( N ) containing training labels ; y [ i ] = c means that X [ i ] has label 0 < = c < C for C classes . learning_rate : ( float ) learning rate for optimization . iteration : ( integer ) number of steps to take when optimizing batch_size : ( integer ) number of training examples to use at each step . Use the given learning_rate iteration or batch_size for this homework problem .
def fit(self, x, y, batch_size=64, iteration=2000, learning_rate=1e-3):
        dim = x.shape[1]
        num_train = x.shape[0]
        if self.W1 is None:
            self.W1 = 0.001 * np.random.randn(dim, self.hidden_size)
            self.b1 = 0
            self.W2 = 0.001 * np.random.randn(self.hidden_size, self.output_size)
            self.b2 = 0
        for it in range(iteration):
            batch_ind = np.random.choice(num_train, batch_size)
            x_batch = x[batch_ind]
            y_batch = y[batch_ind]
            loss, gradient = self.loss(x_batch, y_batch)
            self.W1 = self.W1 - learning_rate * gradient["dW1"]
            self.W2 = self.W2 - learning_rate * gradient["dW2"]
            self.b1 = self.b1 - learning_rate * gradient["db1"]
            self.b2 = self.b2 - learning_rate * gradient["db2"]
            pass
            y_pred = self.predict(x_batch)
            acc = np.mean(y_pred == y_batch)
            if it % 50 == 0:
                print('iteration %d / %d: accuracy : %f: loss : %f' % (it, iteration, acc, loss))
def fit ( self , x , y , batch_size = 64 , iteration = 2000 , learning_rate = 1e-3 ) : dim = x . shape [ 1 ] num_train = x . shape [ 0 ] if self . W1 is None : self . W1 = 0.001 * np . random . randn ( dim , self . hidden_size ) self . b1 = 0 self . W2 = 0.001 * np . random . randn ( self . hidden_size , self . output_size ) self . b2 = 0 for it in range ( iteration ) : batch_ind = np . random . choice ( num_train , batch_size ) x_batch = x [ batch_ind ] y_batch = y [ batch_ind ] loss , gradient = self . loss ( x_batch , y_batch ) self . W1 = self . W1 - learning_rate * gradient [ "dW1" ] self . W2 = self . W2 - learning_rate * gradient [ "dW2" ] self . b1 = self . b1 - learning_rate * gradient [ "db1" ] self . b2 = self . b2 - learning_rate * gradient [ "db2" ] pass y_pred = self . predict ( x_batch ) acc = np . mean ( y_pred == y_batch ) if it % 50 == 0 : print ( 'iteration %d / %d: accuracy : %f: loss : %f' % ( it , iteration , acc , loss ) )
Train this 2 layered neural network classifier using mini-batch stochastic gradient descent.
Train this 2 layered neural network classifier using mini - batch stochastic gradient descent .
"""
        Train this 2 layered neural network classifier using mini-batch stochastic gradient descent.
        Inputs:
        - X: A numpy array of shape (N, D) containing training data; there are N
          training samples each of dimension D.
        - y: A numpy array of shape (N,) containing training labels; y[i] = c
          means that X[i] has label 0 <= c < C for C classes.
        - learning_rate: (float) learning rate for optimization.
        - iteration: (integer) number of steps to take when optimizing
        - batch_size: (integer) number of training examples to use at each step.
        
        Use the given learning_rate, iteration, or batch_size for this homework problem.

        Returns:
        None
        """ # initialize W ############################################################ ############################################################ # BEGIN_YOUR_CODE # Update parameters with mini-batch stochastic gradient descent method # END_YOUR_CODE ############################################################ ############################################################
None
self
x
y
batch_size
iteration
learning_rate