File size: 1,073 Bytes
a3d2818
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from torch import nn, optim
import torch
from torch.nn import functional as F
from typing import Any, Callable, Optional
import math

class VanillaGAN(nn.Module):
    def __init__(self, resolution, latent_dim, hidden_dim=512, channels=3):
        super(VanillaGAN, self).__init__()
        output_dim = resolution * resolution * channels

        self.layers = nn.Sequential(
            self.gen_block(latent_dim, hidden_dim),
            self.gen_block(hidden_dim, hidden_dim*2),
             self.gen_block(hidden_dim*2, hidden_dim*2),
            self.gen_block(hidden_dim*2, hidden_dim),
            self.gen_block(hidden_dim, hidden_dim),
            self.gen_block(hidden_dim, hidden_dim//2),
            
            nn.Linear(hidden_dim//2, output_dim),
            nn.Tanh()
        )

    def gen_block(self, input_dim, output_dim):
        return nn.Sequential(
            nn.Linear(input_dim, output_dim, bias=False),
            nn.BatchNorm1d(output_dim, 0.8),
            nn.LeakyReLU(0.2)
        )

    def forward(self, x):
        return self.layers(x)