Contents

BNNSClipByNorm(_:_:_:_:)

Clips a tensor’s values to a maximum Euclidean norm.

Declaration

func BNNSClipByNorm(_ dest: UnsafeMutablePointer<BNNSNDArrayDescriptor>, _ src: UnsafePointer<BNNSNDArrayDescriptor>, _ max_norm: Float, _ axis_flags: UInt32) -> Int32

Parameters

  • dest:

    The descriptor of the output.

  • src:

    The descriptor of the input.

  • max_norm:

    The maximum Euclidean norm.

  • axis_flags:

    The dimensions that the function uses to compute the Euclidean norm. Set to 0 for the function to compute the norm over all dimensions.

Discussion

Use this function to clip the values in an input tensor to a maximum Euclidean norm. The Euclidean norm is the square root of the sum of squares of the two tensors.

The function supports clipping across the entire tensor or by the norms of the dimension you specify.

The following code clips the values on axis 0b0010 to a Euclidean norm of 10. The Euclidean norms of [1, 2, 3] and [4, 5, 6] are both less than 10, and function returns them unchanged. However, the Euclidean norm of [7, 8, 9] is greater than 10, and the function returns them scaled accordingly.

static func clipToNorm() {
    
    let inputData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 9)
    _ = inputData.initialize(from: [1, 2, 3,
                                    4, 5, 6,
                                    7, 8, 9])
    
    var inputDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                                layout: BNNSDataLayoutRowMajorMatrix,
                                                size: (3, 3, 0, 0, 0, 0, 0, 0),
                                                stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                                data: inputData.baseAddress!,
                                                data_type: BNNSDataType.float,
                                                table_data: nil,
                                                table_data_type: BNNSDataType.float,
                                                data_scale: 1, data_bias: 0)
    
    let outputData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 9)
    var outputDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                                 layout: BNNSDataLayoutRowMajorMatrix,
                                                 size: (3, 3, 0, 0, 0, 0, 0, 0),
                                                 stride: (0, 0, 0, 0, 0, 0, 0, 0),
                                                 data: outputData.baseAddress!,
                                                 data_type: BNNSDataType.float,
                                                 table_data: nil,
                                                 table_data_type: BNNSDataType.float,
                                                 data_scale: 1, data_bias: 0)
    
    BNNSClipByNorm(&outputDescriptor,
                    &inputDescriptor,
                    10,
                    0b0010)
    
    // Prints `[1.0, 2.0, 3.0,
    //          4.0, 5.0, 6.0,
    //          5.0257072, 5.743665, 6.461623]`
    print(Array(outputData))
    
    inputData.deallocate()
    outputData.deallocate()
}

See Also

Utility functions