示例#1
0
 def __init__(self, flag):
     super().__init__()
     if flag:
         self.a = None
         self.a = Linear(3, 5)
     else:
         self.a = Linear(3, 5)
         self.a = None
示例#2
0
    def __init__(self):
        self.mid_layers = 14
        self.num_class = 2
        super().__init__()

        self.fc0 = Linear(self.num_class, self.mid_layers, bias=True)
        self.fc1 = Linear(self.mid_layers, self.mid_layers, bias=True)

        self.fc2 = Linear(self.mid_layers, self.num_class, bias=True)
示例#3
0
def test_sequential_named_children():
    modules = OrderedDict()
    modules["name0"] = Linear(20, 10)
    modules["name1"] = Linear(10, 5)
    modules["name2"] = Linear(5, 1)
    m = Sequential(modules)
    l = list(m.named_children())
    assert l[0][0] == "name0"
    assert l[1][0] == "name1"
    assert l[2][0] == "name2"
示例#4
0
 def __init__(self, has_bn=False):
     super().__init__()
     self.conv0 = Conv2d(1, 20, kernel_size=5, bias=True)
     self.pool0 = AvgPool2d(2)
     self.conv1 = Conv2d(20, 20, kernel_size=5, bias=True)
     self.pool1 = AvgPool2d(2)
     self.fc0 = Linear(20 * 4 * 4, 500, bias=True)
     self.fc1 = Linear(500, 10, bias=True)
     self.bn0 = None
     self.bn1 = None
     if has_bn:
         self.bn0 = BatchNorm2d(20)
         self.bn1 = BatchNorm2d(20)
示例#5
0
 def __init__(self):
     super().__init__()
     self.conv1 = Conv2d(3, 128, 3, padding=1, bias=False)
     self.conv2 = Conv2d(3, 128, 3, dilation=2, bias=False)
     self.bn1 = BatchNorm1d(128)
     self.bn2 = BatchNorm2d(128)
     self.pooling = MaxPool2d(kernel_size=2, padding=0)
     modules = OrderedDict()
     modules["depthwise"] = Conv2d(
         256,
         256,
         3,
         1,
         0,
         groups=256,
         bias=False,
     )
     modules["pointwise"] = Conv2d(
         256,
         256,
         kernel_size=1,
         stride=1,
         padding=0,
         bias=True,
     )
     self.submodule1 = Sequential(modules)
     self.list1 = [Dropout(drop_prob=0.1), [Softmax(axis=100)]]
     self.tuple1 = (
         Dropout(drop_prob=0.1),
         (Softmax(axis=100), Dropout(drop_prob=0.2)),
     )
     self.dict1 = {"Dropout": Dropout(drop_prob=0.1)}
     self.fc1 = Linear(512, 1024)
示例#6
0
    def __init__(self, config):
        super().__init__()
        if config.hidden_size % config.num_attention_heads != 0:
            raise ValueError(
                "The hidden size (%d) is not a multiple of the number of attention "
                "heads (%d)" % (config.hidden_size, config.num_attention_heads)
            )
        self.num_attention_heads = config.num_attention_heads
        self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
        self.all_head_size = self.num_attention_heads * self.attention_head_size

        self.query = Linear(config.hidden_size, self.all_head_size)
        self.key = Linear(config.hidden_size, self.all_head_size)
        self.value = Linear(config.hidden_size, self.all_head_size)

        self.dropout = Dropout(config.attention_probs_dropout_prob)
示例#7
0
 def __init__(self, config):
     super(BertIntermediate, self).__init__()
     self.dense = Linear(config.hidden_size, config.intermediate_size)
     if isinstance(config.hidden_act, str):
         self.intermediate_act_fn = ACT2FN[config.hidden_act]
     else:
         self.intermediate_act_fn = config.hidden_act
示例#8
0
 def __init__(self, config, num_labels, bert=None):
     if bert is None:
         self.bert = BertModel(config)
     else:
         self.bert = bert
     self.num_labels = num_labels
     self.dropout = Dropout(config.hidden_dropout_prob)
     self.classifier = Linear(config.hidden_size, num_labels)
