46 lines
1.3 KiB
Python
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(Module, ABC):
|
|
|
|
@property
|
|
def name(self):
|
|
return self.__class__.__name__
|
|
|
|
def __init__(self, dataParams, **kwargs):
|
|
super(AutoEncoder, self).__init__()
|
|
self.dataParams = dataParams
|
|
self.latent_dim = kwargs.get('latent_dim', 2)
|
|
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:
|
|
|
|
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')
|