22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

36 scores = dot_products / np.sqrt(self.d_k)

37 return scores

38

39 def attn(self, query, mask=None):

40 # Query is batch-first: N, L, D

41 # Score function will generate scores for each head

42 scores = self.score_function(query) # N, n_heads, L, L

43 if mask is not None:

44 scores = scores.masked_fill(mask == 0, -1e9)

45 alphas = F.softmax(scores, dim=-1) # N, n_heads, L, L

46 alphas = self.dropout(alphas)

47 self.alphas = alphas.detach()

48

49 # N, n_heads, L, L x N, n_heads, L, d_k

50 # -> N, n_heads, L, d_k

51 context = torch.matmul(alphas, self.proj_value)

52 return context

53

54 def output_function(self, contexts):

55 # N, L, D

56 out = self.linear_out(contexts) # N, L, D

57 return out

58

59 def forward(self, query, mask=None):

60 if mask is not None:

61 # N, 1, L, L - every head uses the same mask

62 mask = mask.unsqueeze(1)

63

64 # N, n_heads, L, d_k

65 context = self.attn(query, mask=mask)

66 # N, L, n_heads, d_k

67 context = context.transpose(1, 2).contiguous()

68 # N, L, n_heads * d_k = N, L, d_model

69 context = context.view(query.size(0), -1, self.d_model)

70 # N, L, d_model

71 out = self.output_function(context)

72 return out

874 | Chapter 10: Transform and Roll Out

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!