-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathactivations.ts
338 lines (317 loc) · 8.36 KB
/
activations.ts
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
/**
* @license
* Copyright 2018 Google LLC
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
// Layer activation functions
import * as tfc from '@tensorflow/tfjs-core';
import {serialization, Tensor, tidy} from '@tensorflow/tfjs-core';
import * as K from './backend/tfjs_backend';
import {ActivationIdentifier} from './keras_format/activation_config';
import {deserializeKerasObject} from './utils/generic_utils';
/**
* Base class for Activations.
*
* Special note: due to cross-language compatibility reasons, the
* static readonly className field in this family of classes must be set to
* the initialLowerCamelCase name of the activation.
*/
export abstract class Activation extends serialization.Serializable {
abstract apply(tensor: Tensor, axis?: number): Tensor;
getConfig(): serialization.ConfigDict {
return {};
}
}
/**
* Exponential linear unit (ELU).
* Reference: https://arxiv.org/abs/1511.07289
*/
export class Elu extends Activation {
/** @nocollapse */
static readonly className = 'elu';
/**
* Calculate the activation function.
*
* @param x: Input.
* @param alpha: Scaling factor the negative section.
* @return Output of the ELU activation.
*/
apply(x: Tensor, alpha = 1): Tensor {
return K.elu(x, alpha);
}
}
serialization.registerClass(Elu);
/**
* Scaled Exponential Linear Unit. (Klambauer et al., 2017).
* Reference: Self-Normalizing Neural Networks, https://arxiv.org/abs/1706.02515
* Notes:
* - To be used together with the initialization "lecunNormal".
* - To be used together with the dropout variant "AlphaDropout".
*/
export class Selu extends Activation {
/** @nocollapse */
static readonly className = 'selu';
apply(x: Tensor): Tensor {
return tfc.selu(x);
}
}
serialization.registerClass(Selu);
/**
* Rectified linear unit
*/
export class Relu extends Activation {
/** @nocollapse */
static readonly className = 'relu';
apply(x: Tensor): Tensor {
return tfc.relu(x);
}
}
serialization.registerClass(Relu);
/**
* Rectified linear unit activation maxing out at 6.0.
*/
export class Relu6 extends Activation {
/** @nocollapse */
static readonly className = 'relu6';
apply(x: Tensor): Tensor {
return tidy(() => tfc.minimum(6.0, tfc.relu(x)));
}
}
serialization.registerClass(Relu6);
//* Linear activation (no-op) */
export class Linear extends Activation {
/** @nocollapse */
static readonly className = 'linear';
apply(x: Tensor): Tensor {
return x;
}
}
serialization.registerClass(Linear);
/**
* Sigmoid activation function.
*/
export class Sigmoid extends Activation {
/** @nocollapse */
static readonly className = 'sigmoid';
apply(x: Tensor): Tensor {
return tfc.sigmoid(x);
}
}
serialization.registerClass(Sigmoid);
/**
* Segment-wise linear approximation of sigmoid.
*/
export class HardSigmoid extends Activation {
/** @nocollapse */
static readonly className = 'hardSigmoid';
apply(x: Tensor): Tensor {
return K.hardSigmoid(x);
}
}
serialization.registerClass(HardSigmoid);
/**
* Softplus activation function.
*/
export class Softplus extends Activation {
/** @nocollapse */
static readonly className = 'softplus';
apply(x: Tensor): Tensor {
return tfc.softplus(x);
}
}
serialization.registerClass(Softplus);
/**
* Softsign activation function.
*/
export class Softsign extends Activation {
/** @nocollapse */
static readonly className = 'softsign';
apply(x: Tensor): Tensor {
return K.softsign(x);
}
}
serialization.registerClass(Softsign);
/**
* Hyperbolic tangent function.
*/
export class Tanh extends Activation {
/** @nocollapse */
static readonly className = 'tanh';
apply(x: Tensor): Tensor {
return tfc.tanh(x);
}
}
serialization.registerClass(Tanh);
/**
* Softmax activation function
*/
export class Softmax extends Activation {
/** @nocollapse */
static readonly className = 'softmax';
/**
* Calculate the activation function.
*
* @param x Tensor.
* @param axis Integer, axis along which the softmax normalization is applied.
* Invalid if < 2, as softmax across 1 (the batch dimension) is assumed to be
* an error.
*
* @returns a Tensor of the same shape as x
*
* @throws ValueError: In case `dim(x) < 2`.
*/
apply(x: Tensor, axis: number = (-1)): Tensor {
return tfc.softmax(x, axis);
}
}
serialization.registerClass(Softmax);
/**
* Log softmax activation function
*/
export class LogSoftmax extends Activation {
/** @nocollapse */
static readonly className = 'logSoftmax';
/**
* Calculate the activation function of log softmax:
* log( exp(x_i) / sum(exp(x)) )
*
* @param x Tensor.
* @param axis Integer, axis along which the softmax normalization is applied.
* Invalid if < 2, as softmax across 1 (the batch dimension) is assumed to be
* an error.
*
* @returns a Tensor of the same shape as x
*
* @throws ValueError: In case `dim(x) < 2`.
*/
apply(x: Tensor, axis: number = (-1)): Tensor {
return tfc.logSoftmax(x, axis);
}
}
serialization.registerClass(LogSoftmax);
/**
* Gelu activation function
*/
export class Gelu extends Activation {
/** @nocollapse */
static readonly className = 'gelu';
/**
* Calculate the activation function.
*
* @param x Tensor.
* @returns a Tensor of the same shape as x
*/
apply(x: Tensor): Tensor {
return tidy(() => {
return tfc.tidy(() => {
const sqrtTwo = Math.sqrt(2);
// Compute Φ(x) using the erf function
const cdf = tfc.mul(0.5, tfc.add(1, tfc.erf(tfc.div(x, sqrtTwo))));
// Compute GELU(x) = x * Φ(x)
return tfc.mul(x, cdf);
});
});
}
}
serialization.registerClass(Gelu);
/**
* GeluNew activation function
*/
export class GeluNew extends Activation {
/** @nocollapse */
static readonly className = 'gelu_new';
/**
* Calculate the activation function.
*
* @param x Tensor.
* @returns a Tensor of the same shape as x
*/
apply(x: Tensor): Tensor {
return tidy(() => {
return tfc.mul(
0.5,
tfc.mul(
x,
tfc.add(
1,
tfc.tanh(
tfc.mul(
tfc.sqrt(tfc.div(2, Math.PI)),
tfc.add(x, tfc.mul(0.044715, tfc.pow(x, 3)))
)
)
)
)
);
});
}
}
serialization.registerClass(GeluNew);
/**
* Mish activation function
*/
export class Mish extends Activation {
/** @nocollapse */
static readonly className = 'mish';
/**
* Calculate the activation function.
*
* @param x Tensor.
* @returns a Tensor of the same shape as x
*/
apply(x: Tensor): Tensor {
return tidy(() => tfc.mul(x, tfc.tanh(tfc.softplus(x))));
}
}
serialization.registerClass(Mish);
/**
* Swish activation function
*/
export class Swish extends Activation {
/** @nocollapse */
static readonly className = 'swish';
/**
* Calculate the activation function.
*
* @param x Tensor.
* @param alpha Scaling factor for the sigmoid function.
* @returns a Tensor of the same shape as x
*/
apply(x: Tensor, alpha = 1): Tensor {
return tidy(() => tfc.mul(tfc.sigmoid(tfc.mul(x, alpha)), x));
}
}
serialization.registerClass(Swish);
export function serializeActivation(activation: Activation): string {
return activation.getClassName();
}
export function deserializeActivation(
config: serialization.ConfigDict,
customObjects: serialization.ConfigDict = {}): Activation {
return deserializeKerasObject(
config, serialization.SerializationMap.getMap().classNameMap,
customObjects, 'activation');
}
export function getActivation(identifier: ActivationIdentifier|
serialization.ConfigDict|Activation): Activation {
if (identifier == null) {
const config: serialization.ConfigDict = {};
config['className'] = 'linear';
config['config'] = {};
return deserializeActivation(config);
}
if (typeof identifier === 'string') {
const config: serialization.ConfigDict = {};
config['className'] = identifier;
config['config'] = {};
return deserializeActivation(config);
} else if (identifier instanceof Activation) {
return identifier;
} else {
return deserializeActivation(identifier);
}
}