MCPcopy Create free account
hub / github.com/ciphermodelabs/ciphercore / onehot

Function onehot

ciphercore-base/src/ops/utils.rs:329–344  ·  view source on GitHub ↗

One-hot encoding Inputs: val: val is the value to be encoded. It has shape [arbitrary_shape, N] Where N is the number of bits after A2B. max_val: max_val is the maximum value that can be encoded. max_val indicates the number of bits in the output. The output will have shape [arbitrary_shape, max_val]. Obviously, max_val must be less than 2^N. ids: ids is the precomputed array of values in the rang

(val: Node, max_val: usize, ids: Node)

Source from the content-addressed store, hash-verified

327// let ids = ones.cum_sum(0)?.subtract(ones)?.a2b()?;
328// ```
329pub fn onehot(val: Node, max_val: usize, ids: Node) -> Result<Node> {
330 let ids = ids.get_slice(vec![SliceElement::SubArray(
331 Some(0),
332 Some(max_val as i64),
333 None,
334 )])?;
335 let g = val.get_graph();
336 // We need to calculate val == ids , but we need to compare bitwise
337 // XOR(val_bits, Not(ids_bits)) is equivalent to val_bits == ids_bits
338 // XOR is equivalent to addition modulo 2, and Not is equivalent to adding 1 modulo 2
339 let bitwise_comparision = unsqueeze(val, -2)?.add(ids.add(constant_scalar(&g, 1, BIT)?)?)?;
340 // val == ids only if bitwise_comparision is all ones
341 // So we can reduce over the last dimension to get the result
342 let res = reduce_mul(bitwise_comparision)?;
343 Ok(res)
344}
345
346/// Reduces an array over the first dimension, using log number of `combine` calls.
347/// `combine` receives two arrays (first, second) of same shape, and needs to return one array of

Callers

nothing calls this directly

Calls 6

unsqueezeFunction · 0.85
constant_scalarFunction · 0.85
reduce_mulFunction · 0.85
get_sliceMethod · 0.45
get_graphMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected