<filename>EP4/ep4.py
"""
  AO PREENCHER ESSE CABECALHO COM O MEU NOME E O MEU NUMERO USP,
  DECLARO QUE SOU A UNICA PESSOA AUTORA E RESPONSAVEL POR ESSE PROGRAMA.
  TODAS AS PARTES ORIGINAIS DESSE EXERCICIO PROGRAMA (EP) FORAM
  DESENVOLVIDAS E IMPLEMENTADAS POR MIM SEGUINDO AS INSTRUCOES
  DESSE EP E, PORTANTO, NAO CONSTITUEM ATO DE DESONESTIDADE ACADEMICA,
  FALTA DE ETICA OU PLAGIO.
  DECLARO TAMBEM QUE SOU A PESSOA RESPONSAVEL POR TODAS AS COPIAS
  DESSE PROGRAMA E QUE NAO DISTRIBUI OU FACILITEI A
  SUA DISTRIBUICAO. ESTOU CIENTE QUE OS CASOS DE PLAGIO E
  DESONESTIDADE ACADEMICA SERAO TRATADOS SEGUNDO OS CRITERIOS
  DIVULGADOS NA PAGINA DA DISCIPLINA.
  ENTENDO QUE EPS SEM ASSINATURA NAO SERAO CORRIGIDOS E,
  AINDA ASSIM, PODERAO SER PUNIDOS POR DESONESTIDADE ACADEMICA.

  Nome : <NAME>
  NUSP : 10295429

  Referencias: Com excecao das rotinas fornecidas no enunciado
  e em sala de aula, caso voce tenha utilizado alguma referencia,
  liste-as abaixo para que o seu programa nao seja considerado
  plagio ou irregular.

  Exemplo:
  - O algoritmo Quicksort foi baseado em:
  https://pt.wikipedia.org/wiki/Quicksort
  http://www.ime.usp.br/~pf/algoritmos/aulas/quick.html

  - Slides de aula
  - Conversas entre colegas de turma discutindo valores de batch
  size, learning rate e outras constantes que usaram no EP
  - Video sobre redes neurais, explicando o problema da parte 3:
  https://www.youtube.com/watch?v=aircAruvnKk
"""

import nn


class PerceptronModel(object):
    def __init__(self, dimensions):
        """
        Initialize a new Perceptron instance.

        A perceptron classifies data points as either belonging to a particular
        class (+1) or not (-1). `dimensions` is the dimensionality of the data.
        For example, dimensions=2 would mean that the perceptron must classify
        2D points.
        """
        self.w = nn.Parameter(1, dimensions)

    def get_weights(self):
        """
        Return a Parameter instance with the current weights of the perceptron.
        """
        return self.w

    def run(self, x):
        """
        Calculates the score assigned by the perceptron to a data point x.

        Inputs:
            x: a node with shape (1 x dimensions)
        Returns: a node containing a single number (the score)
        """
        return nn.DotProduct(x, self.get_weights())

    def get_prediction(self, x):
        """
        Calculates the predicted class for a single data point `x`.

        Returns: 1 or -1
        """
        score = nn.as_scalar(self.run(x))
        if score < 0:
            return -1
        return 1

    def train(self, dataset):
        """
        Train the perceptron until convergence.
        """
        w = self.get_weights()
        converged = False

        while not converged:
            converged = True
            for x, y in dataset.iterate_once(1):
                if self.get_prediction(x) != nn.as_scalar(y):
                    converged = False
                    w.update(x, self.get_prediction(x))


