Python chainer 模块,links() 实例源码

我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用chainer.links()

项目:chainer-speech-recognition    作者:musyoku    | 项目源码 | 文件源码
def _set_module(self, namespace, module):
        assert isinstance(module, Module)

        for index, layer in enumerate(module.layers):
            if isinstance(layer, chainer.Link):
                super(Module, self).__setattr__("_module_{}_sequential_{}".format(namespace, index), layer)

            if isinstance(layer, Residual):
                for _index, _layer in enumerate(layer.layers):
                    if isinstance(_layer, chainer.Link):
                        super(Module, self).__setattr__("_module_{}_sequential_{}_{}".format(namespace, index, _index), _layer)

        for index, (link_name, link) in enumerate(module.links):
            assert isinstance(link, chainer.Link)
            super(Module, self).__setattr__("_module_{}_link_{}".format(namespace, link_name), link)

        for index, (module_name, module) in enumerate(module.modules):
            assert isinstance(module, Module)
            self._set_module("{}_{}".format(namespace, module_name), module)

        module._locked = True
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_convolution(self):
        self.init_func()
        self.assertEqual(len(self.func.layers), 1)
        f = self.func.l1
        self.assertIsInstance(f, links.Convolution2D)
        for i in range(3):  # 3 == group
            in_slice = slice(i * 4, (i + 1) * 4)  # 4 == channels
            out_slice = slice(i * 2, (i + 1) * 2)  # 2 == num / group
            w = f.W.data[out_slice, in_slice]
            numpy.testing.assert_array_equal(
                w.flatten(), range(i * 32, (i + 1) * 32))

        numpy.testing.assert_array_equal(
            f.b.data, range(6))

        self.call(['x'], ['y'])
        self.mock.assert_called_once_with(self.inputs[0])
项目:convolutional_seq2seq    作者:soskek    | 项目源码 | 文件源码
def __init__(self, n_layers, n_units, width=3, dropout=0.2):
        super(ConvGLUDecoder, self).__init__()
        links = [('l{}'.format(i + 1),
                  ConvGLU(n_units, width=width,
                          dropout=dropout, nopad=True))
                 for i in range(n_layers)]
        for link in links:
            self.add_link(*link)
        self.conv_names = [name for name, _ in links]
        self.width = width

        init_preatt = VarInNormal(1.)
        links = [('preatt{}'.format(i + 1),
                  L.Linear(n_units, n_units, initialW=init_preatt))
                 for i in range(n_layers)]
        for link in links:
            self.add_link(*link)
        self.preatt_names = [name for name, _ in links]
项目:chainer-faster-rcnn    作者:mitmul    | 项目源码 | 文件源码
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 link in links:
            self.add_link(*link)
        self.forward = links
项目:gym-malware    作者:endgameinc    | 项目源码 | 文件源码
def create_acer_agent(env):
    obs_dim = env.observation_space.shape[0]
    n_actions = env.action_space.n

    model = acer.ACERSeparateModel(
        pi=links.Sequence(
            L.Linear( obs_dim, 1024, initialW=LeCunNormal(1e-3)),
            F.relu,
            L.Linear( 1024, 512, initialW=LeCunNormal(1e-3)),
            F.relu,
            L.Linear( 512, n_actions, initialW=LeCunNormal(1e-3)),
            SoftmaxDistribution),
        q=links.Sequence(
            L.Linear( obs_dim, 1024, initialW=LeCunNormal(1e-3)),
            F.relu,
            L.Linear( 1024, 512, initialW=LeCunNormal(1e-3)),
            F.relu,
            L.Linear( 512, n_actions, initialW=LeCunNormal(1e-3)),
            DiscreteActionValue),
        )

    opt = rmsprop_async.RMSpropAsync( lr=7e-4, eps=1e-2, alpha=0.99)
    opt.setup( model )
    opt.add_hook( chainer.optimizer.GradientClipping(40) )

    replay_buffer = EpisodicReplayBuffer( 128 )
    agent = acer.ACER( model, opt, 
        gamma=0.95, # reward discount factor
        t_max=32, # update the model after this many local steps
        replay_buffer=replay_buffer,
        n_times_replay=4, # number of times experience replay is repeated for each update
        replay_start_size=64, # don't start replay unless we have this many experiences in the buffer
        disable_online_update=True, # rely only on experience buffer
        use_trust_region=True,  # enable trust region policy optimiztion
        trust_region_delta=0.1,  # a parameter for TRPO
        truncation_threshold=5.0, # truncate large importance weights
        beta=1e-2, # entropy regularization parameter
        phi= lambda obs: obs.astype(np.float32, copy=False) )

    return agent
项目:chainer-segnet    作者:pfnet-research    | 项目源码 | 文件源码
def remove_link(self, name):
        """Remove a link that has the given name from this model

        Optimizer sees ``~Chain.namedparams()`` to know which parameters should
        be updated. And inside of ``namedparams()``, ``self._children`` is
        called to get names of all links included in the Chain.
        """
        self._children.remove(name)
项目:neural_style_synthesizer    作者:dwango    | 项目源码 | 文件源码
def convert_debug(self, content_img, init_img, output_directory, max_iteration=1000, debug_span=100,
                      random_init=False):
        init_array = self.xp.array(neural_art.utility.img2array(init_img))
        if random_init:
            init_array = self.xp.array(self.xp.random.uniform(-20, 20, init_array.shape), dtype=init_array.dtype)
        content_array = self.xp.array(neural_art.utility.img2array(content_img))
        content_layers = self.model.forward_layers(chainer.Variable(content_array),
                                                   average_pooling=self.average_pooling)

        parameter_now = chainer.links.Parameter(init_array)
        self.optimizer.setup(parameter_now)
        for i in range(max_iteration + 1):
            neural_art.utility.print_ltsv({"iteration": i})
            if i % debug_span == 0 and i > 0:
                print("dump to {}".format(os.path.join(output_directory, "{}.png".format(i))))
                neural_art.utility.array2img(chainer.cuda.to_cpu(parameter_now.W.data)).save(
                    os.path.join(output_directory, "{}.png".format(i)))
            parameter_now.zerograds()
            x = parameter_now.W
            layers = self.model.forward_layers(x, average_pooling=self.average_pooling)

            loss_texture = self._texture_loss(layers)
            loss_content = self._contents_loss(layers, content_layers)
            loss = self.texture_weight * loss_texture + self.content_weight * loss_content
            loss.backward()
            parameter_now.W.grad = x.grad
            self.optimizer.update()
        return neural_art.utility.array2img(chainer.cuda.to_cpu(parameter_now.W.data))
项目:chainer-stack-gan    作者:dsanno    | 项目源码 | 文件源码
def __call__(self, x, train=True):
        h = x
        links = self.children()
        for link in links:
            h = link(h, train=train)
        return h
项目:chainer-stack-gan    作者:dsanno    | 项目源码 | 文件源码
def __call__(self, x, train=True):
        links = self.children()
        h = F.leaky_relu(next(links)(x))
        for link in links:
            h = link(h, train)
        return h
项目:chainer-speech-recognition    作者:musyoku    | 项目源码 | 文件源码
def Convolution2D(in_channel, out_channel, ksize, stride=1, pad=0, initialW=None, weightnorm=False):
    if weightnorm:
        return WeightnormConvolution2D(in_channel, out_channel, ksize, stride=1, pad=pad, initialV=initialW)
    return links.Convolution2D(in_channel, out_channel, ksize, stride=1, pad=pad, initialW=initialW)
项目:chainer-speech-recognition    作者:musyoku    | 项目源码 | 文件源码
def __init__(self, *layers):
        super(Module, self).__init__()
        self.layers = []
        self.blocks = []
        self.links = []
        self.modules = []
        self._locked = False
        if len(layers) > 0:
            self.add(*layers)
项目:chainer-speech-recognition    作者:musyoku    | 项目源码 | 文件源码
def __setattr__(self, name, value):
        if isinstance(value, Module):
            self.modules.append((name, value))
            self._set_module(name, value)
            return super(chainer.Link, self).__setattr__(name, value)   # prevent module from being added to self._children

        if isinstance(value, chainer.Link):
            assert self._locked is False, "Since this module is owned by another module, it is not possible to add Link."
            with self.init_scope():
                if name.startswith("_sequential_"):
                    return super(Module, self).__setattr__(name, value)
                self.links.append((name, value))
                return super(Module, self).__setattr__(name, value)

        super(Module, self).__setattr__(name, value)
项目:chainer-qrnn    作者:musyoku    | 项目源码 | 文件源码
def __init__(self, in_channels, out_channels, kernel_size=2, pooling="f", zoneout=False, wgain=1., weightnorm=False):
        super(QRNNDecoder, self).__init__(in_channels, out_channels, kernel_size, pooling, zoneout, wgain, weightnorm)
        self.num_split = len(pooling) + 1
        wstd = math.sqrt(wgain / in_channels / kernel_size)

        with self.init_scope():
            setattr(self, "V", links.Linear(out_channels, self.num_split * out_channels, initialW=initializers.Normal(wstd)))

    # ht_enc is the last encoder state
