-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transformer.R
208 lines (190 loc) · 7.96 KB
/
Transformer.R
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
Transformer <- torch::nn_module(
name='Transformer',
initialize = function(catFeatures, numFeatures=0, numBlocks, dimToken, dimOut=1,
numHeads, attDropout, ffnDropout, resDropout,
headActivation=torch::nn_relu,
activation=NULL,
ffnNorm=torch::nn_layer_norm,
headNorm=torch::nn_layer_norm,
attNorm=torch::nn_layer_norm,
dimHidden){
activation = nn_reglu
self$Categoricalembedding <- Embedding(catFeatures + 1, dimToken) # + 1 for padding idx
# self$numericalEmbedding <- numericalEmbedding(numFeatures, dimToken)
self$classToken <- ClassToken(dimToken)
self$layers <- torch::nn_module_list(lapply(1:numBlocks,
function(x) {
layer <- torch::nn_module_list()
layer$add_module('attention', torch::nn_multihead_attention(dimToken,numHeads,
dropout=attDropout,
bias=TRUE))
layer$add_module('ffn', FeedForwardBlock(dimToken, dimHidden,
biasFirst=TRUE,
biasSecond=TRUE,
dropout=ffnDropout,
activation=activation))
layer$add_module('attentionResDropout', torch::nn_dropout(resDropout))
layer$add_module('ffnResDropout', torch::nn_dropout(resDropout))
layer$add_module('ffnNorm', ffnNorm(dimToken))
if (x!=1) {
layer$add_module('attentionNorm', attNorm(dimToken))
}
return(layer)
}))
self$head <- Head(dimToken, bias=TRUE, activation=headActivation,
headNorm, dimOut)
},
forward = function(x){
mask <- torch::torch_where(x ==0, TRUE, FALSE)
input <- x
num <- NULL
cat <- self$Categoricalembedding(x)
if (!is.null(num)) {
num <- self$numericalEmbedding(input$num)
x <- torch::torch_cat(list(cat, num), dim=2L)
mask <- torch::torch_cat(list(mask, torch::torch_zeros(c(x$shape[1],
num$shape[2]),
device=mask$device,
dtype=mask$dtype)),
dim=2L)
} else {
x <- cat
}
x <- self$classToken(x)
mask <- torch::torch_cat(list(mask, torch::torch_zeros(c(x$shape[1], 1),
device=mask$device,
dtype=mask$dtype)),
dim=2L)
for (i in 1:length(self$layers)) {
layer <- self$layers[[i]]
xResidual <- self$startResidual(layer, 'attention', x)
if (i==length(self$layers)) {
dims <- xResidual$shape
# in final layer take only attention on CLS token
xResidual <- layer$attention(xResidual[,-1]$view(c(dims[1], 1, dims[3]))$transpose(1,2),
xResidual$transpose(1,2),
xResidual$transpose(1,2), mask)
attnWeights <- xResidual[[2]]
xResidual <- xResidual[[1]]
x <- x[,-1]$view(c(dims[1], 1, dims[3]))
} else {
# attention input is seq_length x batch_size x embedding_dim
xResidual <- layer$attention(xResidual$transpose(1,2),
xResidual$transpose(1,2),
xResidual$transpose(1,2),
mask,
)[[1]]
}
x <- self$endResidual(layer, 'attention', x, xResidual$transpose(1,2))
xResidual <- self$startResidual(layer, 'ffn', x)
xResidual <- layer$ffn(xResidual)
x <- self$endResidual(layer, 'ffn', x, xResidual)
}
x <- self$head(x)[,1] # remove singleton dimension
return(x)
},
startResidual = function(layer, stage, x) {
xResidual <- x
normKey <- paste0(stage, 'Norm')
if (normKey %in% names(as.list(layer))) {
xResidual <- layer[[normKey]](xResidual)
}
return(xResidual)
},
endResidual = function(layer, stage, x, xResidual) {
dropoutKey <- paste0(stage, 'ResDropout')
xResidual <-layer[[dropoutKey]](xResidual)
x <- x + xResidual
return(x)
}
)
FeedForwardBlock <- torch::nn_module(
name='FeedForwardBlock',
initialize = function(dimToken, dimHidden, biasFirst, biasSecond,
dropout, activation) {
self$linearFirst <- torch::nn_linear(dimToken, dimHidden*2, biasFirst)
self$activation <- activation()
self$dropout <- torch::nn_dropout(dropout)
self$linearSecond <- torch::nn_linear(dimHidden, dimToken, biasSecond)
},
forward = function(x) {
x <- self$linearFirst(x)
x <- self$activation(x)
x <- self$dropout(x)
x <- self$linearSecond(x)
return(x)
}
)
Head <- torch::nn_module(
name='Head',
initialize = function(dimIn, bias, activation, normalization, dimOut) {
self$normalization <- normalization(dimIn)
self$activation <- activation()
self$linear <- torch::nn_linear(dimIn,dimOut, bias)
},
forward = function(x) {
x <- x[,-1] # ?
x <- self$normalization(x)
x <- self$activation(x)
x <- self$linear(x)
return(x)
}
)
Embedding <- torch::nn_module(
name='Embedding',
initialize = function(numEmbeddings, embeddingDim) {
self$embedding <- torch::nn_embedding(numEmbeddings, embeddingDim, padding_idx = 1)
},
forward = function(x_cat) {
x <- self$embedding(x_cat + 1L) # padding idx is 1L
return(x)
}
)
numericalEmbedding <- torch::nn_module(
name='numericalEmbedding',
initialize = function(numEmbeddings, embeddingDim, bias=TRUE) {
self$weight <- torch::nn_parameter(torch::torch_empty(numEmbeddings,embeddingDim))
if (bias) {
self$bias <- torch::nn_parameter(torch::torch_empty(numEmbeddings, embeddingDim))
} else {
self$bias <- NULL
}
for (parameter in list(self$weight, self$bias)) {
if (!is.null(parameter)) {
torch::nn_init_kaiming_uniform_(parameter, a=sqrt(5))
}
}
},
forward = function(x) {
x <- self$weight$unsqueeze(1) * x$unsqueeze(-1)
if (!is.null(self$bias)) {
x <- x + self$bias$unsqueeze(1)
}
return(x)
}
)
# adds a class token embedding to embeddings
ClassToken <- torch::nn_module(
name='ClassToken',
initialize = function(dimToken) {
self$weight <- torch::nn_parameter(torch::torch_empty(dimToken,1))
torch::nn_init_kaiming_uniform_(self$weight, a=sqrt(5))
},
expand = function(dims) {
newDims <- vector("integer", length(dims) - 1) + 1
return (self$weight$view(c(newDims,-1))$expand(c(dims, -1)))
},
forward = function(x) {
return(torch::torch_cat(c(x, self$expand(c(dim(x)[[1]], 1))), dim=2))
}
)
nn_reglu <- torch::nn_module(
name='ReGlu',
forward = function(x) {
return(reglu(x))
}
)
reglu <- function(x) {
chunks <- x$chunk(2, dim=-1)
return(chunks[[1]]* torch::nnf_relu(chunks[[2]]))
}