-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
60 lines (52 loc) · 1.57 KB
/
model.py
File metadata and controls
60 lines (52 loc) · 1.57 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from torch import nn
import numpy as np
img_size = [1, 28, 28]
class Generator(nn.Module):
# 输入的是随机噪声
def __init__(self, in_dims=10):
super(Generator, self).__init__()
self.stack = nn.Sequential(
nn.Linear(in_dims, 64),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.Linear(64, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Linear(128, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Linear(256, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Linear(512, np.prod(img_size).item()),
nn.Tanh() # 输出在-1到1之间
)
def forward(self, x):
x = self.stack(x)
x = x.reshape([x.shape[0], *img_size])
return x
class Discriminator(nn.Module):
# 输入是一张图片
def __init__(self):
super(Discriminator, self).__init__()
self.stack = nn.Sequential(
nn.Linear(np.prod(img_size).item(), 512),
nn.BatchNorm1d(512),
nn.LeakyReLU(),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.LeakyReLU(),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.LeakyReLU(),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.LeakyReLU(inplace=True),
nn.Linear(64, 1),
# nn.Sigmoid()
)
def forward(self, x):
# 输入是一张图片
x = x.reshape([x.shape[0], -1])
x = self.stack(x)
return x