MCPcopy Create free account
hub / github.com/tensorflow/tfjs / constructor

Method constructor

tfjs-backend-webgl/src/lrn_gpu.ts:25–66  ·  view source on GitHub ↗
(
      xShape: number[], radius: number, bias: number, alpha: number,
      beta: number)

Source from the content-addressed store, hash-verified

23 userCode: string;
24
25 constructor(
26 xShape: number[], radius: number, bias: number, alpha: number,
27 beta: number) {
28 const rad = radius;
29 const maxD = xShape[3] - 1;
30 this.outputShape = xShape;
31
32 // optimize pow(bias + alpha * sum, -beta)
33 // src: https://github.com/tensorflow/tensorflow/..
34 // blob/26033a1644a9c4a5fbe3170ab2e864b6a4ccd4ca/..
35 // tensorflow/core/kernels/mkl_lrn_op.cc#L320
36 let powOperator;
37 const basis = `float(${bias}) + float(${alpha}) * sum`;
38 if (beta === 0.5) {
39 powOperator = `inversesqrt(${basis})`;
40 } else if (beta === 1.0) {
41 powOperator = `1.0/(${basis})`;
42 } else {
43 powOperator = `exp(log(${basis}) * float(-${beta}));`;
44 }
45
46 this.userCode = `
47 void main() {
48 ivec4 coords = getOutputCoords();
49 int b = coords[0];
50 int r = coords[1];
51 int c = coords[2];
52 int d = coords[3];
53 float x = getX(b, r, c, d);
54 float sum = 0.0;
55 for (int j = -${rad}; j <= ${rad}; j++) {
56 int idx = d + j;
57 if (idx >= 0 && idx <= ${maxD}) {
58 float z = getX(b, r, c, idx);
59 sum += z * z;
60 }
61 }
62 float val = x * ${powOperator};
63 setOutput(val);
64 }
65 `;
66 }
67}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected