-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.py
More file actions
38 lines (30 loc) · 1.25 KB
/
autoencoder.py
File metadata and controls
38 lines (30 loc) · 1.25 KB
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
33
34
35
36
37
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class Net(nn.Module):
def __init__(self,params):
super(Net, self).__init__()
# Encoding
self.norm=nn.BatchNorm2d(params['nc'])
self.conv1=nn.Conv2d(params['nc'],params['nf'], 16,4,6)
self.conv2=nn.Conv2d(params['nf'],2*params['nf'], 16,4,6)
self.conv3=nn.Conv2d(2*params['nf'],4*params['nf'], 4,2,1)
self.conv4=nn.Conv2d(4*params['nf'],params['ncode'], 5,1,0)
# Decoding
self.anticonv1=nn.ConvTranspose2d(params['ncode'],params['nf']*4,5,1,0)
self.anticonv2=nn.ConvTranspose2d(params['nf']*4,params['nf']*2,4,2,1)
self.anticonv3=nn.ConvTranspose2d(params['nf']*2,params['nf'],16,4,6)
self.anticonv4=nn.ConvTranspose2d(params['nf'],params['nc'],16,4,6)
def forward(self,x):
# x=F.batchnorm2d(x)
x=self.norm(x)
x=F.leaky_relu(self.conv1(x))
x=F.leaky_relu(self.conv2(x))
x=F.leaky_relu(self.conv3(x))
x=F.leaky_relu(self.conv4(x))
x=F.leaky_relu(self.anticonv1(x))
x=F.leaky_relu(self.anticonv2(x))
x=F.leaky_relu(self.anticonv3(x))
x=F.leaky_relu(self.anticonv4(x))
return x