nested.py 940 Bytes
Newer Older
Yuge Zhang's avatar
Yuge Zhang committed
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import torch.nn as nn
import torch.nn.functional as F

from nni.nas.pytorch.mutables import LayerChoice, InputChoice


class MutableOp(nn.Module):
    def __init__(self, kernel_size):
        super().__init__()
        self.conv = nn.Conv2d(3, 120, kernel_size, padding=kernel_size // 2)
        self.nested_mutable = InputChoice(n_candidates=10)

    def forward(self, x):
        return self.conv(x)


class NestedSpace(nn.Module):
    # this doesn't pass tests
    def __init__(self, test_case):
        super().__init__()
        self.test_case = test_case
        self.conv1 = LayerChoice([MutableOp(3), MutableOp(5)])
        self.gap = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Linear(120, 10)

    def forward(self, x):
        bs = x.size(0)
        x = F.relu(self.conv1(x))
        x = self.gap(x).view(bs, -1)
        x = self.fc(x)
        return x