* Performs `L_p` normalization of inputs over specified dimension. Operates in place. * @param {number} [p=2] The exponent value in the norm formulation * @param {number} [dim=1] The dimension to reduce * @returns {Tensor} `this` for operation chaining.
(p = 2.0, dim = 1)
| 547 | * @returns {Tensor} `this` for operation chaining. |
| 548 | */ |
| 549 | normalize_(p = 2.0, dim = 1) { |
| 550 | dim = safeIndex(dim, this.dims.length); |
| 551 | |
| 552 | const norm = this.norm(p, dim, true); |
| 553 | |
| 554 | const this_data = this.data; |
| 555 | const norm_data = norm.data; |
| 556 | for (let i = 0; i < this_data.length; ++i) { |
| 557 | // Calculate the index in the resulting array |
| 558 | let resultIndex = 0; |
| 559 | |
| 560 | for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { |
| 561 | const size = this.dims[j]; |
| 562 | if (j !== dim) { |
| 563 | const index = num % size; |
| 564 | resultIndex += index * resultMultiplier; |
| 565 | resultMultiplier *= this.dims[j]; |
| 566 | } |
| 567 | num = Math.floor(num / size); |
| 568 | } |
| 569 | |
| 570 | // Divide by normalized value |
| 571 | this_data[i] /= norm_data[resultIndex]; |
| 572 | } |
| 573 | |
| 574 | return this; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Performs `L_p` normalization of inputs over specified dimension. |