- adapted test to TrainingParameter as parat of Entity
- learn_norbs_labes start working
This commit is contained in:
@@ -3,14 +3,13 @@ import matplotlib.pyplot as plt
|
|||||||
|
|
||||||
from rbm.model import Model
|
from rbm.model import Model
|
||||||
from rbm.label import Label
|
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.matrix import Mat, np
|
||||||
from rbm.train import TrainingParams
|
|
||||||
|
|
||||||
class LabelLearner(Model):
|
class LabelLearner(Model):
|
||||||
def __init__(self, name: str, work_dir: str = '.'):
|
def __init__(self, name: str, work_dir: str = '.'):
|
||||||
super().__init__(name, work_dir)
|
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):
|
def forward(self, x: Mat):
|
||||||
x = self.unit1.forward(x)
|
x = self.unit1.forward(x)
|
||||||
@@ -45,7 +44,7 @@ if __name__ == "__main__":
|
|||||||
model.load()
|
model.load()
|
||||||
|
|
||||||
# Train
|
# Train
|
||||||
model.train(encoded, TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=1000))
|
model.train(encoded)
|
||||||
|
|
||||||
# save state
|
# save state
|
||||||
model.save()
|
model.save()
|
||||||
@@ -57,7 +56,7 @@ if __name__ == "__main__":
|
|||||||
axes[index].imshow(img)
|
axes[index].imshow(img)
|
||||||
axes[index].axis('off')
|
axes[index].axis('off')
|
||||||
axes[index].set_title(f'{test_labels[index]}')
|
axes[index].set_title(f'{test_labels[index]}')
|
||||||
print(inp)
|
|
||||||
plt.show()
|
plt.show()
|
||||||
|
|
||||||
print("Test: [passed]")
|
print("Test: [passed]")
|
||||||
|
|||||||
@@ -2,26 +2,37 @@ import os
|
|||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
from rbm.model import Model
|
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.matrix import Mat, np, read_armadillo
|
||||||
from rbm.train import TrainingParams
|
|
||||||
from rbm.layout import Horizontal
|
|
||||||
|
|
||||||
class TestModel(Model):
|
class TestModel(Model):
|
||||||
def __init__(self, name: str, work_dir: str = '.'):
|
def __init__(self, name: str, work_dir: str = '.'):
|
||||||
super().__init__(name, work_dir)
|
super().__init__(name, work_dir)
|
||||||
self.unit1 = Horizontal([
|
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))
|
||||||
Entity((96*96, 16), EntityParams(do_gaussian_visible=True, do_gaussian_hidden=True)),
|
self.unit2 = Entity((16, 24), EntityParams())
|
||||||
Entity((16, 16), EntityParams())
|
|
||||||
])
|
|
||||||
|
|
||||||
def forward(self, x: Mat):
|
|
||||||
x = self.unit1.forward(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def reconstruct(self, x: Mat):
|
def forward(self, v: Mat):
|
||||||
x = self.unit1.reconstruct(x)
|
res = np.ndarray(shape=(1,0))
|
||||||
return x
|
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__":
|
if __name__ == "__main__":
|
||||||
work_dir = "results"
|
work_dir = "results"
|
||||||
@@ -44,14 +55,16 @@ if __name__ == "__main__":
|
|||||||
test_batch = read_armadillo(os.path.join(prj_root, f"{prj_name}.test.dat"))
|
test_batch = read_armadillo(os.path.join(prj_root, f"{prj_name}.test.dat"))
|
||||||
|
|
||||||
# Train
|
# 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
|
# save state
|
||||||
model.save()
|
model.save()
|
||||||
|
|
||||||
fig, axes = plt.subplots(1, len(test_batch), figsize=(12, 3))
|
fig, axes = plt.subplots(1, len(test_batch), figsize=(12, 3))
|
||||||
for index, inp in enumerate(test_batch):
|
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 = 2*(out_normalized + 0.5)
|
||||||
img = np.reshape(img, (96, 96))
|
img = np.reshape(img, (96, 96))
|
||||||
axes[index].imshow(img)
|
axes[index].imshow(img)
|
||||||
|
|||||||
Reference in New Issue
Block a user