thesonpb

machinelearning

May 2nd, 2021
690
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import nn
  2.  
  3. class PerceptronModel(object):
  4.     def __init__(self, dimensions):
  5.         """
  6.        Initialize a new Perceptron instance.
  7.  
  8.        A perceptron classifies data points as either belonging to a particular
  9.        class (+1) or not (-1). `dimensions` is the dimensionality of the data.
  10.        For example, dimensions=2 would mean that the perceptron must classify
  11.        2D points.
  12.        """
  13.         self.w = nn.Parameter(1, dimensions)
  14.  
  15.     def get_weights(self):
  16.         """
  17.        Return a Parameter instance with the current weights of the perceptron.
  18.        """
  19.         return self.w
  20.  
  21.     def run(self, x):
  22.         """
  23.        Calculates the score assigned by the perceptron to a data point x.
  24.  
  25.        Inputs:
  26.            x: a node with shape (1 x dimensions)
  27.        Returns: a node containing a single number (the score)
  28.        """
  29.         "*** YOUR CODE HERE ***"
  30.         node = nn.DotProduct(x, self.w)
  31.         return node
  32.  
  33.     def get_prediction(self, x):
  34.         """
  35.        Calculates the predicted class for a single data point `x`.
  36.  
  37.        Returns: 1 or -1
  38.        """
  39.         "*** YOUR CODE HERE ***"
  40.         if nn.as_scalar(nn.DotProduct(x, self.w)) >= 0.0:
  41.             return 1
  42.         else:
  43.             return -1
  44.  
  45.     def train(self, dataset):
  46.         """
  47.        Train the perceptron until convergence.
  48.        """
  49.         "*** YOUR CODE HERE ***"
  50.         while True:
  51.             success = True
  52.             for x, y in dataset.iterate_once(1):
  53.                 if nn.as_scalar(y) != self.get_prediction(x): #nếu output thực tế khác với output dự đoán thì false và update lại weight
  54.                     success = False
  55.                     nn.Parameter.update(self.w, x, nn.as_scalar(y))
  56.             if success:
  57.                 break
  58.  
  59.  
  60. class RegressionModel(object):
  61.     """
  62.    A neural network model for approximating a function that maps from real
  63.    numbers to real numbers. The network should be sufficiently large to be able
  64.    to approximate sin(x) on the interval [-2pi, 2pi] to reasonable precision.
  65.    """
  66.     def __init__(self):
  67.         # Initialize your model parameters here
  68.         "*** YOUR CODE HERE ***"
  69.  
  70.         self.batch_size = 1
  71.         self.w0 = nn.Parameter(1, 50)
  72.         self.b0 = nn.Parameter(1, 50)
  73.         self.w1 = nn.Parameter(50, 1)
  74.         self.b1 = nn.Parameter(1, 1)
  75.  
  76.     def run(self, x):
  77.         """
  78.        Runs the model for a batch of examples.
  79.  
  80.        Inputs:
  81.            x: a node with shape (batch_size x 1)
  82.        Returns:
  83.            A node with shape (batch_size x 1) containing predicted y-values
  84.        """
  85.         "*** YOUR CODE HERE ***"
  86.  
  87.         fx1 = nn.AddBias(nn.Linear(x, self.w0), self.b0)
  88.         relu1 = nn.ReLU(fx1)
  89.         fx2 = nn.AddBias(nn.Linear(relu1, self.w1), self.b1)
  90.         return fx2
  91.  
  92.     def get_loss(self, x, y):
  93.         """
  94.        Computes the loss for a batch of examples.
  95.  
  96.        Inputs:
  97.            x: a node with shape (batch_size x 1)
  98.            y: a node with shape (batch_size x 1), containing the true y-values
  99.                to be used for training
  100.        Returns: a loss node
  101.        """
  102.         "*** YOUR CODE HERE ***"
  103.  
  104.         return nn.SquareLoss(self.run(x), y)
  105.  
  106.     def train(self, dataset):
  107.         """
  108.        Trains the model.
  109.        """
  110.         "*** YOUR CODE HERE ***"
  111.         while True:
  112.  
  113.  
  114.  
  115.             for x, y in dataset.iterate_once(self.batch_size):
  116.                 loss = self.get_loss(x, y)
  117.                 grad = nn.gradients(loss, [self.w0, self.w1, self.b0, self.b1])
  118.  
  119.  
  120.                 self.w0.update(grad[0], -0.005)
  121.                 self.w1.update(grad[1], -0.005)
  122.                 self.b0.update(grad[2], -0.005)
  123.                 self.b1.update(grad[3], -0.005)
  124.  
  125.  
  126.             if nn.as_scalar(self.get_loss(nn.Constant(dataset.x), nn.Constant(dataset.y))) < 0.02:
  127.                 return
  128.  
  129.  
  130. class DigitClassificationModel(object):
  131.     """
  132.    A model for handwritten digit classification using the MNIST dataset.
  133.  
  134.    Each handwritten digit is a 28x28 pixel grayscale image, which is flattened
  135.    into a 784-dimensional vector for the purposes of this model. Each entry in
  136.    the vector is a floating point number between 0 and 1.
  137.  
  138.    The goal is to sort each digit into one of 10 classes (number 0 through 9).
  139.  
  140.    (See RegressionModel for more information about the APIs of different
  141.    methods here. We recommend that you implement the RegressionModel before
  142.    working on this part of the project.)
  143.    """
  144.     def __init__(self):
  145.         # Initialize your model parameters here
  146.         "*** YOUR CODE HERE ***"
  147.         self.batch_size = 1
  148.         self.w0 = nn.Parameter(784, 100)
  149.         self.b0 = nn.Parameter(1, 100)
  150.         self.w1 = nn.Parameter(100, 10)
  151.         self.b1 = nn.Parameter(1, 10)
  152.  
  153.     def run(self, x):
  154.         """
  155.        Runs the model for a batch of examples.
  156.  
  157.        Your model should predict a node with shape (batch_size x 10),
  158.        containing scores. Higher scores correspond to greater probability of
  159.        the image belonging to a particular class.
  160.  
  161.        Inputs:
  162.            x: a node with shape (batch_size x 784)
  163.        Output:
  164.            A node with shape (batch_size x 10) containing predicted scores
  165.                (also called logits)
  166.        """
  167.         "*** YOUR CODE HERE ***"
  168.         fx1 = nn.AddBias(nn.Linear(x, self.w0), self.b0)
  169.         relu1 = nn.ReLU(fx1)
  170.         fx2 = nn.AddBias(nn.Linear(relu1, self.w1), self.b1)
  171.         return fx2
  172.  
  173.     def get_loss(self, x, y):
  174.         """
  175.        Computes the loss for a batch of examples.
  176.  
  177.        The correct labels `y` are represented as a node with shape
  178.        (batch_size x 10). Each row is a one-hot vector encoding the correct
  179.        digit class (0-9).
  180.  
  181.        Inputs:
  182.            x: a node with shape (batch_size x 784)
  183.            y: a node with shape (batch_size x 10)
  184.        Returns: a loss node
  185.        """
  186.         "*** YOUR CODE HERE ***"
  187.  
  188.         return nn.SoftmaxLoss(self.run(x), y)
  189.  
  190.  
  191.     def train(self, dataset):
  192.         """
  193.        Trains the model.
  194.        """
  195.         "*** YOUR CODE HERE ***"
  196.         while True:
  197.  
  198.  
  199.             for x, y in dataset.iterate_once(self.batch_size):
  200.                 loss = self.get_loss(x, y)
  201.                 grad = nn.gradients(loss, [self.w0, self.w1, self.b0, self.b1])
  202.  
  203.                 self.w0.update(grad[0], -0.005)
  204.                 self.w1.update(grad[1], -0.005)
  205.                 self.b0.update(grad[2], -0.005)
  206.                 self.b1.update(grad[3], -0.005)
  207.  
  208.             print(dataset.get_validation_accuracy())
  209.             if dataset.get_validation_accuracy() >= 0.97:
  210.                 return
  211.  
  212.  
  213. class LanguageIDModel(object):
  214.     """
  215.    A model for language identification at a single-word granularity.
  216.  
  217.    (See RegressionModel for more information about the APIs of different
  218.    methods here. We recommend that you implement the RegressionModel before
  219.    working on this part of the project.)
  220.    """
  221.     def __init__(self):
  222.         # Our dataset contains words from five different languages, and the
  223.         # combined alphabets of the five languages contain a total of 47 unique
  224.         # characters.
  225.         # You can refer to self.num_chars or len(self.languages) in your code
  226.         self.num_chars = 47
  227.         self.languages = ["English", "Spanish", "Finnish", "Dutch", "Polish"]
  228.  
  229.         # Initialize your model parameters here
  230.         "*** YOUR CODE HERE ***"
  231.  
  232.     def run(self, xs):
  233.         """
  234.        Runs the model for a batch of examples.
  235.  
  236.        Although words have different lengths, our data processing guarantees
  237.        that within a single batch, all words will be of the same length (L).
  238.  
  239.        Here `xs` will be a list of length L. Each element of `xs` will be a
  240.        node with shape (batch_size x self.num_chars), where every row in the
  241.        array is a one-hot vector encoding of a character. For example, if we
  242.        have a batch of 8 three-letter words where the last word is "cat", then
  243.        xs[1] will be a node that contains a 1 at position (7, 0). Here the
  244.        index 7 reflects the fact that "cat" is the last word in the batch, and
  245.        the index 0 reflects the fact that the letter "a" is the inital (0th)
  246.        letter of our combined alphabet for this task.
  247.  
  248.        Your model should use a Recurrent Neural Network to summarize the list
  249.        `xs` into a single node of shape (batch_size x hidden_size), for your
  250.        choice of hidden_size. It should then calculate a node of shape
  251.        (batch_size x 5) containing scores, where higher scores correspond to
  252.        greater probability of the word originating from a particular language.
  253.  
  254.        Inputs:
  255.            xs: a list with L elements (one per character), where each element
  256.                is a node with shape (batch_size x self.num_chars)
  257.        Returns:
  258.            A node with shape (batch_size x 5) containing predicted scores
  259.                (also called logits)
  260.        """
  261.         "*** YOUR CODE HERE ***"
  262.  
  263.     def get_loss(self, xs, y):
  264.         """
  265.        Computes the loss for a batch of examples.
  266.  
  267.        The correct labels `y` are represented as a node with shape
  268.        (batch_size x 5). Each row is a one-hot vector encoding the correct
  269.        language.
  270.  
  271.        Inputs:
  272.            xs: a list with L elements (one per character), where each element
  273.                is a node with shape (batch_size x self.num_chars)
  274.            y: a node with shape (batch_size x 5)
  275.        Returns: a loss node
  276.        """
  277.         "*** YOUR CODE HERE ***"
  278.  
  279.     def train(self, dataset):
  280.         """
  281.        Trains the model.
  282.        """
  283.         "*** YOUR CODE HERE ***"
  284.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×