ae_toolbox_torch/networks/attention_based_auto_enoder.py
2019-09-29 09:37:30 +02:00

49 lines
1.5 KiB
Python

from torch.optim import Adam
from .modules import *
from torch.nn.functional import mse_loss
from torch import Tensor
#######################
# Basic AE-Implementation
class AE_WithAttention(AbstractNeuralNetwork, ABC):
def __init__(self, latent_dim: int=0, features: int = 0, **kwargs):
assert latent_dim and features
super(AE_WithAttention, self).__init__()
self.latent_dim = latent_dim
self.features = features
self.encoder = Encoder(self.latent_dim)
self.decoder = Decoder(self.latent_dim, self.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], batch.shape[1], -1))(z)
x_hat = self.decoder(z_repeatet)
return z, x_hat
class AE_WithAttention_LO(LightningModuleOverrides):
def __init__(self):
super(AE_WithAttention_LO, self).__init__()
def training_step(self, x, batch_nb):
# ToDo: We need a new loss function, fullfilling all attention needs
# z, x_hat
_, x_hat = self.forward(x)
loss = mse_loss(x, x_hat)
return {'loss': loss}
def configure_optimizers(self):
return [Adam(self.parameters(), lr=0.02)]
if __name__ == '__main__':
raise PermissionError('Get out of here - never run this module')