-
Notifications
You must be signed in to change notification settings - Fork 16
/
Net_module.py
201 lines (161 loc) · 6.73 KB
/
Net_module.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 28 19:26:44 2020
@author: Administrator
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.shape[0], -1)
class ConvBn(nn.Module):
def __init__(self, in_c, out_c, kernel=(1, 1), stride=1, padding=0, groups=1):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(in_c, out_c, kernel, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_c)
)
def forward(self, x):
return self.net(x)
class ConvBnPrelu(nn.Module):
def __init__(self, in_c, out_c, kernel=(1, 1), stride=1, padding=0, groups=1):
super().__init__()
self.net = nn.Sequential(
ConvBn(in_c, out_c, kernel, stride, padding, groups),
nn.PReLU(out_c)
)
def forward(self, x):
return self.net(x)
class DepthWise(nn.Module):
def __init__(self, in_c, out_c, kernel=(3, 3), stride=2, padding=1, groups=1):
super().__init__()
self.net = nn.Sequential(
ConvBnPrelu(in_c, groups, kernel=(1, 1), stride=1, padding=0),
ConvBnPrelu(groups, groups, kernel=kernel, stride=stride, padding=padding, groups=groups),
ConvBn(groups, out_c, kernel=(1, 1), stride=1, padding=0),
)
def forward(self, x):
return self.net(x)
class DepthWiseRes(nn.Module):
"""DepthWise with Residual"""
def __init__(self, in_c, out_c, kernel=(3, 3), stride=2, padding=1, groups=1):
super().__init__()
self.net = DepthWise(in_c, out_c, kernel, stride, padding, groups)
def forward(self, x):
return self.net(x) + x
class MultiDepthWiseRes(nn.Module):
def __init__(self, num_block, channels, kernel=(3, 3), stride=1, padding=1, groups=1):
super().__init__()
self.net = nn.Sequential(*[
DepthWiseRes(channels, channels, kernel, stride, padding, groups)
for _ in range(num_block)
])
def forward(self, x):
return self.net(x)
# class Net_dueling_DDQN(nn.Module):
# def __init__(self,input_dim, output_dim):
# """channel size M = search space number N = RF chain number"""
# self.channel = input_dim[0]
# self.M = input_dim[1]
# self.K = input_dim[2]
# super().__init__()
# self.res1 = nn.Linear(self.M*self.K*self.channel, self.K, bias=False)
# self.conv1 = ConvBnPrelu(3, 12, kernel=(3, 3), stride=1, padding=1)
# self.conv2 = ConvBn(12, 12, kernel=(3, 3), stride=1, padding=1, groups=12)
# self.conv3 = DepthWise(12, 12, kernel=(3, 3), stride=2, padding=1, groups=12)
# self.conv4 = MultiDepthWiseRes(num_block=6, channels=12, kernel=3, stride=1, padding=1, groups=12)
# self.conv5 = ConvBn(12, 12, groups=12, kernel=(3, 3))
# self.flatten = Flatten()
# self.linear = nn.Linear(312, output_dim, bias=False)
# def forward(self, x):
# residual = x
# out = self.conv1(x)
# out = self.conv2(out)
# out = self.conv3(out)
# out = self.conv4(out)
# out = self.conv5(out)
# out = self.flatten(out)
# out = self.linear(out)
# return out
class Net_dueling_DDQN(nn.Module):
def __init__(self,input_dim, output_dim):
"""channel size M = search space number N = RF chain number"""
self.channel = input_dim[0]
self.M = input_dim[1]
self.K = input_dim[2]
super().__init__()
self.conv1 = nn.Conv2d(3, 6, (2,2), stride=1)
self.BN1 = nn.BatchNorm2d(6)
self.conv2 = ConvBn(6, 8,(2, 2), stride=1)
self.BN2 = nn.BatchNorm2d((8))
self.flatten = Flatten()
self.linear1 = nn.Linear(1344, 800)
self.linear2 = nn.Linear(800, 300)
self.linear3 = nn.Linear(300, output_dim, bias=False)
def forward(self, x):
residual = x
out = self.conv1(x)
out = F.relu(out)
out = self.BN1(out)
out = self.conv2(out)
out = F.relu(out)
out = self.BN2(out)
out = self.flatten(out)
out = self.linear1(out)
out = F.relu(out)
out = self.linear2(out)
out = F.relu(out)
out = self.linear3(out)
return out
class Net_actor(nn.Module):
def __init__(self, M_bar, K):
"""channel size M = search space number N = RF chain number"""
self.M = M_bar
self.K = K
output_dim = M_bar
super().__init__()
self.res1 = nn.Linear(self.M*self.K*3, self.K, bias=False)
self.conv1 = ConvBnPrelu(3, 12, kernel=(3, 3), stride=1, padding=1)
self.conv2 = ConvBn(12, 12, kernel=(3, 3), stride=1, padding=1, groups=12)
self.conv3 = DepthWise(12, 12, kernel=(3, 3), stride=2, padding=1, groups=12)
self.conv4 = MultiDepthWiseRes(num_block=4, channels=12, kernel=3, stride=1, padding=1, groups=12)
self.conv5 = ConvBn(12, 12, groups=12, kernel=(3, 3))
self.flatten = Flatten()
self.linear = nn.Linear(1296, output_dim, bias=False)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
out = self.conv4(out)
out = self.conv5(out)
out = self.flatten(out)
out = self.linear(out)
out = torch.sigmoid(out)
return out
class Net_critic(nn.Module):
def __init__(self, M_bar, K, output_dim=1):
"""channel size M = search space number N = RF chain number"""
self.M = M_bar
self.K = K
super().__init__()
self.res1 = nn.Linear(self.M*self.K*3, self.K, bias=False)
self.conv1 = ConvBnPrelu(3, 12, kernel=(3, 3), stride=1, padding=1)
self.conv2 = ConvBn(12, 12, kernel=(3, 3), stride=1, padding=1, groups=12)
self.conv3 = DepthWise(12, 12, kernel=(3, 3), stride=2, padding=1, groups=12)
self.conv4 = MultiDepthWiseRes(num_block=4, channels=12, kernel=3, stride=1, padding=1, groups=12)
self.conv5 = ConvBn(12, 12, groups=12, kernel=(3, 3))
self.flatten = Flatten()
self.linear = nn.Linear(1008, output_dim, bias=False)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
out = self.conv4(out)
out = self.conv5(out)
out = self.flatten(out)
out = self.linear(out)
out = torch.sigmoid(out)
return out