-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixed_op.py
165 lines (131 loc) · 5.77 KB
/
mixed_op.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
"""
Created on Sat May 8 19:12:40 2021
@author: HP
"""
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from candidate_op import BasicOp, create_op_first, create_op_second
from mode import Mode
#import candidate_op
class MixedOp_first(BasicOp):
def __init__(self):
super(MixedOp_first, self).__init__()
self._candidate_op_cell = ['TG', 'SG','TG+SG', 'SG+TG']
self._num_ops = len(self._candidate_op_cell)
self._candidate_ops = nn.ModuleList()
for op_name in self._candidate_op_cell:
self._candidate_ops += [create_op_first(op_name)]
self._candidate_alphas = nn.Parameter(torch.zeros(self._num_ops),
requires_grad=True)
self.set_mode(Mode.ONE_PATH_FIXED)
def set_mode(self, mode):
self._mode = mode
if mode == Mode.NONE:
self._sample_idx = None
elif mode == Mode.ONE_PATH_FIXED:
probs = F.softmax(self._candidate_alphas.data, dim=0)
op = torch.argmax(probs).item()
self._sample_idx = np.array([op], dtype=np.int32)
elif mode == Mode.ONE_PATH_RANDOM:
probs = F.softmax(self._candidate_alphas.data, dim=0)
self._sample_idx = torch.multinomial(
probs, 1, replacement=True).cpu().numpy()
elif mode == Mode.TWO_PATHS:
probs = F.softmax(self._candidate_alphas.data, dim=0)
self._sample_idx = torch.multinomial(
probs, 2, replacement=True).cpu().numpy()
elif mode == Mode.ALL_PATHS:
self._sample_idx = np.arange(self._num_ops)
def forward(self, adj_list):
probs = F.softmax(self._candidate_alphas[self._sample_idx], dim=0)
output = 0
for i, idx in enumerate(self._sample_idx):
temp = self._candidate_ops[idx](adj_list)
output = output + probs[i] * temp
return output
def arch_parameters(self):
yield self._candidate_alphas
def weight_parameters(self):
for i in range(self._num_ops):
for p in self._candidate_ops[i].parameters():
yield p
def __repr__(self):
out_str = ''
out_str += 'mode: ' + str(self._mode) + str(self._sample_idx) + ',\n'
probs = F.softmax(self._candidate_alphas.data, dim=0)
for i in range(self._num_ops):
out_str += 'op:%d, prob: %.3f, info: %s,' % (
i, probs[i].item(), self._candidate_ops[i])
if i + 1 < self._num_ops:
out_str += '\n'
from helper import add_indent
out_str = 'mixed_op {\n%s\n}' % add_indent(out_str, 4)
return out_str
def render_name(self):
probs = F.softmax(self._candidate_alphas.data, dim=0)
index = torch.argmax(probs).item()
out_str = self._candidate_ops[index].type
out_str = '%s(%.2f)' % (out_str, probs[index])
return out_str
class MixedOp_second(BasicOp):
def __init__(self):
super(MixedOp_second, self).__init__()
self._candidate_op_cell = ['TG', 'SG', 'TC']
self._num_ops = len(self._candidate_op_cell)
self._candidate_ops = nn.ModuleList()
for op_name in self._candidate_op_cell:
self._candidate_ops += [create_op_second(op_name)]
self._candidate_alphas = nn.Parameter(torch.zeros(self._num_ops),
requires_grad=True)
self.set_mode(Mode.ONE_PATH_FIXED)
def set_mode(self, mode):
self._mode = mode
if mode == Mode.NONE:
self._sample_idx = None
elif mode == Mode.ONE_PATH_FIXED:
probs = F.softmax(self._candidate_alphas.data, dim=0)
op = torch.argmax(probs).item()
self._sample_idx = np.array([op], dtype=np.int32)
elif mode == Mode.ONE_PATH_RANDOM:
probs = F.softmax(self._candidate_alphas.data, dim=0)
self._sample_idx = torch.multinomial(
probs, 1, replacement=True).cpu().numpy()
elif mode == Mode.TWO_PATHS:
probs = F.softmax(self._candidate_alphas.data, dim=0)
self._sample_idx = torch.multinomial(
probs, 2, replacement=True).cpu().numpy()
elif mode == Mode.ALL_PATHS:
self._sample_idx = np.arange(self._num_ops)
def forward(self, adj_list):
probs = F.softmax(self._candidate_alphas[self._sample_idx], dim=0)
output = 0
for i, idx in enumerate(self._sample_idx):
temp = self._candidate_ops[idx](adj_list)
output = output + probs[i] * temp
return output
def arch_parameters(self):
yield self._candidate_alphas
def weight_parameters(self):
for i in range(self._num_ops):
for p in self._candidate_ops[i].parameters():
yield p
def __repr__(self):
out_str = ''
out_str += 'mode: ' + str(self._mode) + str(self._sample_idx) + ',\n'
probs = F.softmax(self._candidate_alphas.data, dim=0)
for i in range(self._num_ops):
out_str += 'op:%d, prob: %.3f, info: %s,' % (
i, probs[i].item(), self._candidate_ops[i])
if i + 1 < self._num_ops:
out_str += '\n'
from helper import add_indent
out_str = 'mixed_op {\n%s\n}' % add_indent(out_str, 4)
return out_str
def render_name(self):
probs = F.softmax(self._candidate_alphas.data, dim=0)
index = torch.argmax(probs).item()
out_str = self._candidate_ops[index].type
out_str = '%s(%.2f)' % (out_str, probs[index])
return out_str