Files
pyRBM/layer_test.ipynb
2025-12-19 15:20:04 +01:00

7.0 KiB

In [1]:
from rbm.params import EntityParams
from rbm.layer import Layer
from rbm.status import Status
from rbm.train import train
from rbm.matrix import Mat, np
In [2]:
params = EntityParams()
In [3]:
params.__dict__
Out [3]:
{'learning_rate': 0.1,
 'momentum': 0.5,
 'weight_decay': 0,
 'num_epochs': 1000,
 'mini_batch_size': 0,
 'do_rao_blackwell': False,
 'do_gibbs_sample_visible': False,
 'do_gibbs_sample_hidden': False,
 'do_batch_sample': False,
 'num_gibbs_samples': 1,
 'do_gaussian_visible': False,
 'do_gaussian_hidden': False}
In [4]:
params.do_rao_blackwell = True
params.num_gibbs_samples = 3
In [5]:
params.__dict__
Out [5]:
{'learning_rate': 0.1,
 'momentum': 0.5,
 'weight_decay': 0,
 'num_epochs': 1000,
 'mini_batch_size': 0,
 'do_rao_blackwell': True,
 'do_gibbs_sample_visible': False,
 'do_gibbs_sample_hidden': False,
 'do_batch_sample': False,
 'num_gibbs_samples': 3,
 'do_gaussian_visible': False,
 'do_gaussian_hidden': False}
In [6]:
layer = Layer("Layer_0", (3, 1, 0, 16), params)
In [7]:
layer.init(0.01)
In [8]:
layer.load()
In [9]:
training_batch = Mat([[0,1,1], [0,0,0], [1,1,0], [1,0,1]], dtype=np.float64)
In [10]:
train(layer.entity, training_batch, Status())
-------------------------------------------
progress              : 0%
err_rms              : 0.24999708676779586
-------------------------------------------
progress              : 10%
err_rms              : 0.24995450907424
-------------------------------------------
progress              : 20%
err_rms              : 0.24948447203582114
-------------------------------------------
progress              : 30%
err_rms              : 0.24412963558981704
-------------------------------------------
progress              : 40%
err_rms              : 0.1949812378035757
-------------------------------------------
progress              : 50%
err_rms              : 0.048675091786564394
-------------------------------------------
progress              : 60%
err_rms              : 0.00301183842639605
-------------------------------------------
progress              : 70%
err_rms              : 0.0006911957660915791
-------------------------------------------
progress              : 80%
err_rms              : 0.000293184530139503
-------------------------------------------
progress              : 90%
err_rms              : 0.0001607680838909887
-------------------------------------------
progress              : 100%
err_rms              : 0.00010079934874358073
-------------------------------------------
progress              : 100%
err_rms_total              : 9.954453566455675e-05
In [11]:
# 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}")
P[0. 0. 0.] : [[0.01017997 0.01309764 0.01289942]]
P[0. 1. 0.] : [[0.01382953 0.99673111 0.0121298 ]]
P[1. 0. 0.] : [[0.99411057 0.03544156 0.02632994]]
P[1. 1. 0.] : [[0.99256076 0.98906099 0.00948805]]
In [12]:
test_batch
Out [12]:
array([[0., 0., 0.],
       [0., 1., 0.],
       [1., 0., 0.],
       [1., 1., 0.]])
In [ ]: