- adapted test to TrainingParameter as parat of Entity

- learn_norbs_labes start working
This commit is contained in:
2026-01-02 14:53:45 +01:00
parent 10691894d1
commit c7f820ec63
2 changed files with 32 additions and 20 deletions
+4 -5
View File
@@ -3,14 +3,13 @@ import matplotlib.pyplot as plt
from rbm.model import Model
from rbm.label import Label
from rbm.entity import Entity, EntityParams
from rbm.entity import Entity, EntityParams, TrainingParams
from rbm.matrix import Mat, np
from rbm.train import TrainingParams
class LabelLearner(Model):
def __init__(self, name: str, work_dir: str = '.'):
super().__init__(name, work_dir)
self.unit1 = Entity((16, 8), EntityParams())
self.unit1 = Entity((16, 8), EntityParams(), TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=10000))
def forward(self, x: Mat):
x = self.unit1.forward(x)
@@ -45,7 +44,7 @@ if __name__ == "__main__":
model.load()
# Train
model.train(encoded, TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=1000))
model.train(encoded)
# save state
model.save()
@@ -57,7 +56,7 @@ if __name__ == "__main__":
axes[index].imshow(img)
axes[index].axis('off')
axes[index].set_title(f'{test_labels[index]}')
print(inp)
plt.show()
print("Test: [passed]")
+28 -15
View File
@@ -2,26 +2,37 @@ import os
import matplotlib.pyplot as plt
from rbm.model import Model
from rbm.entity import Entity, EntityParams
from rbm.entity import Entity, EntityParams, TrainingParams
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())
])
self.unit1 = Entity((96*96, 16), EntityParams(do_gaussian_visible=True, do_gaussian_hidden=True), TrainingParams(learning_rate=0.00001, momentum=0.9, do_rao_blackwell=True, num_epochs=1000, num_gibbs_samples=1))
self.unit2 = Entity((16, 24), 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
def forward(self, v: Mat):
res = np.ndarray(shape=(1,0))
index = 0
for unit in self.objects():
size = unit.shape[0]
vp = v[index:index+size]
index += size
h = unit.forward(vp)
res = np.concat((res, h), axis=1)
return res
def reconstruct(self, h: Mat):
res = np.ndarray(shape=(1,0))
index = 0
for unit in self.objects():
size = unit.shape[1]
hp = np.transpose(np.transpose(h)[index:index+size])
index += size
v = unit.reconstruct(hp)
res = np.concat((res, v), axis=1)
return res
if __name__ == "__main__":
work_dir = "results"
@@ -44,14 +55,16 @@ if __name__ == "__main__":
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))
model.train(train_batch)
# 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))
label_zero = np.zeros(shape=16)
query = np.concat((inp, label_zero), axis=0)
out_normalized = np.ndarray.flatten(model.reconstruct(model.forward(query)))[0:96*96]
img = 2*(out_normalized + 0.5)
img = np.reshape(img, (96, 96))
axes[index].imshow(img)