-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
69 lines (55 loc) · 1.9 KB
/
Copy pathutils.ts
File metadata and controls
69 lines (55 loc) · 1.9 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
import * as tf from '@tensorflow/tfjs-node';
export function getRandomInteger(min, max) {
return Math.floor((max - min) * Math.random()) + min;
}
export function getRandomIntegers(min, max, numIntegers) {
const output = [];
for (let i = 0; i < numIntegers; ++i) {
output.push(Math.floor((max - min) * Math.random()) + min);
}
return output;
}
export function assertPositiveInteger(x, name) {
if (!Number.isInteger(x)) {
throw new Error(
`Expected ${name} to be an integer, but received ${x}`);
}
if (!(x > 0)) {
throw new Error(
`Expected ${name} to be a positive number, but received ${x}`);
}
// You can change this for more or less smoothing
}
export function updateEmaLoss(loss, ema, smoothingFactor) {
// If this is the first loss, initialize EMA with the first loss
if (ema === 0) {
ema = loss;
} else {
ema = smoothingFactor * loss + (1 - smoothingFactor) * ema;
}
return ema;
}
export function clipGradientsByNorm(namedGrads:tf.NamedTensorMap, clipNorm) {
// Calculate the global norm of all gradients
const globalNorm = tf.tidy(() =>
tf.sqrt(
Object.values(namedGrads)
.map(g => g.square().sum())
.reduce((acc, curr) => acc.add(curr), tf.scalar(0))
)
);
// If globalNorm > clipNorm, scale all gradients
if (globalNorm.dataSync()[0] > clipNorm) {
const scale = clipNorm / globalNorm.dataSync()[0];
//console.log(`Clipped grads with scale ${scale}`)
// Scale each gradient in the NamedTensorMap
const clippedGrads = {};
for (const [name, grad] of Object.entries(namedGrads)) {
clippedGrads[name] = grad.mul(scale);
}
globalNorm.dispose(); // Dispose the global norm tensor
return clippedGrads; // Return a NamedTensorMap
}
globalNorm.dispose(); // Dispose the global norm tensor
return namedGrads; // Return the gradients as-is if no clipping is needed
}