class RegressionModel(object):
    """
    A neural network model for approximating a function that maps from real
    numbers to real numbers. The network should be sufficiently large to be able
    to approximate sin(x) on the interval [-2pi, 2pi] to reasonable precision.
    """

    def __init__(self):
        # Initialize your model parameters here
        self.w1 = nn.Parameter(1, 50)
        self.b1 = nn.Parameter(1, 50)
        self.w2 = nn.Parameter(50, 1)
        self.b2 = nn.Parameter(1, 1)

    def run(self, x):
        """
        Runs the model for a batch of examples.

        Inputs:
            x: a node with shape (batch_size x 1)
        Returns:
            A node with shape (batch_size x 1) containing predicted y-values
        """
        xw1 = nn.Linear(x, self.w1)
        xw1b1 = nn.AddBias(xw1, self.b1)
        relu1 = nn.ReLU(xw1b1)

        xw2 = nn.Linear(relu1, self.w2)

        return nn.AddBias(xw2, self.b2)

    def get_loss(self, x, y):
        """
        Computes the loss for a batch of examples.

        Inputs:
            x: a node with shape (batch_size x 1)
            y: a node with shape (batch_size x 1), containing the true y-values
                to be used for training
        Returns: a loss node
        """
        return nn.SquareLoss(self.run(x), y)

    def train(self, dataset):
        """
        Trains the model.
        """
        acceptable_loss = False
        multiplier = 0.05

        while not acceptable_loss:
            for x, y in dataset.iterate_once(50):
                loss = self.get_loss(x, y)
                gradients = nn.gradients(
                    loss, [self.w1, self.b1, self.w2, self.b2])
                self.w1.update(gradients[0], multiplier)
                self.b1.update(gradients[1], multiplier)
                self.w2.update(gradients[2], multiplier)
                self.b2.update(gradients[3], multiplier)

            new_loss = self.get_loss(nn.Constant(
                dataset.x), nn.Constant(dataset.y))
            if nn.as_scalar(new_loss) < 0.02:
                acceptable_loss = True


class DigitClassificationModel(object):
    """
    A model for handwritten digit classification using the MNIST dataset.

    Each handwritten digit is a 28x28 pixel grayscale image, which is flattened
    into a 784-dimensional vector for the purposes of this model. Each entry in
    the vector is a floating point number between 0 and 1.

    The goal is to sort each digit into one of 10 classes (number 0 through 9).

    (See RegressionModel for more information about the APIs of different
    methods here. We recommend that you implement the RegressionModel before
    working on this part of the project.)
    """

    def __init__(self):
        # Initialize your model parameters here
        self.w1 = nn.Parameter(784, 50)
        self.b1 = nn.Parameter(1, 50)
        self.w2 = nn.Parameter(50, 10)
        self.b2 = nn.Parameter(1, 10)

    def run(self, x):
        """
        Runs the model for a batch of examples.

        Your model should predict a node with shape (batch_size x 10),
        containing scores. Higher scores correspond to greater probability of
        the image belonging to a particular class.

        Inputs:
            x: a node with shape (batch_size x 784)
        Output:
            A node with shape (batch_size x 10) containing predicted scores
                (also called logits)
        """
        xw1 = nn.Linear(x, self.w1)
        xw1b1 = nn.AddBias(xw1, self.b1)
        relu1 = nn.ReLU(xw1b1)

        xw2 = nn.Linear(relu1, self.w2)
        return nn.AddBias(xw2, self.b2)

    def get_loss(self, x, y):
        """
        Computes the loss for a batch of examples.

        The correct labels `y` are represented as a node with shape
        (batch_size x 10). Each row is a one-hot vector encoding the correct
        digit class (0-9).

        Inputs:
            x: a node with shape (batch_size x 784)
            y: a node with shape (batch_size x 10)
        Returns: a loss node
        """
        return nn.SoftmaxLoss(self.run(x), y)

    def train(self, dataset):
        """
        Trains the model.
        """
        acceptable_acc = False
        multiplier = 0.1

        while not acceptable_acc:
            for x, y in dataset.iterate_once(50):
                loss = self.get_loss(x, y)
                gradients = nn.gradients(
                    loss, [self.w1, self.b1, self.w2, self.b2])
                self.w1.update(gradients[0], multiplier)
                self.b1.update(gradients[1], multiplier)
                self.w2.update(gradients[2], multiplier)
                self.b2.update(gradients[3], multiplier)

            new_accuracy = dataset.get_validation_accuracy()
            print(new_accuracy)
            if new_accuracy >= 0.96:
                acceptable_acc = True