示例#9
0
    def __init__(self, num_classes=1000, model_size='2.0x', group=None):
        super(ShuffleNetV1, self).__init__()
        print('model size is ', model_size)

        assert group is not None

        self.stage_repeats = [4, 8, 4]
        self.model_size = model_size
        if group == 3:
            if model_size == '0.5x':
                self.stage_out_channels = [-1, 12, 120, 240, 480]
            elif model_size == '1.0x':
                self.stage_out_channels = [-1, 24, 240, 480, 960]
            elif model_size == '1.5x':
                self.stage_out_channels = [-1, 24, 360, 720, 1440]
            elif model_size == '2.0x':
                self.stage_out_channels = [-1, 48, 480, 960, 1920]
            else:
                raise NotImplementedError
        elif group == 8:
            if model_size == '0.5x':
                self.stage_out_channels = [-1, 16, 192, 384, 768]
            elif model_size == '1.0x':
                self.stage_out_channels = [-1, 24, 384, 768, 1536]
            elif model_size == '1.5x':
                self.stage_out_channels = [-1, 24, 576, 1152, 2304]
            elif model_size == '2.0x':
                self.stage_out_channels = [-1, 48, 768, 1536, 3072]
            else:
                raise NotImplementedError

        # building first layer
        input_channel = self.stage_out_channels[1]
        self.first_conv = Sequential(
            ConvBnRelu2d(3, input_channel, 3, 2, 1, bias=False)
        )
        self.maxpool = MaxPool2d(kernel_size=3, stride=2, padding=1)

        self.features = []
        for idxstage in range(len(self.stage_repeats)):
            numrepeat = self.stage_repeats[idxstage]
            output_channel = self.stage_out_channels[idxstage + 2]

            for i in range(numrepeat):
                stride = 2 if i == 0 else 1
                first_group = idxstage == 0 and i == 0
                self.features.append(ShuffleV1Block(input_channel, output_channel,
                                                    group=group, first_group=first_group,
                                                    mid_channels=output_channel // 4, ksize=3, stride=stride))
                input_channel = output_channel

        self.features = Sequential(*self.features)
        self.quant = QuantStub()
        self.dequant = DequantStub()
        self.classifier = Sequential(Linear(self.stage_out_channels[-1], num_classes, bias=False))
        self._initialize_weights()
示例#10
0
    def __init__(self):
        self.mid_layers = 14
        self.num_class = 2
        super().__init__()

        self.fc0 = Linear(self.num_class, self.mid_layers, bias=True)
        fan_in, _ = init.calculate_fan_in_and_fan_out(self.fc0.weight)
        init.normal_(self.fc0.weight, std=np.sqrt(float(1.0) / fan_in))
        init.zeros_(self.fc0.bias)

        self.fc1 = Linear(self.mid_layers, self.mid_layers, bias=True)
        fan_in, _ = init.calculate_fan_in_and_fan_out(self.fc1.weight)
        init.normal_(self.fc1.weight, std=np.sqrt(float(1.0) / fan_in))
        init.zeros_(self.fc1.bias)

        self.fc2 = Linear(self.mid_layers, self.num_class, bias=True)
        fan_in, _ = init.calculate_fan_in_and_fan_out(self.fc2.weight)
        init.normal_(self.fc2.weight, std=np.sqrt(float(1.0) / fan_in))
        init.zeros_(self.fc2.bias)
示例#11
0
 def __init__(self):
     super().__init__()
     self.conv1 = Conv2d(3, 128, 3, stride=2, bias=False)
     self.conv2 = Conv2d(3, 128, 3, padding=1, bias=False)
     self.conv3 = Conv2d(3, 128, 3, dilation=2, bias=False)
     self.bn1 = BatchNorm2d(128)
     self.bn2 = BatchNorm1d(128)
     self.dropout = Dropout(drop_prob=0.1)
     self.softmax = Softmax(axis=100)
     self.pooling = MaxPool2d(kernel_size=2, padding=0)
     self.submodule1 = Sequential(Dropout(drop_prob=0.1), Softmax(axis=100),)
     self.fc1 = Linear(512, 1024)
示例#12
0
def test_calculate_fan_in_and_fan_out():
    l = Linear(in_features=3, out_features=8)
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 3
    assert fanout == 8

    with pytest.raises(ValueError):
        calculate_fan_in_and_fan_out(l.bias)

    l = Conv2d(in_channels=2, out_channels=3, kernel_size=(5, 7))
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 2 * 5 * 7
    assert fanout == 3 * 5 * 7
示例#13
0
def test_calculate_fan_in_and_fan_out():
    l = Linear(in_features=3, out_features=8)
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 3
    assert fanout == 8

    with pytest.raises(ValueError):
        calculate_fan_in_and_fan_out(l.bias)

    l = Conv1d(in_channels=2, out_channels=3, kernel_size=5)
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 2 * 5
    assert fanout == 3 * 5

    # FIXME: will be wrong for group conv1d
    # l = Conv1d(in_channels=2, out_channels=4, kernel_size=5, groups=2)
    # fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    # assert fanin == 2 // 2 * 5
    # assert fanout == 4 // 2 * 5

    l = Conv2d(in_channels=2, out_channels=3, kernel_size=(5, 7))
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 2 * 5 * 7
    assert fanout == 3 * 5 * 7

    l = Conv2d(in_channels=2, out_channels=4, kernel_size=(5, 7), groups=2)
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 2 // 2 * 5 * 7
    assert fanout == 4 // 2 * 5 * 7

    # FIXME: will be wrong for conv3d
    # l = Conv3d(in_channels=2, out_channels=3, kernel_size=(5, 7, 9))
    # fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    # assert fanin == 2 * 5 * 7 * 9
    # assert fanout == 3 * 5 * 7 * 9

    l = Conv3d(in_channels=2, out_channels=4, kernel_size=(5, 7, 9), groups=2)
    fanin, fanout = calculate_fan_in_and_fan_out(l.weight)
    assert fanin == 2 // 2 * 5 * 7 * 9
    assert fanout == 4 // 2 * 5 * 7 * 9
示例#14
0
 def __init__(self, config):
     super(BertPooler, self).__init__()
     self.dense = Linear(config.hidden_size, config.hidden_size)
     self.activation = F.tanh
示例#15
0
 def __init__(self, config):
     super().__init__()
     self.dense = Linear(config.hidden_size, config.hidden_size)
     self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12)
     self.dropout = Dropout(config.hidden_dropout_prob)
示例#16
0
 def __init__(self):
     super().__init__()
     self.dense0 = Linear(28, 50)
     self.dense1 = Linear(50, 20)
示例#17
0
 def __init__(self, config):
     super(BertOutput, self).__init__()
     self.dense = Linear(config.intermediate_size, config.hidden_size)
     self.LayerNorm = BertLayerNorm(config.hidden_size, eps=1e-12)
     self.dropout = Dropout(config.hidden_dropout_prob)
示例#18
0
 def __init__(self):
     super().__init__()
     self.fc0 = Linear(3 * 224 * 224, 500)
     self.fc1 = Linear(500, 10)
示例#19
0
    def __init__(self, dim1=16, dim2=128, dim3=1):
        super().__init__()

        self.fc1 = Linear(dim1, dim2)
        self.fc2 = Linear(dim2, dim3)