feat: str methods for Autoencoder
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user