File size: 995 Bytes
7feeb56 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def binary_to_onehot(a1, a0, weights):
"""Convert 2-bit binary to 4-bit one-hot encoding."""
inp = torch.tensor([float(a1), float(a0)])
y0 = int((inp @ weights['y0.weight'].T + weights['y0.bias'] >= 0).item())
y1 = int((inp @ weights['y1.weight'].T + weights['y1.bias'] >= 0).item())
y2 = int((inp @ weights['y2.weight'].T + weights['y2.bias'] >= 0).item())
y3 = int((inp @ weights['y3.weight'].T + weights['y3.bias'] >= 0).item())
return y0, y1, y2, y3
if __name__ == '__main__':
w = load_model()
print('Binary to One-Hot Encoder (2-to-4):')
print('a1 a0 | y0 y1 y2 y3 | value')
print('------+-------------+------')
for val in range(4):
a1, a0 = (val >> 1) & 1, val & 1
y0, y1, y2, y3 = binary_to_onehot(a1, a0, w)
print(f' {a1} {a0} | {y0} {y1} {y2} {y3} | {val}')
|