added LabelLearner
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
from rbm.model import Model
|
||||
from rbm.label import Label
|
||||
from rbm.entity import Entity, EntityParams
|
||||
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, 16), EntityParams())
|
||||
|
||||
def forward(self, x: Mat):
|
||||
x = self.unit1.forward(x)
|
||||
return x
|
||||
|
||||
def backward(self, x: Mat):
|
||||
x = self.unit1.reconstruct(x)
|
||||
return x
|
||||
|
||||
if __name__ == "__main__":
|
||||
voc_size = 100
|
||||
work_dir = "../../results"
|
||||
prj_root = "/home/jens/work/repos/Rbm"
|
||||
|
||||
# Create Labeler
|
||||
enc = Label(voc_size, 16, work_dir)
|
||||
|
||||
# Load
|
||||
enc.fitter.load()
|
||||
|
||||
# Encode test labels
|
||||
test_labels = np.array([1, 23, 99, 37, 55, 7, 31, 10, 19, 70])
|
||||
encoded, _ = enc.encode(test_labels)
|
||||
|
||||
# Create model
|
||||
model = LabelLearner("Label-Learner", work_dir)
|
||||
|
||||
# Init state
|
||||
model.init(0.01)
|
||||
|
||||
# load state
|
||||
model.load()
|
||||
|
||||
# Train
|
||||
model.train(encoded, TrainingParams(learning_rate=0.01, momentum=0.9, do_rao_blackwell=True, num_epochs=10000))
|
||||
|
||||
# save state
|
||||
model.save()
|
||||
|
||||
# Plot
|
||||
fig, axes = plt.subplots(1, len(encoded), figsize=(12, 3))
|
||||
for index, inp in enumerate(encoded):
|
||||
img = np.reshape(inp, (4, 4))
|
||||
axes[index].imshow(img)
|
||||
axes[index].axis('off')
|
||||
axes[index].set_title(f'{test_labels[index]}')
|
||||
|
||||
plt.show()
|
||||
|
||||
print("Test: [passed]")
|
||||
|
||||
Reference in New Issue
Block a user