Classifying names with a character-level rnn

Beirouti: <p>I’m trying to tailor the <a href="https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html" rel="nofollow noopener">tutorial</a> towards my particular need, but I am not getting predictable and consistent output. It’s my first time using neural networks so excuse the nature of my questions.</p>
<p>I have 3 labeled datasets, with a total size of 27,666 that I will train the model on (80% - set1=8000, set2=5821, set3=8312) and then evaluate (20%) to calculate the accuracy of prediction of each category.</p>
<p>The main parameters I am tweaking are:</p>
<ol>
<li>n_iter</li>
<li>learning_rate</li>
<li>n_hidden</li>
<li>n_confusion</li>
</ol>
<p>At the moment n_iter is the entire 80% of the data set and n_hidden, n_confusion are fixed to the value in the tutorial. I am changing the learning rate from 0.00005 to 0.001.</p>
<p>Questions:</p>
<ol>
<li>What is the link between the train function (that uses the learning rate variable) and the evaluate function (that is used in the prediction)?</li>
<li>Out of the 4 parameters above, which one has the most effect on the accuracy of prediction?</li>
<li>How can I add more linear layers?</li>
</ol>

ptrblck: <ol>
<li>
<p>The train function is used to train the model, i.e. tune its parameters so that the training loss decreases. The evaluate function is used to measure the current accuracy of your model on the validation dataset, which was not used to train the model. In the best case the validation accuracy should give you approx. the test accuracy, i.e. the final accuracy your model will have on new, unseen data in your future application.</p>
</li>
<li>
<p>Probably all except number 4, which is just the number of iterations to get a random sample for your confusion matrix.<br>
<code>n_iters</code> defines the number of iterations to train your model. After some epochs you might see that the training loss does not decrease anymore or the validation loss starts to increase. In that case, you could stop the training, as your model will most likely overfit on the training data after this step (also called early stopping).<br>
<code>learning_rate</code> will define the step size your optimizer will use to change the parameters using the gradients. Some optimizers have also other parameters like <code>momentum</code> and running estimates.<br>
<code>n_hidden</code> defines basically the model capacity in this example. If your model has more capacity, it might learn the training dataset better, but might also overfit easier. If you see a good training accuracy and a bad validation accuracy, you might want to reduce the capacity or add regularization techniques.</p>
</li>
</ol>
<p>Note that these explanations are really scratching the surface and there is a ton of papers about each step, so please take this info with a grain of salt. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<ol start="3">
<li>Where would you like to add this additional layer? If after the <code>output</code>, you could use this code:</li>
</ol>
<pre><code class="lang-python">class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()

        self.hidden_size = hidden_size

        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(input_size + hidden_size, output_size)
        self.additional_lin = nn.Linear(output_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(combined)
        output = F.relu(self.additional_lin(output))
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, self.hidden_size)
</code></pre>
<p>As you can see, you would just have to define this layer in <code>__init__</code> with the right number of input and output features and apply it in <code>forward</code>.</p>