Contents

BNNSClipByValue(_:_:_:_:)

Clips a tensor’s values to the specified minimum and maximum values.

Declaration

func BNNSClipByValue(_ dest: UnsafeMutablePointer<BNNSNDArrayDescriptor>, _ src: UnsafePointer<BNNSNDArrayDescriptor>, _ min_val: Float, _ max_val: Float) -> Int32

Parameters

  • dest:

    The descriptor of the output.

  • src:

    The descriptor of the input.

  • min_val:

    The minimum clipping value.

  • max_val:

    The maximum clipping value.

Discussion

Use this function to clip the values in an input tensor to the range you specify. 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 inputData = UnsafeMutableBufferPointer<Float>.allocate(capacity: 8)
    _ = inputData.initialize(from: [1, 2, 3, 4, 5, 6, 7, 8])
    var inputDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                                layout: BNNSDataLayoutVector,
                                                size: (8, 0, 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: 8)
    var outputDescriptor = BNNSNDArrayDescriptor(flags: BNNSNDArrayFlags(0),
                                                 layout: BNNSDataLayoutVector,
                                                 size: (8, 0, 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)
    
    BNNSClipByValue(&outputDescriptor,
                     &inputDescriptor,
                     3, 6)
    
    // Prints: `[3.0, 3.0, 3.0, 4.0, 5.0, 6.0, 6.0, 6.0]`
    print(Array(outputData))
    
    inputData.deallocate()
    outputData.deallocate()
}

See Also

Utility functions