argmax32(_:count:stride:)
Math.swift:49static func argmax32(_ ptr: UnsafePointer<Float>, count: Int, stride: Int = 1) -> (Int, Float)
static func argmax32(_ ptr: UnsafePointer<Float>, count: Int, stride: Int = 1) -> (Int, Float)
s11TensorUtils4MathV8argmax32_5count6strideSi_SftSPySfG_S2itFZ
What are these?9RDDQ
import TensorUtils
struct Math
From M.I. Hollemans
@frozen struct UnsafePointer<Pointee> where Pointee : ~Copyable
A pointer for accessing data of a specific type.
@frozen struct Float
A single-precision, floating-point value type.
@frozen struct Int
A signed integer value type.
static func argmax(_ multiArray: MLMultiArray) -> (Int, Double)
MLMultiArray helper. Works in our specific use case.
static func argmax(_ shapedArray: MLShapedArray<Float>) -> (Int, Float)
static func argmax(_ shapedArray: some MLShapedArrayProtocol) -> (Int, Float)
static func argmax(_ ptr: UnsafePointer<Double>, count: Int, stride: Int = 1) -> (Int, Double)
Returns the index and value of the largest element in the array.
static func argmax(_ ptr: UnsafePointer<Float>, count: Int, stride: Int = 1) -> (Int, Float)
Returns the index and value of the largest element in the array.
static func argmax32(_ multiArray: MLMultiArray) -> (Int, Float)
MLMultiArray helper. Works in our specific use case.
static func cumsum(_ arr: [Float]) -> [Float]
Returns the cumulative sum of the array.
static func randomNumber(probabilities: [Float]) -> Int
Multinomial sampling
static func sample(indexes: [Int], probs: [Float]) -> Int
Multinomial sampling from an array of probs. Works well with topK
static func softmax(_ x: [Float]) -> [Float]
Computes the “softmax” function over an array. Based on code from https://github.com/nikolaypavlov/MLPNeuralNet/ This is what softmax looks like in “pseudocode” (actually using Python and numpy): x -= np.max(x) exp_scores = np.exp(x) softmax = exp_scores / np.sum(exp_scores) First we shift the values of x so that the highest value in the array is 0. This ensures numerical stability with the exponents, so they don’t blow up.