python类links()的实例源码

seq2seq_mp1.py 文件源码 项目:chainermn 作者: chainer 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(
            self, comm, n_layers, n_source_vocab, n_target_vocab, n_units):

        super(Encoder, self).__init__(
            embed_x=L.EmbedID(n_source_vocab, n_units),
            # Corresponding decoder LSTM will be invoked on process 1.
            mn_encoder=chainermn.links.create_multi_node_n_step_rnn(
                L.NStepLSTM(n_layers, n_units, n_units, 0.1),
                comm, rank_in=None, rank_out=1
            ),
        )
        self.comm = comm
        self.n_layers = n_layers
        self.n_units = n_units
seq2seq_mp1.py 文件源码 项目:chainermn 作者: chainer 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __init__(
            self, comm, n_layers, n_source_vocab, n_target_vocab, n_units):
        super(Decoder, self).__init__(
            embed_y=L.EmbedID(n_target_vocab, n_units),
            # Corresponding encoder LSTM will be invoked on process 0.
            mn_decoder=chainermn.links.create_multi_node_n_step_rnn(
                L.NStepLSTM(n_layers, n_units, n_units, 0.1),
                comm, rank_in=0, rank_out=None),
            W=L.Linear(n_units, n_target_vocab),
        )
        self.comm = comm
        self.n_layers = n_layers
        self.n_units = n_units
resnet50.py 文件源码 项目:chainermn 作者: chainer 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def __init__(self, layer, in_size, ch, out_size, stride=2):
        super(Block, self).__init__()
        links = [('a', BottleNeckA(in_size, ch, out_size, stride))]
        for i in range(layer - 1):
            links += [('b{}'.format(i + 1), BottleNeckB(out_size, ch))]

        for l in links:
            self.add_link(*l)
        self.forward = links
test_communicator.py 文件源码 项目:chainermn 作者: chainer 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def __init__(self):
        super(ExampleModel, self).__init__(
            a=chainer.links.Linear(2, 3),
            b=chainer.links.Linear(3, 4),
            c=chainer.links.Linear(4, 5),
        )
resnext.py 文件源码 项目:chainer-examples 作者: nocotan 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__(self, in_size, ch, out_size, stride, card=32):
        super(Block, self).__init__()
        links = [(
            'c{}'.format(i+1),
            Cardinality(in_size, ch, out_size, stride)) for i in range(card)]
        links += [(
            'x_bypass',
            L.Convolution2D(in_size, out_size, 1, stride, 0, nobias=True))]

        for l in links:
            self.add_link(*l)

        self.forward = links
resnext.py 文件源码 项目:chainer-examples 作者: nocotan 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, layer, in_size, ch, out_size, stride=1):
        super(LaminationBlock, self).__init__()
        links = [('lb0', Block(in_size, ch, out_size, stride))]
        links += [('lb{}'.format(i+1),
                   Block(out_size, ch, out_size, 1)) for i in range(1, layer)]
        for l in links:
            self.add_link(*l)

        self.forward = links
cnn_model.py 文件源码 项目:cgp-cnn 作者: sg-nm 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self, ksize, n_out, initializer):
        super(ConvBlock, self).__init__()
        pad_size = ksize // 2
        links = [('conv1', L.Convolution2D(None, n_out, ksize, pad=pad_size, initialW=initializer))]
        links += [('bn1', L.BatchNormalization(n_out))]
        for link in links:
            self.add_link(*link)
        self.forward = links
cnn_model.py 文件源码 项目:cgp-cnn 作者: sg-nm 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __init__(self, ksize, n_out, initializer):
        super(ResBlock, self).__init__()
        pad_size = ksize // 2
        links  = [('conv1', L.Convolution2D(None, n_out, ksize, pad=pad_size, initialW=initializer))]
        links += [('bn1', L.BatchNormalization(n_out))]
        links += [('_act1', F.ReLU())]
        links += [('conv2', L.Convolution2D(n_out, n_out, ksize, pad=pad_size, initialW=initializer))]
        links += [('bn2', L.BatchNormalization(n_out))]
        for link in links:
            if not link[0].startswith('_'):
                self.add_link(*link)
        self.forward = links
ResNet50.py 文件源码 项目:chainer-caption 作者: apple2373 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def __init__(self, layer, in_size, ch, out_size, stride=2):
        super(Block, self).__init__()
        links = [('a', BottleNeckA(in_size, ch, out_size, stride))]
        for i in range(layer-1):
            links += [('b{}'.format(i+1), BottleNeckB(out_size, ch))]

        for l in links:
            self.add_link(*l)
        self.forward = links
