diff --git a/autoencoder.py b/autoencoder.py index 2bbbfc2..a09b2d9 100644 --- a/autoencoder.py +++ b/autoencoder.py @@ -17,6 +17,9 @@ class Autoencoder: self.encoder = DeepNNLayer(encoder_layers, lr, activation_func) self.decoder = DeepNNLayer(decoder_layers, lr, activation_func) + def __str__(self): + return f'Encoder:\n{self.encoder}\n\nDecoder:\n{self.decoder}' + def loss(self, data_set: list[np.ndarray]) -> float: loss = 0 for x in data_set: diff --git a/layers.py b/layers.py index 1c15247..3860ef7 100644 --- a/layers.py +++ b/layers.py @@ -17,6 +17,9 @@ class NNLayer: self.output_linear = None self.activation_func = activation_func + def __str__(self): + return f'[ {self.W.shape[0]} => {self.W.shape[1]}\tlr:{self.lr}\tactivation:{self.activation_func.__name__} ]' # noqa + def forward(self, V: np.ndarray) -> np.ndarray: self.input = normalize(V) self.output_linear = self.input @ self.W + self.B @@ -50,6 +53,9 @@ class DeepNNLayer: activation_func) ) + def __str__(self): + return '\n'.join([str(layer) for layer in self.layers]) + def forward(self, v: np.ndarray) -> np.ndarray: for layer in self.layers: v = layer.forward(v) diff --git a/mnist_test.py b/mnist_test.py index 288a8c8..d2d1012 100644 --- a/mnist_test.py +++ b/mnist_test.py @@ -51,8 +51,7 @@ def mnist_test(filename: str): x_train = x_train / 255 x_test = x_test / 255 autoencoder: Autoencoder = Autoencoder.load(filename) - for i in autoencoder.encoder.layers: - print(len(i.input), len(i.output)) + print(autoencoder) idx = np.random.randint(0, len(x_test)) example: np.ndarray = x_test[idx] output, code = autoencoder.forward(example.flatten())