Source

AI/Operations/Reduction/generators.js

import { Order } from 'blockly/javascript';
/**
 * @category AI
 * @subcategory Operations
 * @module Reduction
 */
/**
 * Computes the mean of elements across dimensions of a tf.Tensor.
 *
 * @param {Tensor} tensor  The input tensor.
 * @param {Number | Number[]} axis The dimension(s) to reduce. By default it reduces all dimensions.
 * @param {Boolean} keepDims  If true, retains reduced dimensions with size 1.
 * @returns {Tensor} tf.mean(tensor, axis, keepDims)
 */
function reduceMean(block, generator) {
    const tensor = generator.valueToCode(block, 'TENSOR_VALUES', Order.NONE) || '';
    const axis = generator.valueToCode(block, 'AXIS', Order.NONE) || '';
    const keepDims = block.getFieldValue('KEEPDIMS') || '';
    return [`tf.mean(${tensor}, ${axis ? ` ${axis}` : '0'}, ${keepDims})`, Order.VOID];
}
/**
 * Computes the sum of elements across dimensions of a tf.Tensor.
 *
 * @param {Tensor} tensor  The input tensor to compute the sum over. If the dtype is bool it will be converted to int32 and the output dtype will be int32.
 * @param {Number | Number[]} axis The dimension(s) to reduce. By default it reduces all dimensions.
 * @param {Boolean} keepDims  If true, retains reduced dimensions with size 1.
 * @returns {Tensor} tf.sum(tensor, axis, keepDims)
 */
function reduceSum(block, generator) {
    const tensor = generator.valueToCode(block, 'TENSOR_VALUES', Order.NONE) || '';
    const axis = generator.valueToCode(block, 'AXIS', Order.NONE) || '';
    const keepdims = block.getFieldValue('KEEPDIMS') || '';
    return [`tf.sum(${tensor}, ${axis ? ` ${axis}` : '0'}, ${keepdims})`, Order.VOID];
}
/**
 * Computes the product of elements across dimensions of a tf.Tensor.
 *
 * @param {Tensor} tensor The input tensor to compute the product over. If the dtype is bool it will be converted to int32 and the output dtype will be int32.
 * @param {Number | Number[]} axis The dimension(s) to reduce. By default it reduces all dimensions.
 * @returns {Tensor} tf.prod(tensor, axis)
 */
function reduceProd(block, generator) {
    const tensor = generator.valueToCode(block, 'TENSOR_VALUES', Order.NONE) || '';
    const axis = generator.valueToCode(block, 'AXIS', Order.NONE) || '';
    return [`tf.prod(${tensor}${axis ? `, ${axis}` : ''})`, Order.VOID];
}
/**
 * Computes the maximum of elements across dimensions of a tf.Tensor.
 *
 * @param {Tensor} tensor The input tensor.
 * @param {Number | Number[]} axis The dimension(s) to reduce. By default it reduces all dimensions.
 * @returns {Tensor} tf.max(tensor, axis)
 */
function maxValue(block, generator) {
    const tensor = generator.valueToCode(block, 'TENSOR', Order.NONE) || '';
    const axis = generator.valueToCode(block, 'AXIS', Order.NONE) || '';
    return [`tf.max(${tensor}${axis ? `, ${axis}` : ''})`, Order.VOID];
}
/**
 * Returns the indices of the maximum values along an axis.
 *
 * @param {Tensor} tensor The input tensor.
 * @param {Number | Number[]} axis The dimension to reduce. Defaults to 0 (outer-most dimension).
 * @returns {Tensor} tf.argMax(tensor, axis)
 */
function maxValueIndex(block, generator) {
    const tensor = generator.valueToCode(block, 'TENSOR', Order.NONE) || '';
    const axis = generator.valueToCode(block, 'AXIS', Order.NONE) || '';
    return [`tf.argMax(${tensor}${axis ? `, ${axis}` : ''})`, Order.VOID];
}
export const reductionBlockGenerator = {
    ['reduce_mean']: reduceMean,
    ['reduce_sum']: reduceSum,
    ['reduce_prod']: reduceProd,
    ['tensor_max_value']: maxValue,
    ['tensor_max_value_index']: maxValueIndex,
};
//# sourceMappingURL=generators.js.map