--- license: mit tags: - pytorch - safetensors - threshold-logic - neuromorphic - encoding --- # threshold-onehot-decoder 4-to-2 one-hot decoder. Converts a 4-bit one-hot representation to a 2-bit binary value. ## Function onehot_decode(y3, y2, y1, y0) -> (a1, a0) Input must have exactly one bit set. ## Truth Table | y3 | y2 | y1 | y0 | a1 | a0 | Value | |----|----|----|----|----|-----|-------| | 0 | 0 | 0 | 1 | 0 | 0 | 0 | | 0 | 0 | 1 | 0 | 0 | 1 | 1 | | 0 | 1 | 0 | 0 | 1 | 0 | 2 | | 1 | 0 | 0 | 0 | 1 | 1 | 3 | ## Architecture Single-layer implementation: ``` y3 y2 y1 y0 │ │ │ │ └────┴────┴────┘ │ ┌────┴────┐ │ │ ▼ ▼ ┌───┐ ┌───┐ │a1 │ │a0 │ Layer 1 │OR │ │OR │ └───┘ └───┘ │ │ ▼ ▼ ``` Each output is a single OR gate: - a0 = OR(y1, y3): w=[1,0,1,0], b=-1 - a1 = OR(y2, y3): w=[1,1,0,0], b=-1 The decoder recognizes that: - Bit 0 of position = 1 for positions 1 and 3 - Bit 1 of position = 1 for positions 2 and 3 ## Parameters | | | |---|---| | Inputs | 4 | | Outputs | 2 | | Neurons | 2 | | Layers | 1 | | Parameters | 10 | | Magnitude | 6 | ## Usage ```python from safetensors.torch import load_file import torch w = load_file('model.safetensors') def decode(y3, y2, y1, y0): inp = torch.tensor([float(y3), float(y2), float(y1), float(y0)]) a0 = int((inp @ w['a0.weight'].T + w['a0.bias'] >= 0).item()) a1 = int((inp @ w['a1.weight'].T + w['a1.bias'] >= 0).item()) return a1, a0 # decode(0, 1, 0, 0) = (1, 0) # value 2 ``` ## Applications - Priority encoder output processing - State machine decoding - Memory address reconstruction - Neural network output interpretation ## License MIT