首页 > > 详细

Assignment 4 - Convolutional Neural Network

 Assignment 4 - Convolutional Neural Network

In this assignment we will develop a neural network with fully-connected layers to perform classification
 
    #############################################################################
    #                              END OF YOUR CODE                             #
    #############################################################################
    # If the targets are not given then jump out, we're done
    if y is None:
      return scores
    # Compute the loss
    loss = None
    
    scores -= np.max(scores, axis=1, keepdims=True) # avoid numeric instability
    #############################################################################
    # Full Mark: 2                                                              #
    # TODO: Finish the forward pass, and compute the loss. This should include  #
    # both the data loss and L2 regularization for W1 and W2. Store the result  #
    # in the variable loss, which should be a scalar. Use the Softmax           #
    # classifier loss.                                                          #
    #############################################################################
    loss =
    #############################################################################
    #                              END OF YOUR CODE                             #
    #############################################################################
    # Backward pass: compute gradients
    grads = {}
    #############################################################################
    # Full Mark: 2                                                              #
    # TODO: Compute the backward pass, computing the derivatives of the weights #
    # and biases. Store the results in the grads dictionary. For example,       #
    # grads['W1'] should store the gradient on W1, and be a matrix of same size #
    #############################################################################
    # W2 gradient
    dW2 = 
    # b2 gradient
    db2 = 
    # W1 gradient
    dW1 = 
    # b1 gradient
    db1 = 
    # regularization gradient
    # store the results in the grads dictionary
    grads = {'W1':dW1, 'b1':db1, 'W2':dW2, 'b2':db2}
    #############################################################################
    #                              END OF YOUR CODE                             #
    #############################################################################
    return loss, grads
  def train(self, X, y, X_val, y_val,
            learning_rate=1e-3, learning_rate_decay=0.95,
            reg=5e-6, num_iters=100,
            batch_size=200, verbose=False):
    """
    Train this neural network using stochastic gradient descent.
    Inputs:
    - X: A numpy array of shape (N, D) giving training data.
    - y: A numpy array f shape (N,) giving training labels; y[i] = c means that
      X[i] has label c, where 0 <= c < C.
    - X_val: A numpy array of shape (N_val, D) giving validation data.
    - y_val: A numpy array of shape (N_val,) giving validation labels.
    - learning_rate: Scalar giving learning rate for optimization.
    - learning_rate_decay: Scalar giving factor used to decay the learning rate
      after each epoch.
    - reg: Scalar giving regularization strength.
    - num_iters: Number of steps to take when optimizing.
    - batch_size: Number of training examples to use per step.
    - verbose: boolean; if true print progress during optimization.
    """
    num_train = X.shape[0]
    iterations_per_epoch = max(num_train / batch_size, 1)
    # Use SGD to optimize the parameters in self.model
    loss_history = []
    train_acc_history = []
    val_acc_history = []
    for it in xrange(num_iters):
      #########################################################################
      # Full Mark: 0.5                                                        #
      # TODO: Create a random minibatch of training data and labels using     #
      # given num_train and batch_size, storing them in X_batch and y_batch   #
      # respectively.                                                         #
      #########################################################################
      X_batch = 
      y_batch = 
      #########################################################################
      #                             END OF YOUR CODE                          #
      #########################################################################
      # Compute loss and gradients using the current minibatch
      loss, grads = self.loss(X_batch, y=y_batch, reg=reg)
      loss_history.append(loss)
      #########################################################################
      # Full Mark: 0.5                                                        #
      # TODO: Use the gradients in the grads dictionary to update the         #
      # parameters of the network (stored in the dictionary self.params)      #
      # using stochastic gradient descent. You'll need to use the gradients   #
      # stored in the grads dictionary defined above.                         #
      #########################################################################
      self.params['W1'] =
      self.params['W2'] =
      self.params['b1'] =
      self.params['b2'] =
      #########################################################################
      #                             END OF YOUR CODE                          #
      #########################################################################
      if verbose and it % 10 == 0:
        print('iteration %d / %d: loss %f' % (it, num_iters, loss))
      # Every epoch, check train and val accuracy and decay learning rate.
      if it % iterations_per_epoch == 0:
        # Check accuracy
        train_acc = (self.predict(X_batch) == y_batch).mean()
        val_acc = (self.predict(X_val) == y_val).mean()
        train_acc_history.append(train_acc)
        val_acc_history.append(val_acc)
        # Decay learning rate
        learning_rate *= learning_rate_decay
    return {
      'loss_history': loss_history,
      'train_acc_history': train_acc_history,
      'val_acc_history': val_acc_history,
    }
  def predict(self, X):
    """
    Use the trained weights of this two-layer network to predict labels for
    data points. For each data point we predict scores for each of the C
    classes, and assign each data point to the class with the highest score.
    Inputs:
    - X: A numpy array of shape (N, D) giving N D-dimensional data points to
      classify.
    Returns:
    - y_pred: A numpy array of shape (N,) giving predicted labels for each of
      the elements of X. For all i, y_pred[i] = c means that X[i] is predicted
      to have class c, where 0 <= c < C.
    """
    ###########################################################################
    # Full Mark: 1                                                            #
    # TODO: Implement this function                                           #
    ###########################################################################
    y_pred = 
    ###########################################################################
    #                              END OF YOUR CODE                           #
    ###########################################################################
    return y_pred
# To check your implementations.
X,y =load_dataset()
X_train, y_train, X_val, y_val, X_test, y_test=train_test_split(X, y)
###########################################################################
# Full Mark: 1                                                            #
# TODO: 1. Using TwoLayerCNN to train on given datasets                   #
#       2. Print out the final loss                                       #
#       3. Print out the test accuracy                                    #
###########################################################################
input_size = 
hidden_size = 
num_classes = 
net = TwoLayerCNN(input_size, hidden_size, num_classes)
# TODO
###########################################################################
#                              END OF YOUR CODE                           #
###########################################################################
The loss function and the accuracies on the training and validation sets would give more insight views.
 
###########################################################################
# Full Mark: 0.5                                                          #
# TODO: Plot training loss history                                        # 
###########################################################################
###########################################################################
#                              END OF YOUR CODE                           #
###########################################################################   
    
plt.xlabel('iteration')
plt.ylabel('training loss')
plt.title('Training Loss history')
plt.show()
###########################################################################
# Full Mark: 0.5                                                          #
# TODO: Plot Classification accuracy history, compare train/val accuracy  #
###########################################################################
###########################################################################
#                              END OF YOUR CODE                           #
###########################################################################  
plt.xlabel('Epoch')
plt.ylabel('Classification accuracy')
plt.title('Classification accuracy history')
plt.legend()
plt.show()
###############################################################################
# Full Mark: 1                                                                #
# TODO: Describe or using codes to show how you tune your hyperparameters     #
# (hidden layer size, learning rate, numer of training epochs, regularization #
# strength and so on). Is your result good? Does it look underfiting?         #
# Overfiting?                                                                 #
###############################################################################
###############################################################################
#                              END OF YOUR CODE                               #
###############################################################################  
Explain your hyperparameter tuning process below.
 
Y𝑜𝑢𝑟𝐴𝑛𝑠𝑤𝑒𝑟:
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!