| import torch | |
| from safetensors.torch import load_file | |
| def load_model(path='model.safetensors'): | |
| return load_file(path) | |
| def demux4(d, s1, s0, weights): | |
| """1:4 Demultiplexer: routes d to output y[s] where s = 2*s1 + s0""" | |
| inp = torch.tensor([float(d), float(s1), float(s0)]) | |
| outputs = [] | |
| for i in range(4): | |
| y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) | |
| outputs.append(y) | |
| return outputs | |
| if __name__ == '__main__': | |
| w = load_model() | |
| print('DEMUX4 truth table:') | |
| print('d s1 s0 | y0 y1 y2 y3') | |
| print('---+----+------------') | |
| for d in [0, 1]: | |
| for s in range(4): | |
| s1, s0 = (s >> 1) & 1, s & 1 | |
| result = demux4(d, s1, s0, w) | |
| print(f'{d} {s1} {s0} | {result[0]} {result[1]} {result[2]} {result[3]}') | |