refactor RnnModel: multi-unit delay training and unified vc interface
- Add batch_delay() to shift visible input per unit index - Unify forward_step() to work with combined vc matrix - Fix split() to always slice on axis=1 - Add index param to Entity for readable naming - Rename test_xor.py to xor.py, replace Mat with np.array Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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 = np.array([[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]")
|
||||
Reference in New Issue
Block a user