CharlesCNorton
Add canalizing function threshold circuit
1cc3b6f
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def canalizing(x2, x1, x0, weights):
"""Canalizing function: if x0=1, output=0 regardless of x1,x2."""
inp = torch.tensor([float(x2), float(x1), float(x0)])
not_x0 = int((inp @ weights['not_x0.weight'].T + weights['not_x0.bias'] >= 0).item())
or_x1_x2 = int((inp @ weights['or_x1_x2.weight'].T + weights['or_x1_x2.bias'] >= 0).item())
l1 = torch.tensor([float(not_x0), float(or_x1_x2)])
y = int((l1 @ weights['y.weight'].T + weights['y.bias'] >= 0).item())
return y
if __name__ == '__main__':
w = load_model()
print('Canalizing Function (x0 canalizes to 0):')
for i in range(8):
x2, x1, x0 = (i >> 2) & 1, (i >> 1) & 1, i & 1
y = canalizing(x2, x1, x0, w)
print(f' {x2}{x1}{x0} -> {y}')