项目:chainer-qrnn    作者:musyoku    | 项目源码 | 文件源码
def __init__(self, in_channels, out_channels, kernel_size=2, zoneout=False, wgain=1., weightnorm=False):
        super(QRNNGlobalAttentiveDecoder, self).__init__(in_channels, out_channels, kernel_size, "fo", zoneout, wgain, weightnorm)
        wstd = math.sqrt(wgain / in_channels / kernel_size)
        with self.init_scope():
            setattr(self, "o", links.Linear(2 * out_channels, out_channels, initialW=initializers.Normal(wstd)))

    # X is the input of the decoder
    # ht_enc is the last encoder state
    # H_enc is the encoder's las layer's hidden sates
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def links(self, skipself=False):
        """Returns a generator of all links under the hierarchy.

        Args:
            skipself (bool): If ``True``, then the generator skips this link
                and starts with the first child link.

        Returns:
            A generator object that generates all links.

        """
        if not skipself:
            yield self
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def children(self):
        """Returns a generator of all child links.

        Returns:
            A generator object that generates all child links.

        """
        if 0:
            yield
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def __init__(self, **links):
        super(Chain, self).__init__()
        self._children = []

        for name, link in six.iteritems(links):
            self.add_link(name, link)
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def copy(self):
        ret = super(Chain, self).copy()
        ret._children = list(ret._children)
        d = ret.__dict__
        for name in ret._children:
            # copy child links recursively
            copied = d[name].copy()
            copied.name = name
            d[name] = copied
        return ret
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def links(self, skipself=False):
        if not skipself:
            yield self
        d = self.__dict__
        for name in self._children:
            for link in d[name].links():
                yield link
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def links(self, skipself=False):
        if not skipself:
            yield self
        for child in self._children:
            for link in child.links():
                yield link
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_linear(self):
        self.init_func()
        f = self.func.l1
        self.assertIsInstance(f, links.Linear)
        numpy.testing.assert_array_equal(
            f.W.data, numpy.array([[0, 1, 2], [3, 4, 5]], dtype=numpy.float32))
        numpy.testing.assert_array_equal(
            f.b.data, numpy.array([0, 1], dtype=numpy.float32))

        self.call(['x'], ['y'])
        self.mock.assert_called_once_with(self.inputs[0])
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_linear(self):
        self.init_func()
        f = self.func.l1
        self.assertIsInstance(f, links.Linear)
        numpy.testing.assert_array_equal(
            f.W.data, numpy.array([[0, 1, 2], [3, 4, 5]], dtype=numpy.float32))
        self.assertIsNone(f.b)

        self.call(['x'], ['y'])
        self.mock.assert_called_once_with(self.inputs[0])
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_py2_available(self):
        self.assertTrue(links.caffe.caffe_function.available)
项目:deep_metric_learning    作者:ronekko    | 项目源码 | 文件源码
def __init__(self, out_dims=64, normalize_output=False):
        super(ModifiedGoogLeNet, self).__init__()
        # remove links and functions
        for name in [n for n in self._children if n.startswith('loss')]:
            self._children.remove(name)
            delattr(self, name)
        self.functions.pop('loss3_fc')
        self.functions.pop('prob')

        self.add_link('bn_fc', L.BatchNormalization(1024))
        self.add_link('fc', L.Linear(1024, out_dims))

        image_mean = np.array([123, 117, 104], dtype=np.float32)  # RGB
        self._image_mean = image_mean[None, :, None, None]
        self.normalize_output = normalize_output
项目:convolutional_seq2seq    作者:soskek    | 项目源码 | 文件源码
def __init__(self, n_layers, n_units, width=3, dropout=0.2):
        super(ConvGLUEncoder, self).__init__()
        links = [('l{}'.format(i + 1),
                  ConvGLU(n_units, width=width, dropout=dropout))
                 for i in range(n_layers)]
        for link in links:
            self.add_link(*link)
        self.conv_names = [name for name, _ in links]
项目:jrm_ssl    作者:Fhrozen    | 项目源码 | 文件源码
def __init__(self, layer, in_size, ch, out_size, stride=2, act=F.elu):
        super(Block, self).__init__()
        links = [('a', BottleNeckA(in_size, ch, out_size, stride, act))]
        for i in range(layer-1):
            links += [('b{}'.format(i+1), BottleNeckB(out_size, ch, act))]

        for link in links:
            self.add_link(*link)
        self.forward = links
