Files
pyRBM/tests/test_xor.py
T
jensandClaude Sonnet 4.6 3edc124a5c [refactor] move tests to tests/, add pytest functions and main() entrypoints
- Moved src/tests/ → tests/
- Added test_* functions with assertions to script-style test files
- Added main() to each so IDEs offer it as a separate run target from pytest
- Fixed cupy_test.py: remove spurious x_gpu += x_cpu, fix duplicate xlabel

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-02 21:32:58 +02:00

47 lines
1.1 KiB
Python

import os.path
from rbm.layer import Layer
from rbm.status import Status
from rbm.train import train
from rbm.matrix import Mat, np
from rbm.entity import EntityParams, TrainingParams
WORK_DIR = "../../results"
USE_OPTIMIZER = True
def xor():
# Create params
entity_params = EntityParams()
training_params = TrainingParams()
entity_params.do_rao_blackwell = True
entity_params.num_gibbs_samples = 3
# Create layer
layer = Layer("Layer_0", (3, 1, 0, 16), entity_params, training_params)
# Init weights
layer.init(0.01)
# Load weights (if exists)
layer.load(os.path.join(WORK_DIR, "xor_layer0_state.npz"))
# Prepare training data
training_batch = Mat([[0,1,1], [0,0,0], [1,1,0], [1,0,1]], dtype=np.float64)
# Train layer
train(layer.entity, training_batch, Status())
# Save weights
layer.save(os.path.join(WORK_DIR, "xor_layer0_state.npz"))
# Test with test data
test_batch = Mat([[0,0,0], [0,1,0], [1,0,0], [1,1,0]], dtype=np.float64)
for pattern in test_batch:
h = layer.entity.forward(pattern)
v = layer.entity.reconstruct(h)
print(f"P{pattern} : {v}")
if __name__ == "__main__":
xor()
print("Test: [passed]")