(input, dim = null, correction = 1, keepdim = false)
| 1436 | * @returns {Tensor[]} A tuple of (std, mean) tensors. |
| 1437 | */ |
| 1438 | export function std_mean(input, dim = null, correction = 1, keepdim = false) { |
| 1439 | const inputData = /** @type {Float32Array} */ (input.data); |
| 1440 | const inputDims = input.dims; |
| 1441 | |
| 1442 | if (dim === null) { |
| 1443 | // None to reduce over all dimensions. |
| 1444 | const sum = inputData.reduce((a, b) => a + b, 0); |
| 1445 | const mean = sum / inputData.length; |
| 1446 | const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean) ** 2, 0) / (inputData.length - correction)); |
| 1447 | |
| 1448 | const meanTensor = new Tensor( |
| 1449 | input.type, |
| 1450 | [mean], |
| 1451 | [ |
| 1452 | /* scalar */ |
| 1453 | ], |
| 1454 | ); |
| 1455 | const stdTensor = new Tensor( |
| 1456 | input.type, |
| 1457 | [std], |
| 1458 | [ |
| 1459 | /* scalar */ |
| 1460 | ], |
| 1461 | ); |
| 1462 | |
| 1463 | return [stdTensor, meanTensor]; |
| 1464 | } |
| 1465 | dim = safeIndex(dim, inputDims.length); |
| 1466 | const meanTensor = mean(input, dim, keepdim); |
| 1467 | const meanTensorData = meanTensor.data; |
| 1468 | |
| 1469 | // Compute squared sum |
| 1470 | const [type, result, resultDims] = reduce_helper( |
| 1471 | (a, b, i, j) => a + (b - meanTensorData[j]) ** 2, |
| 1472 | input, |
| 1473 | dim, |
| 1474 | keepdim, |
| 1475 | ); |
| 1476 | |
| 1477 | // Square root of the squared sum |
| 1478 | for (let i = 0; i < result.length; ++i) { |
| 1479 | result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); |
| 1480 | } |
| 1481 | |
| 1482 | const stdTensor = new Tensor(type, result, resultDims); |
| 1483 | |
| 1484 | return [stdTensor, meanTensor]; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * Returns the mean value of each row of the input tensor in the given dimension dim. |
no test coverage detected