class LanguageIDModel(object):
    """
    A model for language identification at a single-word granularity.

    (See RegressionModel for more information about the APIs of different
    methods here. We recommend that you implement the RegressionModel before
    working on this part of the project.)
    """

    def __init__(self):
        # Our dataset contains words from five different languages, and the
        # combined alphabets of the five languages contain a total of 47 unique
        # characters.
        # You can refer to self.num_chars or len(self.languages) in your code
        self.num_chars = 47
        self.languages = ["English", "Spanish", "Finnish", "Dutch", "Polish"]

        # Initialize your model parameters here
        "*** YOUR CODE HERE ***"

    def run(self, xs):
        """
        Runs the model for a batch of examples.

        Although words have different lengths, our data processing guarantees
        that within a single batch, all words will be of the same length (L).

        Here `xs` will be a list of length L. Each element of `xs` will be a
        node with shape (batch_size x self.num_chars), where every row in the
        array is a one-hot vector encoding of a character. For example, if we
        have a batch of 8 three-letter words where the last word is "cat", then
        xs[1] will be a node that contains a 1 at position (7, 0). Here the
        index 7 reflects the fact that "cat" is the last word in the batch, and
        the index 0 reflects the fact that the letter "a" is the inital (0th)
        letter of our combined alphabet for this task.

        Your model should use a Recurrent Neural Network to summarize the list
        `xs` into a single node of shape (batch_size x hidden_size), for your
        choice of hidden_size. It should then calculate a node of shape
        (batch_size x 5) containing scores, where higher scores correspond to
        greater probability of the word originating from a particular language.

        Inputs:
            xs: a list with L elements (one per character), where each element
                is a node with shape (batch_size x self.num_chars)
        Returns:
            A node with shape (batch_size x 5) containing predicted scores
                (also called logits)
        """
        "*** YOUR CODE HERE ***"

    def get_loss(self, xs, y):
        """
        Computes the loss for a batch of examples.

        The correct labels `y` are represented as a node with shape
        (batch_size x 5). Each row is a one-hot vector encoding the correct
        language.

        Inputs:
            xs: a list with L elements (one per character), where each element
                is a node with shape (batch_size x self.num_chars)
            y: a node with shape (batch_size x 5)
        Returns: a loss node
        """
        "*** YOUR CODE HERE ***"

    def train(self, dataset):
        """
        Trains the model.
        """
        "*** YOUR CODE HERE ***"

<filename>EP4/ep4.py
"""
  AO PREENCHER ESSE CABECALHO COM O MEU NOME E O MEU NUMERO USP,
  DECLARO QUE SOU A UNICA PESSOA AUTORA E RESPONSAVEL POR ESSE PROGRAMA.
  TODAS AS PARTES ORIGINAIS DESSE EXERCICIO PROGRAMA (EP) FORAM
  DESENVOLVIDAS E IMPLEMENTADAS POR MIM SEGUINDO AS INSTRUCOES
  DESSE EP E, PORTANTO, NAO CONSTITUEM ATO DE DESONESTIDADE ACADEMICA,
  FALTA DE ETICA OU PLAGIO.
  DECLARO TAMBEM QUE SOU A PESSOA RESPONSAVEL POR TODAS AS COPIAS
  DESSE PROGRAMA E QUE NAO DISTRIBUI OU FACILITEI A
  SUA DISTRIBUICAO. ESTOU CIENTE QUE OS CASOS DE PLAGIO E
  DESONESTIDADE ACADEMICA SERAO TRATADOS SEGUNDO OS CRITERIOS
  DIVULGADOS NA PAGINA DA DISCIPLINA.
  ENTENDO QUE EPS SEM ASSINATURA NAO SERAO CORRIGIDOS E,
  AINDA ASSIM, PODERAO SER PUNIDOS POR DESONESTIDADE ACADEMICA.

  Nome : <NAME>
  NUSP : 10295429

  Referencias: Com excecao das rotinas fornecidas no enunciado
  e em sala de aula, caso voce tenha utilizado alguma referencia,
  liste-as abaixo para que o seu programa nao seja considerado
  plagio ou irregular.

  Exemplo:
  - O algoritmo Quicksort foi baseado em:
  https://pt.wikipedia.org/wiki/Quicksort
  http://www.ime.usp.br/~pf/algoritmos/aulas/quick.html

  - Slides de aula
  - Conversas entre colegas de turma discutindo valores de batch
  size, learning rate e outras constantes que usaram no EP
  - Video sobre redes neurais, explicando o problema da parte 3:
  https://www.youtube.com/watch?v=aircAruvnKk
"""

import nn


class PerceptronModel(object):
    def __init__(self, dimensions):
        """
        Initialize a new Perceptron instance.

        A perceptron classifies data points as either belonging to a particular
        class (+1) or not (-1). `dimensions` is the dimensionality of the data.
        For example, dimensions=2 would mean that the perceptron must classify
        2D points.
        """
        self.w = nn.Parameter(1, dimensions)

    def get_weights(self):
        """
        Return a Parameter instance with the current weights of the perceptron.
        """
        return self.w

    def run(self, x):
        """
        Calculates the score assigned by the perceptron to a data point x.

        Inputs:
            x: a node with shape (1 x dimensions)
        Returns: a node containing a single number (the score)
        """
        return nn.DotProduct(x, self.get_weights())

    def get_prediction(self, x):
        """
        Calculates the predicted class for a single data point `x`.

        Returns: 1 or -1
        """
        score = nn.as_scalar(self.run(x))
        if score < 0:
            return -1
        return 1

    def train(self, dataset):
        """
        Train the perceptron until convergence.
        """
        w = self.get_weights()
        converged = False

        while not converged:
            converged = True
            for x, y in dataset.iterate_once(1):
                if self.get_prediction(x) != nn.as_scalar(y):
                    converged = False
                    w.update(x, self.get_prediction(x))