项目:convolutional-pose-machines-chainer    作者:tomoyukun    | 项目源码 | 文件源码
def __init__(self, n_point, n_stage):
        super(CPM, self).__init__()
        self.add_link('branch', Branch())
        self.add_link('stage1', Stage1(n_point))
        links = []
        for i in xrange(n_stage-1):
            links += [('stage{}'.format(i+2), StageN(n_point))]
        for l in links:
            self.add_link(*l)

        self.forward = links
        self.train = True
项目:convolutional-pose-machines-chainer    作者:tomoyukun    | 项目源码 | 文件源码
def __init__(self, n_point, n_stage):
        super(CPM, self).__init__()
        self.add_link('branch', Branch())
        self.add_link('stage1', Stage1(n_point))
        links = []
        for i in xrange(n_stage-1):
            links += [('stage{}'.format(i+2), StageN(n_point))]
        for l in links:
            self.add_link(*l)

        self.forward = links
        self.train = True
项目:vsmlib    作者:undertherain    | 项目源码 | 文件源码
def charRNN(self, context):  # input a list of word ids, output a list of word embeddings
        # if chainer.config.train:
        #     print("train")
        # else:
        #     print("test")
        contexts2charIds = self.index2charIds[context]

        #sorting the context_char, make sure array length in descending order
        # ref: https://docs.chainer.org/en/stable/reference/generated/chainer.links.LSTM.html?highlight=Variable-length
        context_char_length = np.array([len(t) for t in contexts2charIds])
        argsort = context_char_length.argsort()[::-1] # descending order
        argsort_reverse = np.zeros(len(argsort), dtype=np.int32)  # this is used to restore the original order
        for i in range(len(argsort)):
            argsort_reverse[argsort[i]] = i
        contexts2charIds = contexts2charIds[context_char_length.argsort()[::-1]]

        #transpose a 2D list/numpy array
        rnn_inputs = [[] for i in range(len(contexts2charIds[0]))]
        for j in range(len(contexts2charIds)) :
            for i in range(len(contexts2charIds[j])):
                rnn_inputs[i].append(contexts2charIds[j][i])

        self.reset_state()
        for i in range(len(rnn_inputs)):
            y_ = self(np.array(rnn_inputs[i], np.int32))
        y = self.out(self.mid.h)
        y = y[argsort_reverse] # restore the original order
        return y
项目:vsmlib    作者:undertherain    | 项目源码 | 文件源码
def charRNN(self, context):  # input a list of word ids, output a list of word embeddings
        # if chainer.config.train:
        #     print("train")
        # else:
        #     print("test")
        contexts2charIds = index2charIds[context]

        #sorting the context_char, make sure array length in descending order
        # ref: https://docs.chainer.org/en/stable/reference/generated/chainer.links.LSTM.html?highlight=Variable-length
        context_char_length = np.array([len(t) for t in contexts2charIds])
        argsort = context_char_length.argsort()[::-1] # descending order
        argsort_reverse = np.zeros(len(argsort), dtype=np.int32)  # this is used to restore the original order
        for i in range(len(argsort)):
            argsort_reverse[argsort[i]] = i
        contexts2charIds = contexts2charIds[context_char_length.argsort()[::-1]]

        #transpose a 2D list/numpy array
        rnn_inputs = [[] for i in range(len(contexts2charIds[0]))]
        for j in range(len(contexts2charIds)) :
            for i in range(len(contexts2charIds[j])):
                rnn_inputs[i].append(contexts2charIds[j][i])

        self.reset_state()
        for i in range(len(rnn_inputs)):
            y_ = self(np.array(rnn_inputs[i], np.int32))
        y = self.out(self.mid.h)
        y = y[argsort_reverse] # restore the original order
        return y
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
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
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
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
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
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
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
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),
        )
项目:chainer-examples    作者:nocotan    | 项目源码 | 文件源码
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
项目:chainer-examples    作者:nocotan    | 项目源码 | 文件源码
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
项目:cgp-cnn    作者:sg-nm    | 项目源码 | 文件源码
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
项目:cgp-cnn    作者:sg-nm    | 项目源码 | 文件源码
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
项目:chainer-caption    作者:apple2373    | 项目源码 | 文件源码
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
项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
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)
项目:chainer-fcis    作者:knorth55    | 项目源码 | 文件源码
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')