- added training params as list for model.train()
- added gaussian sample - introduced layout concept - updated README
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import os
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from rbm.model import Model
|
||||
from rbm.entity import Entity, EntityParams
|
||||
from rbm.matrix import Mat, np, read_armadillo
|
||||
from rbm.train import TrainingParams
|
||||
from rbm.layout import Horizontal
|
||||
|
||||
class TestModel(Model):
|
||||
def __init__(self, name: str, work_dir: str = '.'):
|
||||
super().__init__(name, work_dir)
|
||||
self.unit1 = Horizontal([
|
||||
Entity((96*96, 16), EntityParams(do_gaussian_visible=True, do_gaussian_hidden=True)),
|
||||
Entity((16, 16), EntityParams())
|
||||
])
|
||||
|
||||
def forward(self, x: Mat):
|
||||
x = self.unit1.forward(x)
|
||||
return x
|
||||
|
||||
def reconstruct(self, x: Mat):
|
||||
x = self.unit1.reconstruct(x)
|
||||
return x
|
||||
|
||||
if __name__ == "__main__":
|
||||
work_dir = "results"
|
||||
prj_name = "norb_small_16h_v2"
|
||||
prj_root = "/home/jens/work/repos/Rbm"
|
||||
|
||||
# Create model
|
||||
model = TestModel("norb_small_16h_v2", "results")
|
||||
|
||||
# Init state
|
||||
model.init(0.01)
|
||||
|
||||
# load state
|
||||
model.load()
|
||||
|
||||
# Load train data
|
||||
train_batch = read_armadillo(os.path.join(prj_root, f"{prj_name}.training.dat"))
|
||||
|
||||
# Load test data
|
||||
test_batch = read_armadillo(os.path.join(prj_root, f"{prj_name}.test.dat"))
|
||||
|
||||
# Train
|
||||
model.train(train_batch, TrainingParams(learning_rate=0.00001, momentum=0.9, do_rao_blackwell=True, num_epochs=100, num_gibbs_samples=3))
|
||||
|
||||
# save state
|
||||
model.save()
|
||||
|
||||
fig, axes = plt.subplots(1, len(test_batch), figsize=(12, 3))
|
||||
for index, inp in enumerate(test_batch):
|
||||
out_normalized = model.reconstruct(model.forward(inp))
|
||||
img = 2*(out_normalized + 0.5)
|
||||
img = np.reshape(img, (96, 96))
|
||||
axes[index].imshow(img)
|
||||
axes[index].axis('off')
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user