Repository navigation
Expand file tree
/
Copy pathmain.tex
More file actions
145 lines (124 loc) · 8.59 KB
/
Copy pathmain.tex
File metadata and controls
145 lines (124 loc) · 8.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
\documentclass[11pt]{article}
% --- Margins (similar to ACL) ---
\usepackage[a4paper,margin=2.5cm]{geometry}
% --- Fonts ---
\usepackage{times} % main text font
\usepackage{latexsym} % symbols for NLP-style papers
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{verbatim}
% --- Section spacing (ACL style tweaks) ---
\usepackage{titlesec}
\titlespacing*{\section}{0pt}{2ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing*{\subsection}{0pt}{1.8ex plus .5ex minus .2ex}{0.8ex plus .2ex}
\titlespacing*{\subsubsection}{0pt}{1.5ex plus .5ex minus .2ex}{0.5ex plus .2ex}
% --- Caption styling ---
\usepackage{caption}
\captionsetup{font=small, labelfont=bf}
\title{Assignment 2 Writeup}
\author{YuChen (Jean) Lin \\ \texttt{lin.4842@osu.edu}}
\date{}
\begin{document}
\maketitle
\section{Part 1}
\subsection{Results}
Training a one-layer Transformer model on the counting task for 15 epochs yielded the following average loss per batch for each epoch:
\begin{verbatim}
Epoch 1, average loss per batch: 2.1955
Epoch 2, average loss per batch: 1.9638
Epoch 3, average loss per batch: 1.8906
Epoch 4, average loss per batch: 1.8551
Epoch 5, average loss per batch: 1.8357
Epoch 6, average loss per batch: 1.8208
Epoch 7, average loss per batch: 1.8103
Epoch 8, average loss per batch: 1.8119
Epoch 9, average loss per batch: 1.8117
Epoch 10, average loss per batch: 1.8120
Epoch 11, average loss per batch: 1.8243
Epoch 12, average loss per batch: 1.8164
Epoch 13, average loss per batch: 1.8177
Epoch 14, average loss per batch: 1.8264
Epoch 15, average loss per batch: 1.8112
Training completed in 2.19 minutes
=====Results=====
{
"sane": true,
"normalizes": true,
"range": true,
"log_prob": -945.3776791859418,
"avg_log_prob": -1.8907553583718837,
"perplexity": 6.624370565462698
}
\end{verbatim}
\subsection{Exploration 1}
The one-layer Transformer model's attention heatmap Figure~\ref{fig:one-layer-model} aligns well with expectations for the counting task. Rows represent query positions (tokens being predicted), and columns represent key positions (tokens attended to). The causal mask is strictly respected, with non-zero attention mass only on or to the left of the diagonal, ensuring no future tokens are used for predictions. Diagonal peaks indicate strong self-attention, as the model often attends to the current token itself, which is crucial for determining whether it is a new occurrence or a repeat. Off-diagonal peaks at earlier same-character positions show the model explicitly attending to prior occurrences, a clear signal that it uses past context to solve the counting task. The attention patterns vary between diffuse (spread across multiple earlier positions) and focused (sharp peaks), reflecting the model's confidence and the ambiguity of the context. Overall, the one-layer model demonstrates interpretable and task-appropriate attention behavior.
\begin{figure}[ht]
\centering
\includegraphics[width=\linewidth]{writeup_plots/plots_1_layer_legend/0_attns0.png}
\caption{Attention heatmap for the one-layer Transformer model. Rows represent query positions, and columns represent key positions. The model demonstrates interpretable attention behavior, with strong self-attention and explicit use of past context.}
\label{fig:one-layer-model}
\end{figure}
\begin{figure}[ht]
\centering
\begin{tabular}{cc}
\subcaptionbox{First layer of multi-layer model \label{fig:multi-layer-1-layer}}{
\includegraphics[width=0.45\linewidth]{writeup_plots/plots_4_layers_legend/0_attns0.png}} &
\subcaptionbox{Second layer of multi-layer model \label{fig:multi-layer-2-layer}}{
\includegraphics[width=0.45\linewidth]{writeup_plots/plots_4_layers_legend/0_attns1.png}} \\
\subcaptionbox{Third layer of multi-layer model \label{fig:multi-layer-3-layer}}{
\includegraphics[width=0.45\linewidth]{writeup_plots/plots_4_layers_legend/0_attns2.png}} &
\subcaptionbox{Fourth layer of multi-layer model \label{fig:multi-layer-4-layer}}{
\includegraphics[width=0.45\linewidth]{writeup_plots/plots_4_layers_legend/0_attns3.png}} \\
\end{tabular}
\caption{Results of multi-layer model.}
\label{fig:multi-layer-model}
\end{figure}
In contrast, the multi-layer Transformer model, Figure~\ref{fig:multi-layer-model}, exhibits significant differences in its first-layer attention maps when using the same input as the one-layer model. These differences stem from the deeper architecture and optimization dynamics. The multi-layer model's first layer, Figure~\ref{fig:multi-layer-1-layer}, is influenced by gradients propagated through subsequent layers, leading to different learned representations compared to the one-layer model. While the first layer still respects causality, the higher layers (Figure~\ref{fig:multi-layer-3-layer} to Figure~\ref{fig:multi-layer-4-layer}) show signs of optimization collapse, with attention mass overly concentrated on the first column. This collapse disrupts the expected patterns, as the model fails to distribute attention appropriately across relevant past positions. These results highlight the challenges of training deeper models and the need for careful tuning to prevent such issues.
\subsection{Exploration 2}
I implemented multi-headed attention as described in the "Attention Is All You Need" paper. Multi-head attention provides richer feature representations by allowing the model to focus on multiple aspects of the input sequence simultaneously. This leads to faster convergence during training, with a steeper decline in the loss curve in the initial epochs, and more stable gradient updates, reducing the risk of vanishing or exploding gradients. The model achieves improved accuracy on both training and validation datasets and demonstrates better generalization to unseen data, particularly for tasks involving long-range dependencies.
However, these benefits come at the cost of increased computational requirements. The additional computations for multiple attention heads result in longer training times per epoch and higher memory usage to store parameters and intermediate results. Visualizing attention maps reveals that different heads focus on diverse parts of the input sequence, providing valuable insights into the model's learning process. Overall, multi-head attention enhances the model's ability to learn and generalize effectively, making it particularly suitable for tasks with complex input patterns or long input sequences.
\section{Part 2}
\subsection{Results}
\begin{verbatim}
Epoch 1/10 - avg loss per example: 0.282525
Epoch 2/10 - avg loss per example: 0.088344
Epoch 3/10 - avg loss per example: 0.059189
Epoch 4/10 - avg loss per example: 0.047644
Epoch 5/10 - avg loss per example: 0.043536
Epoch 6/10 - avg loss per example: 0.037482
Epoch 7/10 - avg loss per example: 0.035319
Epoch 8/10 - avg loss per example: 0.032457
Epoch 9/10 - avg loss per example: 0.030124
Epoch 10/10 - avg loss per example: 0.029298
INPUT 0: heir average albedo
GOLD 0: array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 0, 0, 2])
PRED 0: array([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 0, 0, 2])
Passed normalization test on attention maps
INPUT 1: ed by rank and file
GOLD 1: array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 1, 2])
PRED 1: array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 1, 2])
Passed normalization test on attention maps
INPUT 2: s can also extend in
GOLD 2: array([0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 0, 2, 0, 2])
PRED 2: array([0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 1, 1, 0, 2, 0, 2])
Passed normalization test on attention maps
INPUT 3: erages between nine
GOLD 3: array([0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 2, 0, 1, 1, 0, 2, 2, 2])
PRED 3: array([0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 2, 2, 0, 1, 1, 0, 1, 2, 2])
Passed normalization test on attention maps
INPUT 4: that civilization n
GOLD 4: array([0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 2, 0, 1, 2, 2, 0, 0, 2, 1])
PRED 4: array([0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 2, 0, 1, 2, 2, 0, 0, 2, 1])
Passed normalization test on attention maps
Accuracy: 99 / 100 = 0.990000
Training accuracy (100 exs):
Accuracy: 1986 / 2000 = 0.993000
Dev accuracy (whole set):
Decoding on a large number of examples (1000); not printing or plotting
Accuracy: 19905 / 20000 = 0.995250
\end{verbatim}
\subsection{Discussion}
The results indicate that the model has learned to predict the next character in a sequence with high accuracy. The training and development accuracies are both above 99\%, suggesting that the model is not overfitting and is generalizing well to unseen data. The low average loss values further confirm the model's effectiveness in capturing the underlying patterns in the data.
\end{document}