transition

This commit is contained in:
Si11ium
2021-02-01 09:59:56 +01:00
parent 4c489237d7
commit 578727d043
35 changed files with 177 additions and 305 deletions

View File

@ -11,9 +11,10 @@ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class AdversarialAE(AutoEncoder):
def __init__(self, *args, **kwargs):
def __init__(self, *args, train_on_predictions=False, use_norm=False, **kwargs):
super(AdversarialAE, self).__init__(*args, **kwargs)
self.discriminator = Discriminator(self.latent_dim, self.features)
self.discriminator = Discriminator(self.latent_dim, self.features, use_norm=use_norm)
self.train_on_predictions = train_on_predictions
def forward(self, batch):
# Encoder
@ -25,13 +26,6 @@ class AdversarialAE(AutoEncoder):
x_hat = self.decoder(z_repeatet)
return z, x_hat
class AdversarialAE_LO(LightningModuleOverrides):
def __init__(self, train_on_predictions=False):
super(AdversarialAE_LO, self).__init__()
self.train_on_predictions = train_on_predictions
def training_step(self, batch, _, optimizer_i):
x, y = batch
z, x_hat = self.forward(x)
@ -66,8 +60,7 @@ class AdversarialAE_LO(LightningModuleOverrides):
else:
raise RuntimeError('This should not have happened, catch me if u can.')
# This is Fucked up, why do i need to put an additional empty list here?
#FIXME: This is Fucked up, why do i need to put an additional empty list here?
def configure_optimizers(self):
return [Adam(self.network.discriminator.parameters(), lr=0.02),
Adam([*self.network.encoder.parameters(), *self.network.decoder.parameters()], lr=0.02), ],\

View File

@ -27,12 +27,6 @@ class AE_WithAttention(AbstractNeuralNetwork, ABC):
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

View File

@ -9,9 +9,11 @@ from torch import Tensor
# Basic AE-Implementation
class AutoEncoder(AbstractNeuralNetwork, ABC):
def __init__(self, latent_dim: int=0, features: int = 0, use_norm=True, **kwargs):
def __init__(self, latent_dim: int=0, features: int = 0, use_norm=True,
train_on_predictions=False, **kwargs):
assert latent_dim and features
super(AutoEncoder, self).__init__()
self.train_on_predictions = train_on_predictions
self.latent_dim = latent_dim
self.features = features
self.encoder = Encoder(self.latent_dim, use_norm=use_norm)
@ -27,13 +29,6 @@ class AutoEncoder(AbstractNeuralNetwork, ABC):
x_hat = self.decoder(z_repeatet)
return z, x_hat
class AutoEncoder_LO(LightningModuleOverrides):
def __init__(self, train_on_predictions=False):
super(AutoEncoder_LO, self).__init__()
self.train_on_predictions = train_on_predictions
def training_step(self, batch, batch_nb):
x, y = batch
# z, x_hat

View File