class RegressionModel(object):
    """
    A neural network model for approximating a function that maps from real
    numbers to real numbers. The network should be sufficiently large to be able
    to approximate sin(x) on the interval [-2pi, 2pi] to reasonable precision.
    """

    def __init__(self):
        # Initialize your model parameters here
        self.w1 = nn.Parameter(1, 50)
        self.b1 = nn.Parameter(1, 50)
        self.w2 = nn.Parameter(50, 1)
        self.b2 = nn.Parameter(1, 1)

    def run(self, x):
        """
        Runs the model for a batch of examples.

        Inputs:
            x: a node with shape (batch_size x 1)
        Returns:
            A node with shape (batch_size x 1) containing predicted y-values
        """
        xw1 = nn.Linear(x, self.w1)
        xw1b1 = nn.AddBias(xw1, self.b1)
        relu1 = nn.ReLU(xw1b1)

        xw2 = nn.Linear(relu1, self.w2)

        return nn.AddBias(xw2, self.b2)

    def get_loss(self, x, y):
        """
        Computes the loss for a batch of examples.

        Inputs:
            x: a node with shape (batch_size x 1)
            y: a node with shape (batch_size x 1), containing the true y-values
                to be used for training
        Returns: a loss node
        """
        return nn.SquareLoss(self.run(x), y)

    def train(self, dataset):
        """
        Trains the model.
        """
        acceptable_loss = False
        multiplier = 0.05

        while not acceptable_loss:
            for x, y in dataset.iterate_once(50):
                loss = self.get_loss(x, y)
                gradients = nn.gradients(
                    loss, [self.w1, self.b1, self.w2, self.b2])
                self.w1.update(gradients[0], multiplier)
                self.b1.update(gradients[1], multiplier)
                self.w2.update(gradients[2], multiplier)
                self.b2.update(gradients[3], multiplier)

            new_loss = self.get_loss(nn.Constant(
                dataset.x), nn.Constant(dataset.y))
            if nn.as_scalar(new_loss) < 0.02:
                acceptable_loss = True


class DigitClassificationModel(object):
    """
    A model for handwritten digit classification using the MNIST dataset.

    Each handwritten digit is a 28x28 pixel grayscale image, which is flattened
    into a 784-dimensional vector for the purposes of this model. Each entry in
    the vector is a floating point number between 0 and 1.

    The goal is to sort each digit into one of 10 classes (number 0 through 9).

    (See RegressionModel for more information about the APIs of different
    methods here. We recommend that you implement the RegressionModel before
    working on this part of the project.)
    """

    def __init__(self):
        # Initialize your model parameters here
        self.w1 = nn.Parameter(784, 50)
        self.b1 = nn.Parameter(1, 50)
        self.w2 = nn.Parameter(50, 10)
        self.b2 = nn.Parameter(1, 10)

    def run(self, x):
        """
        Runs the model for a batch of examples.

        Your model should predict a node with shape (batch_size x 10),
        containing scores. Higher scores correspond to greater probability of
        the image belonging to a particular class.

        Inputs:
            x: a node with shape (batch_size x 784)
        Output:
            A node with shape (batch_size x 10) containing predicted scores
                (also called logits)
        """
        xw1 = nn.Linear(x, self.w1)
        xw1b1 = nn.AddBias(xw1, self.b1)
        relu1 = nn.ReLU(xw1b1)

        xw2 = nn.Linear(relu1, self.w2)
        return nn.AddBias(xw2, self.b2)

    def get_loss(self, x, y):
        """
        Computes the loss for a batch of examples.

        The correct labels `y` are represented as a node with shape
        (batch_size x 10). Each row is a one-hot vector encoding the correct
        digit class (0-9).

        Inputs:
            x: a node with shape (batch_size x 784)
            y: a node with shape (batch_size x 10)
        Returns: a loss node
        """
        return nn.SoftmaxLoss(self.run(x), y)

    def train(self, dataset):
        """
        Trains the model.
        """
        acceptable_acc = False
        multiplier = 0.1

        while not acceptable_acc:
            for x, y in dataset.iterate_once(50):
                loss = self.get_loss(x, y)
                gradients = nn.gradients(
                    loss, [self.w1, self.b1, self.w2, self.b2])
                self.w1.update(gradients[0], multiplier)
                self.b1.update(gradients[1], multiplier)
                self.w2.update(gradients[2], multiplier)
                self.b2.update(gradients[3], multiplier)

            new_accuracy = dataset.get_validation_accuracy()
            print(new_accuracy)
            if new_accuracy >= 0.96:
                acceptable_acc = True


