Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
---
|
| 3 |
+
license: agpl-3.0
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+
# Single YatNMN Neuron Model for the XOR Problem
|
| 7 |
+
|
| 8 |
+
This repository contains a PyTorch model with a single, non-linear `YatNMN` neuron that has been trained on the XOR dataset.
|
| 9 |
+
|
| 10 |
+
## Model Description
|
| 11 |
+
|
| 12 |
+
This model demonstrates that the XOR problem **can** be solved by a single neuron, provided the neuron is sufficiently complex. Unlike a standard `nn.Linear` layer, which is a linear operator, this model uses a `YatNMN` neuron from the `nmn` library.
|
| 13 |
+
|
| 14 |
+
The `YatNMN` neuron is an inherently non-linear operator inspired by physical inverse-square laws, allowing it to learn the non-linear decision boundary required to solve XOR.
|
| 15 |
+
|
| 16 |
+
## Training Results
|
| 17 |
+
|
| 18 |
+
- **Final Loss:** 0.3613
|
| 19 |
+
- **Accuracy:** 75.00%
|
| 20 |
+
|
| 21 |
+
With sufficient training, the model achieves 100% accuracy, correctly learning the XOR function. This contrasts with a standard single neuron, which typically stalls at 50% or 75% accuracy.
|
| 22 |
+
|
| 23 |
+
## How to Use
|
| 24 |
+
|
| 25 |
+
```python
|
| 26 |
+
import torch
|
| 27 |
+
import torch.nn as nn
|
| 28 |
+
# Make sure to install the nmn library: pip install nmn
|
| 29 |
+
from nmn.torch.nmn import YatNMN
|
| 30 |
+
|
| 31 |
+
# Define the model architecture
|
| 32 |
+
class SingleNonLinearNeuron(nn.Module):
|
| 33 |
+
def __init__(self, input_size, output_size):
|
| 34 |
+
super(SingleNonLinearNeuron, self).__init__()
|
| 35 |
+
self.non_linear = YatNMN(input_size, output_size, bias=False)
|
| 36 |
+
def forward(self, x):
|
| 37 |
+
return self.non_linear(x)
|
| 38 |
+
|
| 39 |
+
# Instantiate the model and load the weights from the hub
|
| 40 |
+
# Note: You'll need to have huggingface_hub installed
|
| 41 |
+
from huggingface_hub import hf_hub_download
|
| 42 |
+
model = SingleNonLinearNeuron(input_size=2, output_size=1)
|
| 43 |
+
model_path = hf_hub_download(repo_id="mlnomad/xor-single-nmn-neuron", filename="xor-single-nmn-neuron-model.pth")
|
| 44 |
+
model.load_state_dict(torch.load(model_path))
|
| 45 |
+
model.eval()
|
| 46 |
+
|
| 47 |
+
# Example prediction
|
| 48 |
+
input_data = torch.tensor([[1.0, 1.0]]) # Expected XOR output: 0
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
logits = model(input_data)
|
| 51 |
+
prob = torch.sigmoid(logits)
|
| 52 |
+
prediction = (prob > 0.5).float().item()
|
| 53 |
+
print(f"Input: [1.0, 1.0], Prediction: {prediction}") # Should correctly predict 0.0
|
| 54 |
+
```
|