@ -5,9 +5,7 @@ from functools import reduce
import torch
from torch import randn
import pytorch_lightning as pl
from pytorch_lightning import data_loader
from torch.nn import Module, Linear, ReLU, Sigmoid, Dropout, GRU, Tanh
from torchvision.transforms import Normalize
from abc import ABC, abstractmethod
@ -27,21 +25,12 @@ class LightningModuleOverrides:
def name(self):
return self.__class__.__name__
def forward(self, x):
return self.network.forward(x)
@data_loader
@pl.data_loader
def train_dataloader(self):
num_workers = 0 # os.cpu_count() // 2
num_workers = 0 # os.cpu_count() // 2
return DataLoader(DataContainer(os.path.join('data', 'training'), self.size, self.step),
shuffle=True, batch_size=10000, num_workers=num_workers)
"""
@data_loader
def val_dataloader(self):
num_workers = 0 # os.cpu_count() // 2
return DataLoader(DataContainer(os.path.join('data', 'validation'), self.size, self.step),
shuffle=True, batch_size=100, num_workers=num_workers)
"""
class AbstractNeuralNetwork(Module):
@ -56,53 +45,6 @@ class AbstractNeuralNetwork(Module):
def forward(self, batch):
pass
######################
# Abstract Network class following the Lightning Syntax
class LightningModule(pl.LightningModule, ABC):
def __init__(self):
super(LightningModule, self).__init__()
@abstractmethod
def forward(self, x):
raise NotImplementedError
@abstractmethod
def training_step(self, batch, batch_nb):
# REQUIRED
raise NotImplementedError
@abstractmethod
def configure_optimizers(self):
# REQUIRED
raise NotImplementedError
@pl.data_loader
def train_dataloader(self):
# REQUIRED
raise NotImplementedError
"""
def validation_step(self, batch, batch_nb):
# OPTIONAL
pass
def validation_end(self, outputs):
# OPTIONAL
pass
@pl.data_loader
def val_dataloader(self):
# OPTIONAL
pass
@pl.data_loader
def test_dataloader(self):
# OPTIONAL
pass
"""
#######################
# Utility Modules
class TimeDistributed(Module):
@ -167,12 +109,14 @@ class AvgDimPool(Module):
# Generators, Decoders, Encoders, Discriminators
class Discriminator(Module):
def __init__(self, latent_dim, features, dropout=.0, activation=ReLU):
def __init__(self, latent_dim, features, dropout=.0, activation=ReLU, use_norm=False):
super(Discriminator, self).__init__()
self.features = features
self.latent_dim = latent_dim
self.l1 = Linear(self.latent_dim, self.features * 10)
self.norm1 = torch.nn.BatchNorm1d(self.features * 10) if use_norm else False
self.l2 = Linear(self.features * 10, self.features * 20)
self.norm2 = torch.nn.BatchNorm1d(self.features * 20) if use_norm else False
self.lout = Linear(self.features * 20, 1)
self.dropout = Dropout(dropout)
self.activation = activation()
@ -180,9 +124,15 @@ class Discriminator(Module):
def forward(self, x, **kwargs):
tensor = self.l1(x)
tensor = self.dropout(self.activation(tensor))
tensor = self.dropout(tensor)
if self.norm1:
tensor = self.norm1(tensor)
tensor = self.activation(tensor)
tensor = self.l2(tensor)
tensor = self.dropout(self.activation(tensor))
tensor = self.dropout(tensor)
if self.norm2:
tensor = self.norm2(tensor)
tensor = self.activation(tensor)
tensor = self.lout(tensor)
tensor = self.sigmoid(tensor)
return tensor
@ -296,13 +246,13 @@ class AttentionEncoder(Module):
class PoolingEncoder(Module):
def __init__(self, lat_dim, variational=False):
def __init__(self, lat_dim, variational=False, use_norm=True):
self.lat_dim = lat_dim
self.variational = variational
super(PoolingEncoder, self).__init__()
self.p = AvgDimPool()
self.l = EncoderLinearStack()
self.l = EncoderLinearStack(use_norm=use_norm)
if variational:
self.mu = Linear(self.l.shape, self.lat_dim)
self.logvar = Linear(self.l.shape, self.lat_dim)

View File

@ -6,12 +6,13 @@ import torch
class SeperatingAAE(Module):
def __init__(self, latent_dim, features, use_norm=True):
def __init__(self, latent_dim, features, train_on_predictions=False, use_norm=True):
super(SeperatingAAE, self).__init__()
self.latent_dim = latent_dim
self.features = features
self.spatial_encoder = PoolingEncoder(self.latent_dim)
self.train_on_predictions = train_on_predictions
self.spatial_encoder = PoolingEncoder(self.latent_dim, use_norm=use_norm)
self.temporal_encoder = Encoder(self.latent_dim, use_dense=False, use_norm=use_norm)
self.decoder = Decoder(self.latent_dim * 2, self.features, use_norm=use_norm)
self.spatial_discriminator = Discriminator(self.latent_dim, self.features)
@ -28,13 +29,6 @@ class SeperatingAAE(Module):
x_hat = self.decoder(z_repeatet)
return z_spatial, z_temporal, x_hat
class SeparatingAAE_LO(LightningModuleOverrides):
def __init__(self, train_on_predictions=False):
super(SeparatingAAE_LO, self).__init__()
self.train_on_predictions = train_on_predictions
def training_step(self, batch, _, optimizer_i):
x, y = batch
spatial_latent_fake, temporal_latent_fake, x_hat = self.network.forward(x)
@ -92,7 +86,7 @@ class SeparatingAAE_LO(LightningModuleOverrides):
else:
raise RuntimeError('This should not have happened, catch me if u can.')
# This is Fucked up, why do i need to put an additional empty list here?
#FixMe: This is Fucked up, why do i need to put an additional empty list here?
def configure_optimizers(self):
return [Adam([*self.network.spatial_discriminator.parameters(), *self.network.spatial_encoder.parameters()]
, lr=0.02),

View File

@ -12,7 +12,7 @@ class VariationalAE(AbstractNeuralNetwork, ABC):
def name(self):
return self.__class__.__name__
def __init__(self, latent_dim=0, features=0, use_norm=True, **kwargs):
def __init__(self, latent_dim=0, features=0, use_norm=True, train_on_predictions=False, **kwargs):
assert latent_dim and features
super(VariationalAE, self).__init__()
self.features = features
@ -34,13 +34,6 @@ class VariationalAE(AbstractNeuralNetwork, ABC):
x_hat = self.decoder(repeat(z))
return mu, logvar, x_hat
class VAE_LO(LightningModuleOverrides):
def __init__(self, train_on_predictions=False):
super(VAE_LO, self).__init__()
self.train_on_predictions=train_on_predictions
def training_step(self, batch, _):
x, y = batch
mu, logvar, x_hat = self.forward(x)