class LanguageIDModel(object):
    """
    A model for language identification at a single-word granularity.

    (See RegressionModel for more information about the APIs of different
    methods here. We recommend that you implement the RegressionModel before
    working on this part of the project.)
    """

    def __init__(self):
        # Our dataset contains words from five different languages, and the
        # combined alphabets of the five languages contain a total of 47 unique
        # characters.
        # You can refer to self.num_chars or len(self.languages) in your code
        self.num_chars = 47
        self.languages = ["English", "Spanish", "Finnish", "Dutch", "Polish"]

        # Initialize your model parameters here
        "*** YOUR CODE HERE ***"

    def run(self, xs):
        """
        Runs the model for a batch of examples.

        Although words have different lengths, our data processing guarantees
        that within a single batch, all words will be of the same length (L).

        Here `xs` will be a list of length L. Each element of `xs` will be a
        node with shape (batch_size x self.num_chars), where every row in the
        array is a one-hot vector encoding of a character. For example, if we
        have a batch of 8 three-letter words where the last word is "cat", then
        xs[1] will be a node that contains a 1 at position (7, 0). Here the
        index 7 reflects the fact that "cat" is the last word in the batch, and
        the index 0 reflects the fact that the letter "a" is the inital (0th)
        letter of our combined alphabet for this task.

        Your model should use a Recurrent Neural Network to summarize the list
        `xs` into a single node of shape (batch_size x hidden_size), for your
        choice of hidden_size. It should then calculate a node of shape
        (batch_size x 5) containing scores, where higher scores correspond to
        greater probability of the word originating from a particular language.

        Inputs:
            xs: a list with L elements (one per character), where each element
                is a node with shape (batch_size x self.num_chars)
        Returns:
            A node with shape (batch_size x 5) containing predicted scores
                (also called logits)
        """
        "*** YOUR CODE HERE ***"

    def get_loss(self, xs, y):
        """
        Computes the loss for a batch of examples.

        The correct labels `y` are represented as a node with shape
        (batch_size x 5). Each row is a one-hot vector encoding the correct
        language.

        Inputs:
            xs: a list with L elements (one per character), where each element
                is a node with shape (batch_size x self.num_chars)
            y: a node with shape (batch_size x 5)
        Returns: a loss node
        """
        "*** YOUR CODE HERE ***"

    def train(self, dataset):
        """
        Trains the model.
        """
        "*** YOUR CODE HERE ***"

