File size: 1,305 Bytes
e9afe3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""

Threshold Network for 4-input NOR Gate

"""

import torch
from safetensors.torch import load_file


class ThresholdNOR4:
    def __init__(self, weights_dict):
        self.weight = weights_dict['weight']
        self.bias = weights_dict['bias']

    def __call__(self, x1, x2, x3, x4):
        inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
        weighted_sum = (inputs * self.weight).sum() + self.bias
        return (weighted_sum >= 0).float()

    @classmethod
    def from_safetensors(cls, path="model.safetensors"):
        return cls(load_file(path))


if __name__ == "__main__":
    weights = load_file("model.safetensors")
    model = ThresholdNOR4(weights)

    print("4-input NOR Gate Truth Table:")
    print("-" * 35)
    correct = 0
    for x1 in [0, 1]:
        for x2 in [0, 1]:
            for x3 in [0, 1]:
                for x4 in [0, 1]:
                    out = int(model(x1, x2, x3, x4).item())
                    expected = 1 - (x1 | x2 | x3 | x4)
                    status = "OK" if out == expected else "FAIL"
                    if out == expected:
                        correct += 1
                    print(f"NOR4({x1}, {x2}, {x3}, {x4}) = {out}  [{status}]")
    print(f"\nTotal: {correct}/16 correct")