This commit is contained in:
shoney.arickathil 2023-08-23 08:43:09 +02:00
parent f8f5c723a3
commit 0b11ddf99b
10 changed files with 100 additions and 10 deletions

View file

@ -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}
}

View file

@ -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,8 +344,8 @@ 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}
@ -353,5 +353,36 @@ The steps involved in training a model with \acs{CLR} are as follows :
\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}
\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}.

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
loss_tlr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

BIN
loss_tlr2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

BIN
loss_tlr_5000.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
lossinfo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View file

@ -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()
@ -25,6 +25,10 @@ class Train():
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):
@ -128,3 +132,60 @@ class Train():
# 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")

View file

@ -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)