test_async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def test_shared_link(self):
        """Check interprocess parameter sharing works if models share links"""

        head = L.Linear(2, 2)
        model_a = chainer.ChainList(head.copy(), L.Linear(2, 3))
        model_b = chainer.ChainList(head.copy(), L.Linear(2, 4))

        a_arrays = async.extract_params_as_shared_arrays(
            chainer.ChainList(model_a))
        b_arrays = async.extract_params_as_shared_arrays(
            chainer.ChainList(model_b))

        print(('model_a shared_arrays', a_arrays))
        print(('model_b shared_arrays', b_arrays))

        head = L.Linear(2, 2)
        model_a = chainer.ChainList(head.copy(), L.Linear(2, 3))
        model_b = chainer.ChainList(head.copy(), L.Linear(2, 4))

        async.set_shared_params(model_a, a_arrays)
        async.set_shared_params(model_b, b_arrays)

        print('model_a replaced')
        a_params = dict(model_a.namedparams())
        for param_name, param in list(a_params.items()):
            print((param_name, param.data.ctypes.data))

        print('model_b replaced')
        b_params = dict(model_b.namedparams())
        for param_name, param in list(b_params.items()):
            print((param_name, param.data.ctypes.data))

        # Pointers to head parameters must be the same
        self.assertEqual(a_params['/0/W'].data.ctypes.data,
                         b_params['/0/W'].data.ctypes.data)
        self.assertEqual(a_params['/0/b'].data.ctypes.data,
                         b_params['/0/b'].data.ctypes.data)

        # Pointers to tail parameters must be different
        self.assertNotEqual(a_params['/1/W'].data.ctypes.data,
                            b_params['/1/W'].data.ctypes.data)
        self.assertNotEqual(a_params['/1/b'].data.ctypes.data,
                            b_params['/1/b'].data.ctypes.data)
fcis_resnet101.py 文件源码 项目:chainer-fcis 作者: knorth55 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def init_weight(self, resnet101=None):
        if resnet101 is None:
            resnet101 = chainer.links.ResNet101Layers(pretrained_model='auto')

        n_layer_dict = {
            'res2': 3,
            'res3': 4,
            'res4': 23,
            'res5': 3
        }

        def copy_conv(conv, orig_conv):
            assert conv is not orig_conv
            assert conv.W.array.shape == orig_conv.W.array.shape
            conv.W.array[:] = orig_conv.W.array

        def copy_bn(bn, orig_bn):
            assert bn is not orig_bn
            assert bn.gamma.array.shape == orig_bn.gamma.array.shape
            assert bn.beta.array.shape == orig_bn.beta.array.shape
            assert bn.avg_var.shape == orig_bn.avg_var.shape
            assert bn.avg_mean.shape == orig_bn.avg_mean.shape
            bn.gamma.array[:] = orig_bn.gamma.array
            bn.beta.array[:] = orig_bn.beta.array
            bn.avg_var[:] = orig_bn.avg_var
            bn.avg_mean[:] = orig_bn.avg_mean

        def copy_bottleneck(bottle, orig_bottle, n_conv):
            for i in range(0, n_conv):
                conv_name = 'conv{}'.format(i + 1)
                conv = getattr(bottle, conv_name)
                orig_conv = getattr(orig_bottle, conv_name)
                copy_conv(conv, orig_conv)

                bn_name = 'bn{}'.format(i + 1)
                bn = getattr(bottle, bn_name)
                orig_bn = getattr(orig_bottle, bn_name)
                copy_bn(bn, orig_bn)

        def copy_block(block, orig_block, res_name):
            n_layer = n_layer_dict[res_name]
            bottle = getattr(block, '{}_a'.format(res_name))
            copy_bottleneck(bottle, orig_block.a, 4)
            for i in range(1, n_layer):
                bottle = getattr(block, '{0}_b{1}'.format(res_name, i))
                orig_bottle = getattr(orig_block, 'b{}'.format(i))
                copy_bottleneck(bottle, orig_bottle, 3)

        copy_conv(self.res1.conv1, resnet101.conv1)
        copy_bn(self.res1.bn1, resnet101.bn1)
        copy_block(self.res2, resnet101.res2, 'res2')
        copy_block(self.res3, resnet101.res3, 'res3')
        copy_block(self.res4, resnet101.res4, 'res4')
        copy_block(self.res5, resnet101.res5, 'res5')


问题


面经


文章

微信
公众号

扫码关注公众号