Contents

clip(to:input:output:)

Clips the input tensor to a closed range and writes the result to the output tensor.

Declaration

static func clip(to bounds: ClosedRange<Float>, input: BNNSNDArrayDescriptor, output: BNNSNDArrayDescriptor) throws

Parameters

  • bounds:

    The clipping values.

  • input:

    The descriptor of the input.

  • output:

    The descriptor of the output.

Discussion

Use this function to clip the values in an input tensor to a range. The function sets values below the minimum to the minimum and values above the maximum to the maximum.

The following code clips the values of the input tensor to the range 3...6:

static func clipToBounds() {
    let input = BNNSNDArrayDescriptor.allocate(
        initializingFrom: [1, 2, 3, 4, 5, 6, 7, 8] as [Float],
        shape: .vector(8))
    
    let output = BNNSNDArrayDescriptor.allocateUninitialized(
        scalarType: Float.self,
        shape: .vector(8))
    
    try? BNNS.clip(to: 3...6,
                   input: input,
                   output: output)
    
    // Prints: `[3.0, 3.0, 3.0, 4.0, 5.0, 6.0, 6.0, 6.0]`
    print(output.makeArray(of: Float.self)!)

    input.deallocate()
    output.deallocate()
}

See Also

Utility functions