(self, k, mutation_num, m_prob, s_prob)
| 152 | print('random_num = {}'.format(len(self.candidates))) |
| 153 | |
| 154 | def get_mutation(self, k, mutation_num, m_prob, s_prob): |
| 155 | assert k in self.keep_top_k |
| 156 | print('mutation ......') |
| 157 | res = [] |
| 158 | iter = 0 |
| 159 | max_iters = mutation_num * 10 |
| 160 | |
| 161 | def random_func(): |
| 162 | cand = list(random.choice(self.keep_top_k[k])) |
| 163 | depth, mlp_ratio, num_heads, embed_dim = decode_cand_tuple(cand) |
| 164 | random_s = random.random() |
| 165 | |
| 166 | # depth |
| 167 | if random_s < s_prob: |
| 168 | new_depth = random.choice(self.choices['depth']) |
| 169 | |
| 170 | if new_depth > depth: |
| 171 | mlp_ratio = mlp_ratio + [random.choice(self.choices['mlp_ratio']) for _ in range(new_depth - depth)] |
| 172 | num_heads = num_heads + [random.choice(self.choices['num_heads']) for _ in range(new_depth - depth)] |
| 173 | else: |
| 174 | mlp_ratio = mlp_ratio[:new_depth] |
| 175 | num_heads = num_heads[:new_depth] |
| 176 | |
| 177 | depth = new_depth |
| 178 | # mlp_ratio |
| 179 | for i in range(depth): |
| 180 | random_s = random.random() |
| 181 | if random_s < m_prob: |
| 182 | mlp_ratio[i] = random.choice(self.choices['mlp_ratio']) |
| 183 | |
| 184 | # num_heads |
| 185 | |
| 186 | for i in range(depth): |
| 187 | random_s = random.random() |
| 188 | if random_s < m_prob: |
| 189 | num_heads[i] = random.choice(self.choices['num_heads']) |
| 190 | |
| 191 | # embed_dim |
| 192 | random_s = random.random() |
| 193 | if random_s < s_prob: |
| 194 | embed_dim = random.choice(self.choices['embed_dim']) |
| 195 | |
| 196 | result_cand = [depth] + mlp_ratio + num_heads + [embed_dim] |
| 197 | |
| 198 | return tuple(result_cand) |
| 199 | |
| 200 | cand_iter = self.stack_random_cand(random_func) |
| 201 | while len(res) < mutation_num and max_iters > 0: |
| 202 | max_iters -= 1 |
| 203 | cand = next(cand_iter) |
| 204 | if not self.is_legal(cand): |
| 205 | continue |
| 206 | res.append(cand) |
| 207 | print('mutation {}/{}'.format(len(res), mutation_num)) |
| 208 | |
| 209 | print('mutation_num = {}'.format(len(res))) |
| 210 | return res |
| 211 |
no test coverage detected