Python chainer.initializers() Examples
The following are 8
code examples of chainer.initializers().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer
, or try the search function
.
Example #1
Source File: test_communicator.py From chainer with MIT License | 5 votes |
def __init__(self, dtype=None): W = None bias = None if dtype is not None: self.dtype = dtype W = chainer.initializers.Normal(dtype=self.dtype) bias = chainer.initializers.Zero(dtype=self.dtype) super(ExampleModel, self).__init__() with self.init_scope(): self.a = chainer.links.Linear(2, 3, initialW=W, initial_bias=bias) self.b = chainer.links.Linear(3, 4, initialW=W, initial_bias=bias) self.c = chainer.links.Linear(None, 5, initialW=W, initial_bias=bias)
Example #2
Source File: test_communicator.py From chainer with MIT License | 5 votes |
def __init__(self): W16 = chainer.initializers.Normal(dtype=np.float16) W32 = chainer.initializers.Normal(dtype=np.float32) bias16 = chainer.initializers.Zero(dtype=np.float16) bias32 = chainer.initializers.Zero(dtype=np.float32) super(ExampleMixedModel, self).__init__() with self.init_scope(): self.a = chainer.links.Linear(2, 3, initialW=W32, initial_bias=bias32) self.b = chainer.links.Linear(3, 4, initialW=W16, initial_bias=bias16) self.c = chainer.links.Linear(None, 5, initialW=W16, initial_bias=bias32)
Example #3
Source File: resnet.py From chainer with MIT License | 5 votes |
def __init__(self, pretrained_model, n_layers, downsample_fb=False): super(ResNetLayers, self).__init__() if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. conv_kwargs = {'initialW': constant.Zero()} else: # employ default initializers used in the original paper conv_kwargs = {'initialW': normal.HeNormal(scale=1.0)} kwargs = conv_kwargs.copy() kwargs['downsample_fb'] = downsample_fb if n_layers == 50: block = [3, 4, 6, 3] elif n_layers == 101: block = [3, 4, 23, 3] elif n_layers == 152: block = [3, 8, 36, 3] else: raise ValueError('The n_layers argument should be either 50, 101,' ' or 152, but {} was given.'.format(n_layers)) with self.init_scope(): self.conv1 = Convolution2D(3, 64, 7, 2, 3, **conv_kwargs) self.bn1 = BatchNormalization(64) self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs) self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs) self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs) self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs) self.fc6 = Linear(2048, 1000) if pretrained_model and pretrained_model.endswith('.caffemodel'): _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers), pretrained_model, self) elif pretrained_model: npz.load_npz(pretrained_model, self)
Example #4
Source File: variable.py From chainer with MIT License | 5 votes |
def zerograd(self): super(Parameter, self).zerograd() if not self.is_initialized: dtype = getattr(self.initializer, 'dtype', None) self._grad_initializer = initializers.Zero(dtype)
Example #5
Source File: variable.py From chainer with MIT License | 5 votes |
def initialize(self, shape): """Initializes the uninitialized variable. Uninitialized variable is a variable created with the data array set to None. This method creates and initializes the data array. The shape of the variable can be left unknown until this method is called. Args: shape (tuple of int): Shape of the data array. """ device = self._initial_device assert device is not None xp = device.xp data = initializers.generate_array( self.initializer, shape, xp, device=device) data = chainer.memory_layouts._transpose_array(data, None, self.layout) if self._grad_initializer is None: grad = None else: grad = initializers.generate_array( self._grad_initializer, shape, xp, device=device) grad = chainer.memory_layouts._transpose_array( grad, None, self.layout) self._set_array(data, layout_check=False) self._set_grad(grad, layout_check=False) # Convert the array for iDeep. # TODO(niboshi): This could be done in generate_array(). if isinstance(self._initial_device, intel64.Intel64Device): self.to_intel64()
Example #6
Source File: vgg.py From chainer with MIT License | 4 votes |
def __init__(self, pretrained_model='auto', n_layers=16): super(VGGLayers, self).__init__() if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. init = constant.Zero() kwargs = {'initialW': init, 'initial_bias': init} else: # employ default initializers used in the original paper kwargs = { 'initialW': normal.Normal(0.01), 'initial_bias': constant.Zero(), } if n_layers not in [16, 19]: raise ValueError( 'The n_layers argument should be either 16 or 19, ' 'but {} was given.'.format(n_layers) ) with self.init_scope(): self.conv1_1 = Convolution2D(3, 64, 3, 1, 1, **kwargs) self.conv1_2 = Convolution2D(64, 64, 3, 1, 1, **kwargs) self.conv2_1 = Convolution2D(64, 128, 3, 1, 1, **kwargs) self.conv2_2 = Convolution2D(128, 128, 3, 1, 1, **kwargs) self.conv3_1 = Convolution2D(128, 256, 3, 1, 1, **kwargs) self.conv3_2 = Convolution2D(256, 256, 3, 1, 1, **kwargs) self.conv3_3 = Convolution2D(256, 256, 3, 1, 1, **kwargs) self.conv4_1 = Convolution2D(256, 512, 3, 1, 1, **kwargs) self.conv4_2 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.conv4_3 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.conv5_1 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.conv5_2 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.conv5_3 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.fc6 = Linear(512 * 7 * 7, 4096, **kwargs) self.fc7 = Linear(4096, 4096, **kwargs) self.fc8 = Linear(4096, 1000, **kwargs) if n_layers == 19: self.conv3_4 = Convolution2D(256, 256, 3, 1, 1, **kwargs) self.conv4_4 = Convolution2D(512, 512, 3, 1, 1, **kwargs) self.conv5_4 = Convolution2D(512, 512, 3, 1, 1, **kwargs) if pretrained_model == 'auto': if n_layers == 16: _retrieve( 'VGG_ILSVRC_16_layers.npz', 'https://www.robots.ox.ac.uk/%7Evgg/software/very_deep/' 'caffe/VGG_ILSVRC_16_layers.caffemodel', self) else: _retrieve( 'VGG_ILSVRC_19_layers.npz', 'http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/' 'caffe/VGG_ILSVRC_19_layers.caffemodel', self) elif pretrained_model: npz.load_npz(pretrained_model, self)
Example #7
Source File: googlenet.py From chainer with MIT License | 4 votes |
def __init__(self, pretrained_model='auto'): super(GoogLeNet, self).__init__() if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. kwargs = {'initialW': constant.Zero()} else: # employ default initializers used in BVLC. For more detail, see # https://github.com/chainer/chainer/pull/2424#discussion_r109642209 kwargs = {'initialW': uniform.LeCunUniform(scale=1.0)} with self.init_scope(): self.conv1 = Convolution2D(3, 64, 7, stride=2, pad=3, **kwargs) self.conv2_reduce = Convolution2D(64, 64, 1, **kwargs) self.conv2 = Convolution2D(64, 192, 3, stride=1, pad=1, **kwargs) self.inc3a = Inception(192, 64, 96, 128, 16, 32, 32) self.inc3b = Inception(256, 128, 128, 192, 32, 96, 64) self.inc4a = Inception(480, 192, 96, 208, 16, 48, 64) self.inc4b = Inception(512, 160, 112, 224, 24, 64, 64) self.inc4c = Inception(512, 128, 128, 256, 24, 64, 64) self.inc4d = Inception(512, 112, 144, 288, 32, 64, 64) self.inc4e = Inception(528, 256, 160, 320, 32, 128, 128) self.inc5a = Inception(832, 256, 160, 320, 32, 128, 128) self.inc5b = Inception(832, 384, 192, 384, 48, 128, 128) self.loss3_fc = Linear(1024, 1000, **kwargs) self.loss1_conv = Convolution2D(512, 128, 1, **kwargs) self.loss1_fc1 = Linear(2048, 1024, **kwargs) self.loss1_fc2 = Linear(1024, 1000, **kwargs) self.loss2_conv = Convolution2D(528, 128, 1, **kwargs) self.loss2_fc1 = Linear(2048, 1024, **kwargs) self.loss2_fc2 = Linear(1024, 1000, **kwargs) if pretrained_model == 'auto': _retrieve( 'bvlc_googlenet.npz', 'http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel', self) elif pretrained_model: npz.load_npz(pretrained_model, self)
Example #8
Source File: vgg16.py From chainercv with MIT License | 4 votes |
def __init__(self, n_class=None, pretrained_model=None, mean=None, initialW=None, initial_bias=None): param, path = utils.prepare_pretrained_model( {'n_class': n_class, 'mean': mean}, pretrained_model, self._models, {'n_class': 1000, 'mean': _imagenet_mean}) self.mean = param['mean'] if initialW is None: # Employ default initializers used in the original paper. initialW = normal.Normal(0.01) if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. initialW = constant.Zero() kwargs = {'initialW': initialW, 'initial_bias': initial_bias} super(VGG16, self).__init__() with self.init_scope(): self.conv1_1 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs) self.conv1_2 = Conv2DActiv(None, 64, 3, 1, 1, **kwargs) self.pool1 = _max_pooling_2d self.conv2_1 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs) self.conv2_2 = Conv2DActiv(None, 128, 3, 1, 1, **kwargs) self.pool2 = _max_pooling_2d self.conv3_1 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) self.conv3_2 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) self.conv3_3 = Conv2DActiv(None, 256, 3, 1, 1, **kwargs) self.pool3 = _max_pooling_2d self.conv4_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.conv4_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.conv4_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.pool4 = _max_pooling_2d self.conv5_1 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.conv5_2 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.conv5_3 = Conv2DActiv(None, 512, 3, 1, 1, **kwargs) self.pool5 = _max_pooling_2d self.fc6 = Linear(None, 4096, **kwargs) self.fc6_relu = relu self.fc6_dropout = dropout self.fc7 = Linear(None, 4096, **kwargs) self.fc7_relu = relu self.fc7_dropout = dropout self.fc8 = Linear(None, param['n_class'], **kwargs) self.prob = softmax if path: chainer.serializers.load_npz(path, self)