Skip to content

Instantly share code, notes, and snippets.

View antonin-lfv's full-sized avatar
🏠
Working from home

Antonin antonin-lfv

🏠
Working from home
View GitHub Profile
@antonin-lfv
antonin-lfv / train_VAE.py
Created December 24, 2022 11:50
VAE train function
def train(vae, data_dataloader, epochs):
loss_evolution = []
opt = torch.optim.Adam(autoencoder.parameters())
for _ in tqdm(range(epochs)):
for i, (x, y) in enumerate(data_dataloader):
opt.zero_grad()
x_hat = vae(x)
# Loss = MSE + divergence KL
loss = ((x - x_hat) ** 2).sum() + autoencoder.encoder.kl
if i == (len(x) - 1):
@antonin-lfv
antonin-lfv / generation_ECG.py
Created December 4, 2022 12:11
Auto encoder ECG generation
generated_ECG = []
for i in range(n):
latent_vector_shape = (1, 25)
new_latent_ECG = vae_trained.encoder.N.sample(latent_vector_shape)
generated_ECG.append(vae_trained.decoder(new_latent_ECG))
@antonin-lfv
antonin-lfv / autoencoder.py
Created December 3, 2022 22:50
Autoencoder for ECG generation and classification
class VariationalAutoencoder(nn.Module):
def __init__(self):
super(VariationalAutoencoder, self).__init__()
self.encoder = Encoder()
self.decoder = Decoder()
def forward(self, x):
z = self.encoder(x)
return self.decoder(z)
@antonin-lfv
antonin-lfv / decoder.py
Last active December 24, 2022 11:53
VAE Decoder for ECG generation and classification
class Decoder(nn.Module):
"""
Décode le vecteur latent et renvoie les données reconstruites
"""
def __init__(self):
super(Decoder, self).__init__()
# Couches convolutives
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2)
self.convbatchNorm1 = nn.BatchNorm2d(16)
@antonin-lfv
antonin-lfv / encoder.py
Last active December 26, 2022 13:47
VAE Encoder for ECG generation and classification
class Encoder(nn.Module):
"""
Encode les données et renvoie le vecteur latent
"""
def __init__(self):
super(Encoder, self).__init__()
# Couches convolutives
self.conv1 = nn.Conv2d(1, 8, stride=1, padding=2, kernel_size=5)
self.convbatchNorm1 = nn.BatchNorm2d(8)