AO PREENCHER ESSE CABECALHO COM O MEU NOME E O MEU NUMERO USP,   DECLARO QUE SOU A UNICA PESSOA AUTORA E RESPONSAVEL POR ESSE PROGRAMA.   TODAS AS PARTES ORIGINAIS DESSE EXERCICIO PROGRAMA (EP) FORAM   DESENVOLVIDAS E IMPLEMENTADAS POR MIM SEGUINDO AS INSTRUCOES   DESSE EP E, PORTANTO, NAO CONSTITUEM ATO DE DESONESTIDADE ACADEMICA,   FALTA DE ETICA OU PLAGIO.   DECLARO TAMBEM QUE SOU A PESSOA RESPONSAVEL POR TODAS AS COPIAS   DESSE PROGRAMA E QUE NAO DISTRIBUI OU FACILITEI A   SUA DISTRIBUICAO. ESTOU CIENTE QUE OS CASOS DE PLAGIO E   DESONESTIDADE ACADEMICA SERAO TRATADOS SEGUNDO OS CRITERIOS   DIVULGADOS NA PAGINA DA DISCIPLINA.   ENTENDO QUE EPS SEM ASSINATURA NAO SERAO CORRIGIDOS E,   AINDA ASSIM, PODERAO SER PUNIDOS POR DESONESTIDADE ACADEMICA.    Nome : <NAME>   NUSP : 10295429    Referencias: Com excecao das rotinas fornecidas no enunciado   e em sala de aula, caso voce tenha utilizado alguma referencia,   liste-as abaixo para que o seu programa nao seja considerado   plagio ou irregular.    Exemplo:   - O algoritmo Quicksort foi baseado em:   https://pt.wikipedia.org/wiki/Quicksort   http://www.ime.usp.br/~pf/algoritmos/aulas/quick.html    - Slides de aula   - Conversas entre colegas de turma discutindo valores de batch   size, learning rate e outras constantes que usaram no EP   - Video sobre redes neurais, explicando o problema da parte 3:   https://www.youtube.com/watch?v=aircAruvnKk Initialize a new Perceptron instance.          A perceptron classifies data points as either belonging to a particular         class (+1) or not (-1). `dimensions` is the dimensionality of the data.         For example, dimensions=2 would mean that the perceptron must classify         2D points. Return a Parameter instance with the current weights of the perceptron. Calculates the score assigned by the perceptron to a data point x.          Inputs:             x: a node with shape (1 x dimensions)         Returns: a node containing a single number (the score) Calculates the predicted class for a single data point `x`.          Returns: 1 or -1 Train the perceptron until convergence. A neural network model for approximating a function that maps from real     numbers to real numbers. The network should be sufficiently large to be able     to approximate sin(x) on the interval [-2pi, 2pi] to reasonable precision. # Initialize your model parameters here Runs the model for a batch of examples.          Inputs:             x: a node with shape (batch_size x 1)         Returns:             A node with shape (batch_size x 1) containing predicted y-values Computes the loss for a batch of examples.          Inputs:             x: a node with shape (batch_size x 1)             y: a node with shape (batch_size x 1), containing the true y-values                 to be used for training         Returns: a loss node Trains the model. A model for handwritten digit classification using the MNIST dataset.      Each handwritten digit is a 28x28 pixel grayscale image, which is flattened     into a 784-dimensional vector for the purposes of this model. Each entry in     the vector is a floating point number between 0 and 1.      The goal is to sort each digit into one of 10 classes (number 0 through 9).      (See RegressionModel for more information about the APIs of different     methods here. We recommend that you implement the RegressionModel before     working on this part of the project.) # Initialize your model parameters here Runs the model for a batch of examples.          Your model should predict a node with shape (batch_size x 10),         containing scores. Higher scores correspond to greater probability of         the image belonging to a particular class.          Inputs:             x: a node with shape (batch_size x 784)         Output:             A node with shape (batch_size x 10) containing predicted scores                 (also called logits) Computes the loss for a batch of examples.          The correct labels `y` are represented as a node with shape         (batch_size x 10). Each row is a one-hot vector encoding the correct         digit class (0-9).          Inputs:             x: a node with shape (batch_size x 784)             y: a node with shape (batch_size x 10)         Returns: a loss node Trains the model. A model for language identification at a single-word granularity.      (See RegressionModel for more information about the APIs of different     methods here. We recommend that you implement the RegressionModel before     working on this part of the project.) # Our dataset contains words from five different languages, and the # combined alphabets of the five languages contain a total of 47 unique # characters. # You can refer to self.num_chars or len(self.languages) in your code # Initialize your model parameters here Runs the model for a batch of examples.          Although words have different lengths, our data processing guarantees         that within a single batch, all words will be of the same length (L).          Here `xs` will be a list of length L. Each element of `xs` will be a         node with shape (batch_size x self.num_chars), where every row in the         array is a one-hot vector encoding of a character. For example, if we         have a batch of 8 three-letter words where the last word is "cat", then         xs[1] will be a node that contains a 1 at position (7, 0). Here the         index 7 reflects the fact that "cat" is the last word in the batch, and         the index 0 reflects the fact that the letter "a" is the inital (0th)         letter of our combined alphabet for this task.          Your model should use a Recurrent Neural Network to summarize the list         `xs` into a single node of shape (batch_size x hidden_size), for your         choice of hidden_size. It should then calculate a node of shape         (batch_size x 5) containing scores, where higher scores correspond to         greater probability of the word originating from a particular language.          Inputs:             xs: a list with L elements (one per character), where each element                 is a node with shape (batch_size x self.num_chars)         Returns:             A node with shape (batch_size x 5) containing predicted scores                 (also called logits) Computes the loss for a batch of examples.          The correct labels `y` are represented as a node with shape         (batch_size x 5). Each row is a one-hot vector encoding the correct         language.          Inputs:             xs: a list with L elements (one per character), where each element                 is a node with shape (batch_size x self.num_chars)             y: a node with shape (batch_size x 5)         Returns: a loss node Trains the model.