from model.model import Model from rbm.entity import Entity, EntityParams, TrainingParams from rbm.matrix import Mat, np class DeepModel(Model): def __init__(self, name: str, work_dir: str = '.'): super().__init__(name, work_dir) self.unit1 = Entity((1024, 333), EntityParams(), TrainingParams(learning_rate=0.1, momentum=0.9, do_rao_blackwell=True, num_epochs=1000)) self.unit2 = Entity((333, 64), EntityParams(), TrainingParams(learning_rate=0.1, momentum=0.9, do_rao_blackwell=True, num_epochs=1000)) self.unit3 = Entity((64, 128), EntityParams(), TrainingParams(learning_rate=0.1, momentum=0.9, do_rao_blackwell=True, num_epochs=1000)) self.unit4 = Entity((128, 128), EntityParams(), TrainingParams(learning_rate=0.1, momentum=0.9, do_rao_blackwell=True, num_epochs=1000)) def forward(self, x: Mat): x = self.unit1.forward(x) x = self.unit2.forward(x) x = self.unit3.forward(x) x = self.unit4.forward(x) return x def backward(self, x: Mat): x = self.unit4.reconstruct(x) x = self.unit3.reconstruct(x) x = self.unit2.reconstruct(x) x = self.unit1.reconstruct(x) return x def test_deep_model(): model = DeepModel("TestModel_pytest", "results") model.unit1.training_params.num_epochs = 100 model.unit2.training_params.num_epochs = 100 model.unit3.training_params.num_epochs = 100 model.unit4.training_params.num_epochs = 100 model.init(0.1) batch = (np.random.rand(32, 1024) > 0.5).astype(np.float64) model.train(batch) errors = [float(np.mean((model.backward(model.forward(x)) - x) ** 2)) for x in batch] assert sum(errors) / len(errors) < 0.5, "Deep model reconstruction MSE too high" def main(): model = DeepModel("TestModel", "results") model.init(0.1) model.load() batch = (np.random.rand(64, 1024) > 0.5).astype(np.float64) model.train(batch) model.save() for index, inp in enumerate(batch): out = model.backward(model.forward(inp))[0] print(f"- Pattern {index} -------------------------") print(f"Input : {inp}") print(f"Output : {(out > 0.9).astype(np.float64)}") print(f"Error : {np.mean((out - inp)**2):0.3f}") if __name__ == "__main__": main()