diff --git a/doc/References.bib b/doc/References.bib index d134f20..3e94233 100644 --- a/doc/References.bib +++ b/doc/References.bib @@ -299,10 +299,8 @@ abstract = {It is known that the learning rate is the most important hyper-parameter to tune for training deep neural networks. This paper describes a new method for setting the learning rate, named cyclical learning rates, which practically eliminates the need to experimentally find the best values and schedule for the global learning rates. Instead of monotonically decreasing the learning rate, this method lets the learning rate cyclically vary between reasonable boundary values. Training with cyclical learning rates instead of fixed values achieves improved classification accuracy without a need to tune and often in fewer iterations. This paper also describes a simple way to estimate {\textquotedbl}reasonable bounds{\textquotedbl} -- linearly increasing the learning rate of the network for a few epochs. In addition, cyclical learning rates are demonstrated on the CIFAR-10 and CIFAR-100 datasets with ResNets, Stochastic Depth networks, and DenseNets, and the ImageNet dataset with the AlexNet and GoogLeNet architectures. These are practical tools for everyone who trains neural networks.}, author = {Smith, Leslie N.}, date = {03/06/2015}, - title = {Cyclical Learning Rates for Training Neural Networks}, - url = {https://arxiv.org/pdf/1506.01186.pdf}, - urldate = {13/08/2023}, - file = {1506.01186:Attachments/1506.01186.pdf:application/pdf} + year = {2015}, + title = {Cyclical Learning Rates for Training Neural Networks} } diff --git a/doc/chapters/chapter7.tex b/doc/chapters/chapter7.tex index 539554b..1f678c8 100644 --- a/doc/chapters/chapter7.tex +++ b/doc/chapters/chapter7.tex @@ -298,7 +298,7 @@ The steps involved in training a model with \acs{CLR} are as follows : \end{enumerate} -\subsection{Learning rate range} +\subsection{Experimentation and analysis to determine learning rate range} \begin{enumerate} \item Experiment by setting $base\_lr$ and $max\_lr$ manually : \\ @@ -344,14 +344,45 @@ The steps involved in training a model with \acs{CLR} are as follows : \label{fig:Loss value at 0.015} \end{figure} - \item From the above experiment we concluded that $max\_lr = 0.02$. works. - Notice in figure \ref{fig:Loss value at 0.015}, the loss had not neared zero and the model fails to predict the classification. + \item From the earlier mentioned experimentation with $max\_lr$, the loss classification model is nearing zero by the end the training when $max\_lr > 0.015$ + Figure \ref{fig:Loss value at 0.02} illustrates the loss reaching near zero when $max\_lr = 0.02$. The figure \ref{fig:Loss value at 0.02}, there is a plateau until the $ \epsilon = 0.0075$. Indicating that the model did not learn until the learning rate $ \epsilon = 0.0075$. \begin{figure}[H] \centering \includegraphics[scale=0.9]{loss_002.png} \caption{Loss vs Learning rate; where $max\_lr = 0.02$} \label{fig:Loss value at 0.02} \end{figure} + + \item Updating the $base_lr=0.0075$ and $max_lr=0.02$, resulted a steady slop in the loss vs learning rate. + Figure \ref{fig:Loss value at 0.0075} illustrates the loss reaching near zero when $max\_lr = 0.02$ and $base_lr=0.0075$. The steady decent indicates the learning rate range is optimal. + \begin{figure}[H] + \centering + \includegraphics[scale=0.9]{loss_0075_002.png} + \caption{Loss vs Learning rate; where $base\_lr = 0.0075$ and $max\_lr = 0.02$ } + \label{fig:Loss value at 0.0075} + \end{figure} -\end{enumerate} \ No newline at end of file +\end{enumerate} + +\section{Triangular learning rate} + +\parencite{Smith.03062015} introduced the concept of letting the learning rate vary within the range of minimum and maximum boundaries of learning rate known as learning rate range. In this method the learning rate linearly increases until step size $ t $ and then linearly decreases. + + +How triangular learning rate works? + +\begin{enumerate} + \item \textbf{Initialize}: The step size $ t $, minimum bound or base learning rate $ base\_lr$ and maximum bound or the max learning rate $max\_lr$ are initialized. These hyperparameters are tuned based on the architecture of the model. In this experiment, the values set are $ base\_lr=0.0075 ; max\_lr=0.02; t=10000$. + \item \textbf{Up}: During training, the learning rate is linearly increased from $ base\_lr$ to $max\_lr$ over a certain number of step size $ t $. + \item \textbf{Down}: During training, the learning rate is linearly decreased from $max\_lr$ to $base\_lr$. +\end{enumerate} + +\begin{figure}[H] + \centering + \includegraphics[scale=0.9]{triangular learning rate.png} + \caption{Triangular learning rate policy. \parencite{Smith.03062015} + } + \label{fig:trl} +\end{figure} + In figure \ref{fig:trl}, the blue lines represent learning rate values changing between the bounds. The input parameter stepsize is the number of iterations in half a cycle \parencite{Smith.03062015}. diff --git a/doc/images/loss_0075_002.png b/doc/images/loss_0075_002.png new file mode 100644 index 0000000..a5930b9 Binary files /dev/null and b/doc/images/loss_0075_002.png differ diff --git a/doc/images/triangular learning rate.png b/doc/images/triangular learning rate.png new file mode 100644 index 0000000..393b46c Binary files /dev/null and b/doc/images/triangular learning rate.png differ diff --git a/loss_tlr.png b/loss_tlr.png new file mode 100644 index 0000000..651e5bc Binary files /dev/null and b/loss_tlr.png differ diff --git a/loss_tlr2.png b/loss_tlr2.png new file mode 100644 index 0000000..dcf2c4b Binary files /dev/null and b/loss_tlr2.png differ diff --git a/loss_tlr_5000.png b/loss_tlr_5000.png new file mode 100644 index 0000000..c12e09e Binary files /dev/null and b/loss_tlr_5000.png differ diff --git a/lossinfo.png b/lossinfo.png new file mode 100644 index 0000000..e49e8de Binary files /dev/null and b/lossinfo.png differ diff --git a/rm_clasificationmodel/TrainClass.py b/rm_clasificationmodel/TrainClass.py index c81d5ff..5a35063 100644 --- a/rm_clasificationmodel/TrainClass.py +++ b/rm_clasificationmodel/TrainClass.py @@ -15,7 +15,7 @@ class Train(): self.n_hidden = 128 self.rnn= RNN(self.inputSize, self.n_hidden, self.n_category) - self.lr_low=0.00000000001 + self.lr_low=0.0075 self.lr_max=0.02 self.learning_rate= 0.00000000000001 self.criterion = nn.NLLLoss() @@ -24,6 +24,10 @@ class Train(): self.all_losses = [] self.helper= Helper() + + # Define the number of iterations or epochs for the up and down phases + self.up_iterations = 1000 + self.down_iterations = 1000 def train(self,category_tensor, name_tensor): @@ -127,4 +131,61 @@ class Train(): plt.xlabel('Learning rate') # Title and display the plot plt.title('Learning Rates vs. Losses') + plt.savefig("lossinfo") + + + + def runBatchWithTLR(self): + batch=[10000,10000,10000,10000,10000,10000,10000,10000,10000,10000] + start = time.time() + no_batch=0 + no_iterations=0 + learning_rates =[] + + for n_iters in batch: + + no_batch =no_batch+1 + isEven=no_batch % 2 + learn_rates=np.linspace(self.lr_low,self.lr_max,n_iters) + if isEven==0: + learn_rates=np.flip(learn_rates) + + for epoch,lr in zip(range(1, n_iters + 1),learn_rates): + no_iterations= no_iterations+1 + + self.learning_rate=lr + category, name, category_tensor, name_tensor = self.data.randomTrainingExample() + + output, loss = self.train(category_tensor, name_tensor) + self.current_loss += loss + # Print ``iter`` number, loss, name and guess + if epoch % 5000 == 0: + guess, guess_i = self.data.categoryFromOutput(output) + correct = '✓' if guess == category else '✗ (%s)' % category + + print('%d %d%% (%s) %.4f %s / %s %s %.10f' % (no_iterations, + no_iterations / np.sum(batch) * 100, + self.helper.timeSince(start), + loss, + name, + guess, + correct, self.learning_rate)) + + + if epoch % 5000 == 0: + if (math.isnan(self.current_loss)!=True): + self.all_losses.append(self.current_loss / 5000) + learning_rates.append(self.learning_rate) + self.current_loss = 0 + else: + break + + # Plot loss curve + + plt.figure() + plt.plot(self.all_losses) + plt.ylabel("Loss") + plt.xlabel('Epoch') + # # Title and display the plot + plt.title('Losses Vs No of Epoch') plt.savefig("lossinfo") \ No newline at end of file diff --git a/rm_clasificationmodel/main.py b/rm_clasificationmodel/main.py index 1cca4f1..d5b5534 100644 --- a/rm_clasificationmodel/main.py +++ b/rm_clasificationmodel/main.py @@ -27,7 +27,7 @@ def main(): # dfProductTaxonomyEn=df.getProductTaxonomy(10000,0) df_en = df.getNormal() train = Train(df_en) - train.runBatch() + train.runBatchWithTLR() # predict = Predict() # # predict.confusionMatix(df_en)