ae_toolbox_torch/networks/auto_encoder.py
Si11ium 744c0c50b7 Done: First VIsualization
ToDo: Visualization for all classes, latent space setups
2019-08-21 07:56:31 +02:00

46 lines
1.3 KiB
Python

from .modules import *
from torch.nn.functional import mse_loss
from torch import Tensor
#######################
# Basic AE-Implementation
class AutoEncoder(AbstractNeuralNetwork, ABC):
def __init__(self, latent_dim: int, dataParams: dict, **kwargs):
super(AutoEncoder, self).__init__()
self.dataParams = dataParams
self.latent_dim = latent_dim
self.encoder = Encoder(self.latent_dim)
self.decoder = Decoder(self.latent_dim, self.dataParams['features'])
def forward(self, batch: Tensor):
# Encoder
# outputs, hidden (Batch, Timesteps aka. Size, Features / Latent Dim Size)
z = self.encoder(batch)
# Decoder
# First repeat the data accordingly to the batch size
z_repeatet = Repeater((batch.shape[0], self.dataParams['size'], -1))(z)
x_hat = self.decoder(z_repeatet)
return z, x_hat
class AutoEncoderLightningOverrides:
@property
def name(self):
return self.__class__.__name__
def forward(self, x):
return self.network.forward(x)
def training_step(self, x, batch_nb):
# z, x_hat
_, x_hat = self.forward(x)
loss = mse_loss(x, x_hat)
return {'loss': loss}
if __name__ == '__main__':
raise PermissionError('Get out of here - never run this module')