-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnn_lstm_captioning.py
963 lines (824 loc) · 40.4 KB
/
rnn_lstm_captioning.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
import math
from typing import Optional, Tuple
import torch
# from torch._C import T
import torchvision
from torch import nn
from torch.nn import functional as F
from torchvision.models import feature_extraction
def hello_rnn_lstm_captioning():
print("Hello from rnn_lstm_captioning.py!")
class ImageEncoder(nn.Module):
"""
Convolutional network that accepts images as input and outputs their spatial
grid features. This module servesx as the image encoder in image captioning
model. We will use a tiny RegNet-X 400MF model that is initialized with
ImageNet-pretrained weights from Torchvision library.
NOTE: We could use any convolutional network architecture, but we opt for a
tiny RegNet model so it can train decently with a single K80 Colab GPU.
"""
def __init__(self, pretrained: bool = True, verbose: bool = True):
"""
Args:
pretrained: Whether to initialize this model with pretrained weights
from Torchvision library.
verbose: Whether to log expected output shapes during instantiation.
"""
super().__init__()
self.cnn = torchvision.models.regnet_x_400mf(pretrained=pretrained)
# Torchvision models return global average pooled features by default.
# Our attention-based models may require spatial grid features. So we
# wrap the ConvNet with torchvision's feature extractor. We will get
# the spatial features right before the final classification layer.
self.backbone = feature_extraction.create_feature_extractor(
self.cnn, return_nodes={"trunk_output.block4": "c5"}
)
# We call these features "c5", a name that may sound familiar from the
# object detection assignment. :-)
# Pass a dummy batch of input images to infer output shape.
# dummy_out = self.backbone(torch.randn(2, 3, 224, 224))["c5"]
dummy_out = self.backbone(torch.randn(2, 3, 112, 112))["c5"]
self._out_channels = dummy_out.shape[1]
if verbose:
print("For input images in NCHW format, shape (2, 3, 112, 112)")
print(f"Shape of output c5 features: {dummy_out.shape}")
# Input image batches are expected to be float tensors in range [0, 1].
# However, the backbone here expects these tensors to be normalized by
# ImageNet color mean/std (as it was trained that way).
# We define a function to transform the input images before extraction:
self.normalize = torchvision.transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
@property
def out_channels(self):
"""
Number of output channels in extracted image features. You may access
this value freely to define more modules to go with this encoder.
"""
return self._out_channels
def forward(self, images: torch.Tensor):
# Input images may be uint8 tensors in [0-255], change them to float
# tensors in [0-1]. Get float type from backbone (could be float32/64).
if images.dtype == torch.uint8:
images = images.to(dtype=self.cnn.stem[0].weight.dtype)
images /= 255.0
# Normalize images by ImageNet color mean/std.
images = self.normalize(images)
# Extract c5 features from encoder (backbone) and return.
# shape: (B, out_channels, H / 32, W / 32)
features = self.backbone(images)["c5"]
return features
##############################################################################
# Recurrent Neural Network #
##############################################################################
def rnn_step_forward(x, prev_h, Wx, Wh, b):
"""
Run the forward pass for a single timestep of a vanilla RNN that uses a tanh
activation function.
The input data has dimension D, the hidden state has dimension H, and we use
a minibatch size of N.
Args:
x: Input data for this timestep, of shape (N, D).
prev_h: Hidden state from previous timestep, of shape (N, H)
Wx: Weight matrix for input-to-hidden connections, of shape (D, H)
Wh: Weight matrix for hidden-to-hidden connections, of shape (H, H)
b: Biases, of shape (H,)
Returns a tuple of:
next_h: Next hidden state, of shape (N, H)
cache: Tuple of values needed for the backward pass.
"""
next_h, cache = None, None
##########################################################################
# TODO: Implement a single forward step for the vanilla RNN. Store next
# hidden state and any values you need for the backward pass in the next_h
# and cache variables respectively.
##########################################################################
# Replace "pass" statement with your code
# print(prev_h.shape, Wh.shape, x.shape, Wx.shape, b.shape)
next_h = torch.tanh(prev_h@ Wh + x@ Wx + b)
cache = (x, prev_h, Wx, Wh, b)
##########################################################################
# END OF YOUR CODE #
##########################################################################
return next_h, cache
def rnn_step_backward(dnext_h, cache):
"""
Backward pass for a single timestep of a vanilla RNN.
Args:
dnext_h: Gradient of loss with respect to next hidden state, of shape (N, H)
cache: Cache object from the forward pass
Returns a tuple of:
dx: Gradients of input data, of shape (N, D)
dprev_h: Gradients of previous hidden state, of shape (N, H)
dWx: Gradients of input-to-hidden weights, of shape (D, H)
dWh: Gradients of hidden-to-hidden weights, of shape (H, H)
db: Gradients of bias vector, of shape (H,)
"""
dx, dprev_h, dWx, dWh, db = None, None, None, None, None
##########################################################################
# TODO: Implement the backward pass for a single step of a vanilla RNN.
#
# HINT: For the tanh function, you can compute the local derivative in
# terms of the output value from tanh.
##########################################################################
# Replace "pass" statement with your code
(x, prev_h, Wx, Wh, b) = cache
dx = dnext_h * (1 - torch.tanh(prev_h@ Wh + x@ Wx + b)**2)@ Wx.T
dprev_h = dnext_h * (1 - torch.tanh(prev_h@ Wh + x@ Wx + b)**2)@ Wh.T
dWx = x.T@ (dnext_h * (1 - torch.tanh(prev_h@ Wh + x@ Wx + b)**2))
dWh = prev_h.T@ (dnext_h * (1 - torch.tanh(prev_h@ Wh + x@ Wx + b)**2))
db = torch.sum(dnext_h * (1 - torch.tanh(prev_h@ Wh + x@ Wx + b)**2), dim=0)
##########################################################################
# END OF YOUR CODE #
##########################################################################
return dx, dprev_h, dWx, dWh, db
def rnn_forward(x, h0, Wx, Wh, b):
"""
Run a vanilla RNN forward on an entire sequence of data. We assume an input
sequence composed of T vectors, each of dimension D. The RNN uses a hidden
size of H, and we work over a minibatch containing N sequences. After running
the RNN forward, we return the hidden states for all timesteps.
Args:
x: Input data for the entire timeseries, of shape (N, T, D).
h0: Initial hidden state, of shape (N, H)
Wx: Weight matrix for input-to-hidden connections, of shape (D, H)
Wh: Weight matrix for hidden-to-hidden connections, of shape (H, H)
b: Biases, of shape (H,)
Returns a tuple of:
h: Hidden states for the entire timeseries, of shape (N, T, H).
cache: Values needed in the backward pass
"""
h, cache = None, None
##########################################################################
# TODO: Implement forward pass for a vanilla RNN running on a sequence of
# input data. You should use the rnn_step_forward function that you defined
# above. You can use a for loop to help compute the forward pass.
##########################################################################
# Replace "pass" statement with your code
# x = x.transpose(0,1)
# hi=h0
# T = x.size(dim=0)
# print(T, h0.shape)
# h = h0.unsqueeze(0).repeat(T, 1, 1)
# cache = []
# for i in range(T):
# print(hi.shape, Wh.shape, x.shape, Wx.shape, b.shape)
# hi, cachei = rnn_step_forward(x[i],hi,Wx,Wh,b)
# h[i]= hi
# cache.append(cachei)
# h = h.transpose(0,1)
N= x.shape[0]
T= x.shape[1]
H = h0.shape[1]
h = torch.zeros(N, T, H, dtype=x.dtype, device=x.device)
cache = []
prev_h = h0
for t in range(T):
xt = x[:, t, :]
next_h, cache_t = rnn_step_forward(xt, prev_h, Wx, Wh, b)
h[:, t, :] = next_h
cache.append(cache_t)
prev_h = next_h
##########################################################################
# END OF YOUR CODE #
##########################################################################
return h, cache
def rnn_backward(dh, cache):
"""
Compute the backward pass for a vanilla RNN over an entire sequence of data.
Args:
dh: Upstream gradients of all hidden states, of shape (N, T, H).
NOTE: 'dh' contains the upstream gradients produced by the
individual loss functions at each timestep, *not* the gradients
being passed between timesteps (which you'll have to compute yourself
by calling rnn_step_backward in a loop).
Returns a tuple of:
dx: Gradient of inputs, of shape (N, T, D)
dh0: Gradient of initial hidden state, of shape (N, H)
dWx: Gradient of input-to-hidden weights, of shape (D, H)
dWh: Gradient of hidden-to-hidden weights, of shape (H, H)
db: Gradient of biases, of shape (H,)
"""
dx, dh0, dWx, dWh, db = None, None, None, None, None
##########################################################################
# TODO: Implement the backward pass for a vanilla RNN running an entire
# sequence of data. You should use the rnn_step_backward function that you
# defined above. You can use a for loop to help compute the backward pass.
##########################################################################
# Replace "pass" statement with your code
dh = dh.transpose(0,1)
T = dh.size(dim=0)
x0, _, _, _, _ = cache[0]
N, D = x0.shape
H = dh.shape[2]
dx = torch.zeros(T, N, D, dtype=dh.dtype, device=dh.device)
dh0 = torch.zeros(N, H, dtype=dh.dtype, device=dh.device)
dWx = torch.zeros(D, H, dtype=dh.dtype, device=dh.device)
dWh = torch.zeros(H, H, dtype=dh.dtype, device=dh.device)
db = torch.zeros(H, dtype=dh.dtype, device=dh.device)
dprev_h = 0
for i in range(T):
dx[-(i+1)], dprev_h, dWxi, dWhi, dbi = rnn_step_backward(dh[-(i+1)] + dprev_h,cache[-(i+1)])
dWx += dWxi
dWh += dWhi
db += dbi
dh0 = dprev_h
dx = dx.transpose(0,1)
##########################################################################
# END OF YOUR CODE #
##########################################################################
return dx, dh0, dWx, dWh, db
class RNN(nn.Module):
"""
Single-layer vanilla RNN module.
You don't have to implement anything here but it is highly recommended to
read through the code as you will implement subsequent modules.
"""
def __init__(self, input_dim: int, hidden_dim: int):
"""
Initialize an RNN. Model parameters to initialize:
Wx: Weight matrix for input-to-hidden connections, of shape (D, H)
Wh: Weight matrix for hidden-to-hidden connections, of shape (H, H)
b: Biases, of shape (H,)
Args:
input_dim: Input size, denoted as D before
hidden_dim: Hidden size, denoted as H before
"""
super().__init__()
# Register parameters
self.Wx = nn.Parameter(
torch.randn(input_dim, hidden_dim).div(math.sqrt(input_dim))
)
self.Wh = nn.Parameter(
torch.randn(hidden_dim, hidden_dim).div(math.sqrt(hidden_dim))
)
self.b = nn.Parameter(torch.zeros(hidden_dim))
def forward(self, x, h0):
"""
Args:
x: Input data for the entire timeseries, of shape (N, T, D)
h0: Initial hidden state, of shape (N, H)
Returns:
hn: The hidden state output
"""
hn, _ = rnn_forward(x, h0, self.Wx, self.Wh, self.b)
return hn
def step_forward(self, x, prev_h):
"""
Args:
x: Input data for one time step, of shape (N, D)
prev_h: The previous hidden state, of shape (N, H)
Returns:
next_h: The next hidden state, of shape (N, H)
"""
next_h, _ = rnn_step_forward(x, prev_h, self.Wx, self.Wh, self.b)
return next_h
class WordEmbedding(nn.Module):
"""
Simplified version of torch.nn.Embedding.
We operate on minibatches of size N where
each sequence has length T. We assume a vocabulary of V words, assigning each
word to a vector of dimension D.
Args:
x: Integer array of shape (N, T) giving indices of words. Each element idx
of x muxt be in the range 0 <= idx < V.
Returns a tuple of:
out: Array of shape (N, T, D) giving word vectors for all input words.
"""
def __init__(self, vocab_size: int, embed_size: int):
super().__init__()
# Register parameters
self.W_embed = nn.Parameter(
torch.randn(vocab_size, embed_size).div(math.sqrt(vocab_size))
)
def forward(self, x):
out = None
######################################################################
# TODO: Implement the forward pass for word embeddings.
######################################################################
# Replace "pass" statement with your code
# N,T = x.shape
# dummy_out = self.W_embed(torch.randn(1))
# D = dummy_out.shape
# out = torch.zeros((N, T, D))
# for i in range(N):
# for j in range(T):
# out[i,j,:] = self.W_embed(x[i,j])
out = self.W_embed[x]
######################################################################
# END OF YOUR CODE #
######################################################################
return out
def temporal_softmax_loss(x, y, ignore_index=None):
"""
A temporal version of softmax loss for use in RNNs. We assume that we are
making predictions over a vocabulary of size V for each timestep of a
timeseries of length T, over a minibatch of size N. The input x gives scores
for all vocabulary elements at all timesteps, and y gives the indices of the
ground-truth element at each timestep. We use a cross-entropy loss at each
timestep, *summing* the loss over all timesteps and *averaging* across the
minibatch.
As an additional complication, we may want to ignore the model output at some
timesteps, since sequences of different length may have been combined into a
minibatch and padded with NULL tokens. The optional ignore_index argument
tells us which elements in the caption should not contribute to the loss.
Args:
x: Input scores, of shape (N, T, V)
y: Ground-truth indices, of shape (N, T) where each element is in the
range 0 <= y[i, t] < V
Returns a tuple of:
loss: Scalar giving loss
"""
loss = None
##########################################################################
# TODO: Implement the temporal softmax loss function.
#
# REQUIREMENT: This part MUST be done in one single line of code!
#
# HINT: Look up the function torch.functional.cross_entropy, set
# ignore_index to the variable ignore_index (i.e., index of NULL) and
# set reduction to either 'sum' or 'mean' (avoid using 'none' for now).
#
# We use a cross-entropy loss at each timestep, *summing* the loss over
# all timesteps and *averaging* across the minibatch.
##########################################################################
# Replace "pass" statement with your code
loss = F.cross_entropy(x.reshape(-1, x.size(2)), y.reshape(-1), ignore_index=ignore_index, reduction='sum')/ x.size(0)
##########################################################################
# END OF YOUR CODE #
##########################################################################
return loss
class CaptioningRNN(nn.Module):
"""
A CaptioningRNN produces captions from images using a recurrent
neural network.
The RNN receives input vectors of size D, has a vocab size of V, works on
sequences of length T, has an RNN hidden dimension of H, uses word vectors
of dimension W, and operates on minibatches of size N.
Note that we don't use any regularization for the CaptioningRNN.
You will implement the `__init__` method for model initialization and
the `forward` method first, then come back for the `sample` method later.
"""
def __init__(
self,
word_to_idx,
input_dim: int = 512,
wordvec_dim: int = 128,
hidden_dim: int = 128,
cell_type: str = "rnn",
image_encoder_pretrained: bool = True,
ignore_index: Optional[int] = None,
):
"""
Construct a new CaptioningRNN instance.
Args:
word_to_idx: A dictionary giving the vocabulary. It contains V
entries, and maps each string to a unique integer in the
range [0, V).
input_dim: Dimension D of input image feature vectors.
wordvec_dim: Dimension W of word vectors.
hidden_dim: Dimension H for the hidden state of the RNN.
cell_type: What type of RNN to use; either 'rnn' or 'lstm'.
"""
super().__init__()
if cell_type not in {"rnn", "lstm", "attn"}:
raise ValueError('Invalid cell_type "%s"' % cell_type)
self.cell_type = cell_type
self.word_to_idx = word_to_idx
self.idx_to_word = {i: w for w, i in word_to_idx.items()}
self.vocab_size = len(word_to_idx)
self._null = word_to_idx["<NULL>"]
self._start = word_to_idx.get("<START>", None)
self._end = word_to_idx.get("<END>", None)
self.ignore_index = ignore_index
######################################################################
# TODO: Initialize the image captioning module. Refer to the TODO
# in the captioning_forward function on layers you need to create
#
# You may want to check the following pre-defined classes:
# ImageEncoder, WordEmbedding, RNN, LSTM, AttentionLSTM, nn.Linear
#
# (1) output projection (from RNN hidden state to vocab probability)
# (2) feature projection (from CNN pooled feature to h0)
######################################################################
# Replace "pass" statement with your code
self.model = ImageEncoder(pretrained=True, verbose=True)
self.conv1 = torch.nn.Conv2d(400, hidden_dim,(1,1))#.to( device=device, dtype=dtype)
self.model_emb = WordEmbedding(self.vocab_size, wordvec_dim)#.to(**to_double)
self.output_projection = nn.Linear(hidden_dim, self.vocab_size)
self.feature_projection = nn.Linear(400*4*4, hidden_dim)
self.rnn_module = RNN(wordvec_dim, hidden_dim)
self.lstm_module = LSTM(wordvec_dim, hidden_dim)
self.attn_module = AttentionLSTM(wordvec_dim, hidden_dim)
######################################################################
# END OF YOUR CODE #
######################################################################
def forward(self, images, captions):
"""
Compute training-time loss for the RNN. We input images and the GT
captions for those images, and use an RNN (or LSTM) to compute loss. The
backward part will be done by torch.autograd.
Args:
images: Input images, of shape (N, 3, 112, 112)
captions: Ground-truth captions; an integer array of shape (N, T + 1)
where each element is in the range 0 <= y[i, t] < V
Returns:
loss: A scalar loss
"""
# Cut captions into two pieces: captions_in has everything but the last
# word and will be input to the RNN; captions_out has everything but the
# first word and this is what we will expect the RNN to generate. These
# are offset by one relative to each other because the RNN should produce
# word (t+1) after receiving word t. The first element of captions_in
# will be the START token, and the first element of captions_out will
# be the first word.
captions_in = captions[:, :-1]
# print(captions_in.shape)
captions_out = captions[:, 1:]
loss = 0.0
######################################################################
# TODO: Implement the forward pass for the CaptioningRNN.
# In the forward pass you will need to do the following:
# (1) Use an affine transformation to project the image feature to
# the initial hidden state $h0$ (for RNN/LSTM, of shape (N, H)) or
# the projected CNN activation input $A$ (for Attention LSTM,
# of shape (N, H, 4, 4).
# (2) Use a word embedding layer to transform the words in captions_in
# from indices to vectors, giving an array of shape (N, T, W).
# (3) Use either a vanilla RNN or LSTM (depending on self.cell_type) to
# process the sequence of input word vectors and produce hidden state
# vectors for all timesteps, producing an array of shape (N, T, H).
# (4) Use a (temporal) affine transformation to compute scores over the
# vocabulary at every timestep using the hidden states, giving an
# array of shape (N, T, V).
# (5) Use (temporal) softmax to compute loss using captions_out, ignoring
# the points where the output word is <NULL>.
#
# Do not worry about regularizing the weights or their gradients!
######################################################################
# Replace "pass" statement with your code
N = images.shape[0]
#N=10,H=40
if self.cell_type in {"rnn", "lstm"}:
h0 = self.feature_projection(self.model(images).reshape(N,-1))
elif self.cell_type in {"attn"}:
A = self.conv1(self.model(images))
captions_in_emb = self.model_emb(captions_in)
# print(captions_in_emb.shape)
if self.cell_type in {"rnn"}:
hn = self.rnn_module(captions_in_emb, h0)
if self.cell_type in {"lstm"}:
hn = self.lstm_module(captions_in_emb, h0)
if self.cell_type in {"attn"}:
hn = self.attn_module(captions_in_emb, A)
T = hn.shape[1]
V = self.vocab_size
device = captions_out.device
scores = torch.zeros(N, T, V).to(device)
for t in range(T):
scores[:,t,:] = self.output_projection(hn[:,t,:])
loss = temporal_softmax_loss(scores, captions_out, ignore_index=self.ignore_index)
######################################################################
# END OF YOUR CODE #
######################################################################
return loss
def sample(self, images, max_length=15):
"""
Run a test-time forward pass for the model, sampling captions for input
feature vectors.
At each timestep, we embed the current word, pass it and the previous hidden
state to the RNN to get the next hidden state, use the hidden state to get
scores for all vocab words, and choose the word with the highest score as
the next word. The initial hidden state is computed by applying an affine
transform to the image features, and the initial word is the <START>
token.
For LSTMs you will also have to keep track of the cell state; in that case
the initial cell state should be zero.
Args:
images: Input images, of shape (N, 3, 112, 112)
max_length: Maximum length T of generated captions
Returns:
captions: Array of shape (N, max_length) giving sampled captions,
where each element is an integer in the range [0, V). The first
element of captions should be the first sampled word, not the
<START> token.
"""
N = images.shape[0]
captions = self._null * images.new(N, max_length).fill_(1).long()
#captions.shape=(N,max_length)
if self.cell_type == "attn":
attn_weights_all = images.new(N, max_length, 4, 4).fill_(0).float()
######################################################################
# TODO: Implement test-time sampling for the model. You will need to
# initialize the hidden state of the RNN by applying the learned affine
# transform to the image features. The first word that you feed to
# the RNN should be the <START> token; its value is stored in the
# variable self._start. At each timestep you will need to do to:
# (1) Embed the previous word using the learned word embeddings
# (2) Make an RNN step using the previous hidden state and the embedded
# current word to get the next hidden state.
# (3) Apply the learned affine transformation to the next hidden state to
# get scores for all words in the vocabulary
# (4) Select the word with the highest score as the next word, writing it
# (the word index) to the appropriate slot in the captions variable
#
# For simplicity, you do not need to stop generating after an <END> token
# is sampled, but you can if you want to.
#
# NOTE: we are still working over minibatches in this function. Also if
# you are using an LSTM, initialize the first cell state to zeros.
# For AttentionLSTM, first project the 1280x4x4 CNN feature activation
# to $A$ of shape Hx4x4. The LSTM initial hidden state and cell state
# would both be A.mean(dim=(2, 3)).
#######################################################################
# Replace "pass" statement with your code
if self.cell_type == "rnn":
hn = self.feature_projection(self.model(images).reshape(N, -1))
if self.cell_type == "lstm":
hn = self.feature_projection(self.model(images).reshape(N, -1))
cn = torch.zeros_like(hn)
if self.cell_type == "attn":
A = self.conv1(self.model(images))
hn = A.mean(dim=(2, 3))
cn = A.mean(dim=(2, 3))
captions[:, 0] = self._start
caption_emb = self.model_emb(captions[:, 0])
#caption_emb.shape = (N,D)
if self.cell_type in {"rnn"}:
caption_emb = caption_emb.unsqueeze(-1).transpose(1,2)
#captions_emb.shape = (N,1,D)
hn = self.rnn_module(caption_emb, hn)
scores = self.output_projection(hn)
captions[:, 1] = scores[:,0,:].argmax(dim=1)
elif self.cell_type in {"lstm"}:
hn, cn = self.lstm_module.step_forward(caption_emb, hn, cn)
scores = self.output_projection(hn)
captions[:, 1] = scores.argmax(dim=1)
elif self.cell_type in {"attn"}:
attn, attn_weights = dot_product_attention(hn,A)
attn_weights_all[:,1] = attn_weights
hn, cn = self.attn_module.step_forward(caption_emb, hn, cn, attn)
scores = self.output_projection(hn)
captions[:, 1] = scores.argmax(dim=1)
for t in range(2, max_length):
captions_emb = self.model_emb(captions[:, t - 1])
if self.cell_type == "rnn":
captions_emb = captions_emb.unsqueeze(-1).transpose(1,2)
hn = self.rnn_module(captions_emb, hn[:,0,:])
scores = self.output_projection(hn)
captions[:, t] = scores[:,0,:].argmax(dim=1)
elif self.cell_type == "lstm":
hn, cn = self.lstm_module.step_forward(captions_emb, hn, cn)
scores = self.output_projection(hn)
captions[:, t] = scores.argmax(dim=1)
elif self.cell_type == "attn":
attn, attn_weights = dot_product_attention(hn,A)
attn_weights_all[:,t] = attn_weights
hn, cn = self.attn_module.step_forward(caption_emb, hn, cn, attn)
scores = self.output_projection(hn)
captions[:, t] = scores.argmax(dim=1)
######################################################################
# END OF YOUR CODE #
######################################################################
if self.cell_type == "attn":
return captions, attn_weights_all.cpu()
else:
return captions
class LSTM(nn.Module):
"""Single-layer, uni-directional LSTM module."""
def __init__(self, input_dim: int, hidden_dim: int):
"""
Initialize a LSTM. Model parameters to initialize:
Wx: Weights for input-to-hidden connections, of shape (D, 4H)
Wh: Weights for hidden-to-hidden connections, of shape (H, 4H)
b: Biases, of shape (4H,)
Args:
input_dim: Input size, denoted as D before
hidden_dim: Hidden size, denoted as H before
"""
super().__init__()
# Register parameters
self.Wx = nn.Parameter(
torch.randn(input_dim, hidden_dim * 4).div(math.sqrt(input_dim))
)
self.Wh = nn.Parameter(
torch.randn(hidden_dim, hidden_dim * 4).div(math.sqrt(hidden_dim))
)
self.b = nn.Parameter(torch.zeros(hidden_dim * 4))
def step_forward(
self, x: torch.Tensor, prev_h: torch.Tensor, prev_c: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Forward pass for a single timestep of an LSTM.
The input data has dimension D, the hidden state has dimension H, and
we use a minibatch size of N.
Args:
x: Input data for one time step, of shape (N, D)
prev_h: The previous hidden state, of shape (N, H)
prev_c: The previous cell state, of shape (N, H)
Wx: Input-to-hidden weights, of shape (D, 4H)
Wh: Hidden-to-hidden weights, of shape (H, 4H)
b: Biases, of shape (4H,)
Returns:
Tuple[torch.Tensor, torch.Tensor]
next_h: Next hidden state, of shape (N, H)
next_c: Next cell state, of shape (N, H)
"""
######################################################################
# TODO: Implement the forward pass for a single timestep of an LSTM.
######################################################################
next_h, next_c = None, None
H = prev_h.shape[1]
A = x @ self.Wx + prev_h @ self.Wh + self.b
# print(A[:,:H].shape)
m = nn.Sigmoid()
t = nn.Tanh()
i = m(A[:,:H])
f = m(A[:,H:2*H])
o = m(A[:,2*H:3*H])
g = t(A[:,3*H:])
next_c = f*prev_c + i*g
next_h = o * t(next_c)
######################################################################
# END OF YOUR CODE #
######################################################################
return next_h, next_c
def forward(self, x: torch.Tensor, h0: torch.Tensor) -> torch.Tensor:
"""
Forward pass for an LSTM over an entire sequence of data. We assume an
input sequence composed of T vectors, each of dimension D. The LSTM
uses a hidden size of H, and we work over a minibatch containing N
sequences. After running the LSTM forward, we return the hidden states
for all timesteps.
Note that the initial cell state is passed as input, but the initial
cell state is set to zero. Also note that the cell state is not returned;
it is an internal variable to the LSTM and is not accessed from outside.
Args:
x: Input data for the entire timeseries, of shape (N, T, D)
h0: Initial hidden state, of shape (N, H)
Returns:
hn: The hidden state output.
"""
c0 = torch.zeros_like(
h0
) # we provide the intial cell state c0 here for you!
######################################################################
# TODO: Implement the forward pass for an LSTM over entire timeseries
######################################################################
hn = None
N= x.shape[0]
T= x.shape[1]
H = h0.shape[1]
hn = torch.zeros(N, T, H, dtype=x.dtype, device=x.device)
prev_h = h0
prev_c = c0
for t in range(T):
xt = x[:, t, :]
next_h, next_c = self.step_forward(xt, prev_h, prev_c)
hn[:, t, :] = next_h
prev_h = next_h
prev_c = next_c
######################################################################
# END OF YOUR CODE #
######################################################################
return hn
def dot_product_attention(prev_h, A):
"""
A simple scaled dot-product attention layer.
Args:
prev_h: The LSTM hidden state from previous time step, of shape (N, H)
A: **Projected** CNN feature activation, of shape (N, H, 4, 4),
where H is the LSTM hidden state size
Returns:
attn: Attention embedding output, of shape (N, H)
attn_weights: Attention weights, of shape (N, 4, 4)
"""
N, H, D_a, _ = A.shape
attn, attn_weights = None, None
##########################################################################
# TODO: Implement the scaled dot-product attention we described earlier. #
# You will use this function for `AttentionLSTM` forward and sample #
# functions. HINT: Make sure you reshape attn_weights back to (N, 4, 4)! #
##########################################################################
# Replace "pass" statement with your code
A_tilt = A.reshape(N,H,16)
M_tilt = (torch.bmm(A_tilt.transpose(1, 2), prev_h.unsqueeze(2)) / torch.sqrt(torch.tensor(H, dtype=torch.float32))).squeeze(2)
M_norm = F.softmax(M_tilt, dim=1)
M_norm_reshaped = M_norm.unsqueeze(1)
attn = torch.bmm(A_tilt, M_norm_reshaped.transpose(1, 2)).squeeze(2)
attn_weights = M_norm.reshape(N,4,4)
return attn, attn_weights
##########################################################################
# END OF YOUR CODE #
##########################################################################
return attn, attn_weights
class AttentionLSTM(nn.Module):
"""
This is our single-layer, uni-directional Attention module.
Args:
input_dim: Input size, denoted as D before
hidden_dim: Hidden size, denoted as H before
"""
def __init__(self, input_dim: int, hidden_dim: int):
"""
Initialize a LSTM. Model parameters to initialize:
Wx: Weights for input-to-hidden connections, of shape (D, 4H)
Wh: Weights for hidden-to-hidden connections, of shape (H, 4H)
Wattn: Weights for attention-to-hidden connections, of shape (H, 4H)
b: Biases, of shape (4H,)
"""
super().__init__()
# Register parameters
self.Wx = nn.Parameter(
torch.randn(input_dim, hidden_dim * 4).div(math.sqrt(input_dim))
)
self.Wh = nn.Parameter(
torch.randn(hidden_dim, hidden_dim * 4).div(math.sqrt(hidden_dim))
)
self.Wattn = nn.Parameter(
torch.randn(hidden_dim, hidden_dim * 4).div(math.sqrt(hidden_dim))
)
self.b = nn.Parameter(torch.zeros(hidden_dim * 4))
def step_forward(
self,
x: torch.Tensor,
prev_h: torch.Tensor,
prev_c: torch.Tensor,
attn: torch.Tensor,
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Args:
x: Input data for one time step, of shape (N, D)
prev_h: The previous hidden state, of shape (N, H)
prev_c: The previous cell state, of shape (N, H)
attn: The attention embedding, of shape (N, H)
Returns:
next_h: The next hidden state, of shape (N, H)
next_c: The next cell state, of shape (N, H)
"""
#######################################################################
# TODO: Implement forward pass for a single timestep of attention LSTM.
# Feel free to re-use some of your code from `LSTM.step_forward()`.
#######################################################################
next_h, next_c = None, None
H = prev_h.shape[1]
A = x @ self.Wx + prev_h @ self.Wh + attn @ self.Wattn +self.b
m = nn.Sigmoid()
t = nn.Tanh()
i = m(A[:,:H])
f = m(A[:,H:2*H])
o = m(A[:,2*H:3*H])
g = t(A[:,3*H:])
next_c = f*prev_c + i*g
next_h = o * t(next_c)
######################################################################
# END OF YOUR CODE #
######################################################################
return next_h, next_c
def forward(self, x: torch.Tensor, A: torch.Tensor):
"""
Forward pass for an LSTM over an entire sequence of data. We assume an
input sequence composed of T vectors, each of dimension D. The LSTM uses
a hidden size of H, and we work over a minibatch containing N sequences.
After running the LSTM forward, we return hidden states for all timesteps.
Note that the initial cell state is passed as input, but the initial cell
state is set to zero. Also note that the cell state is not returned; it
is an internal variable to the LSTM and is not accessed from outside.
h0 and c0 are same initialized as the global image feature (meanpooled A)
For simplicity, we implement scaled dot-product attention, which means in
Eq. 4 of the paper (https://arxiv.org/pdf/1502.03044.pdf),
f_{att}(a_i, h_{t-1}) equals to the scaled dot product of a_i and h_{t-1}.
Args:
x: Input data for the entire timeseries, of shape (N, T, D)
A: The projected CNN feature activation, of shape (N, H, 4, 4)
Returns:
hn: The hidden state output
"""
# The initial hidden state h0 and cell state c0 are initialized
# differently in AttentionLSTM from the original LSTM and hence
# we provided them for you.
h0 = A.mean(dim=(2, 3)) # Initial hidden state, of shape (N, H)
c0 = h0 # Initial cell state, of shape (N, H)
######################################################################
# TODO: Implement the forward pass for an LSTM over an entire time- #
# series. You should use the `dot_product_attention` function that #
# is defined outside this module. #
######################################################################
hn = None
N= x.shape[0]
T= x.shape[1]
H = h0.shape[1]
hn = torch.zeros(N, T, H, dtype=x.dtype, device=x.device)
prev_h = h0
prev_c = c0
for t in range(T):
xt = x[:, t, :]
attn, _ = dot_product_attention(prev_h, A)
next_h, next_c = self.step_forward(xt, prev_h, prev_c, attn)
hn[:, t, :] = next_h
prev_h = next_h
prev_c = next_c
######################################################################
# END OF YOUR CODE #
######################################################################
return hn