def __init__(self, size=None, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, axis=None): super(BatchNormalization, self).__init__() if size is None and axis is None: raise RuntimeError('size or axis is required') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps if isinstance(axis, int): axis = (axis,) self.axis = axis self._dtype = dtype with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 gamma_initializer = \ initializers._get_initializer(initial_gamma) gamma_initializer.dtype = self._dtype self.gamma = variable.Parameter(gamma_initializer) if use_beta: if initial_beta is None: initial_beta = 0 beta_initializer = initializers._get_initializer(initial_beta) beta_initializer.dtype = self._dtype self.beta = variable.Parameter(beta_initializer) if size is not None: self._initialize_params(size)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None): super(DeformableConvolution2DSampler, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.out_channels = out_channels self.initialW = initialW if initialW is None: initialW = constant.Zero() with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer) if in_channels is not None: self._initialize_params(in_channels)
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, dilate=1, nobias=False, initialW=None, initial_bias=None): super(DilatedConvolution2D, self).__init__() if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.dilate = _pair(dilate) self.out_channels = out_channels with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, wscale=1, bias=0, nobias=False, use_cudnn=True, initialW=None, initial_bias=None): super(Convolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.use_cudnn = use_cudnn self.out_channels = out_channels # For backward compatibility self.initialW = initialW self.wscale = wscale # For backward compatibility, the scale of weights is proportional to # the square root of wscale. self._W_initializer = initializers._get_initializer( initialW, scale=math.sqrt(wscale)) if in_channels is None: self.add_uninitialized_param('W') else: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias bias_initilizer = initializers._get_initializer(initial_bias) self.add_param('b', out_channels, initializer=bias_initilizer)
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None): chainer.utils.experimental( 'chainermn.links.MultiNodeBatchNormalization') if chainer.__version__.startswith('1.'): raise RuntimeError( 'MultiNodeBatchNormalization works only with ' 'chainer >= 2.0.0.') super(MultiNodeBatchNormalization, self).__init__() self.comm = comm self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, in_channels, channel_multiplier, ksize, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None): super(DepthwiseConvolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.channel_multiplier = channel_multiplier self.nobias = nobias if initialW is None: initialW = initializers.HeNormal(1. / numpy.sqrt(2)) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer) if in_channels is not None: self._initialize_params(in_channels)
def __init__(self, ndim, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, cover_all=False, dilate=1, groups=1): super(ConvolutionND, self).__init__() if ksize is None: out_channels, ksize, in_channels = \ in_channels, out_channels, None self.out_channels = out_channels self.ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.cover_all = cover_all self.dilate = conv_nd.as_tuple(dilate, ndim) self.groups = int(groups) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels)
def __init__(self, in_channels, channel_multiplier, ksize, stride=1, pad=0, wscale=1, bias=0, nobias=False, initialW=None, initial_bias=None): super(DepthwiseConvolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.channel_multiplier = channel_multiplier self.nobias = nobias # For compatibility with Convolution2D self.initialW = initialW self.wscale = wscale # For compatibility with Convolution2D, the scale of weights is # proportional to the square root of wscale. self._W_initializer = initializers._get_initializer( initialW, scale=math.sqrt(wscale)) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias self.bias_initilizer = initializers._get_initializer(initial_bias) if in_channels is None: self.add_uninitialized_param('b') if in_channels is None: self.add_uninitialized_param('W') else: self._initialize_params(in_channels)
def __init__(self, size, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None): super(BatchNormalization, self).__init__() self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, **kwargs): super(Convolution2D, self).__init__() argument.check_unexpected_kwargs( kwargs, deterministic="deterministic argument is not " "supported anymore. " "Use chainer.using_config('cudnn_deterministic', value) " "context where value is either `True` or `False`.") dilate, = argument.parse_kwargs(kwargs, ('dilate', 1)) if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.dilate = _pair(dilate) self.out_channels = out_channels with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_channels)
def __init__(self, in_size, out_size, wscale=1, bias=0, nobias=False, initialW=None, initial_bias=None): super(Linear, self).__init__() # For backward compatibility self.initialW = initialW self.wscale = wscale self.out_size = out_size # For backward compatibility, the scale of weights is proportional to # the square root of wscale. self._W_initializer = initializers._get_initializer( initialW, math.sqrt(wscale)) if in_size is None: self.add_uninitialized_param('W') else: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias bias_initializer = initializers._get_initializer(initial_bias) self.add_param('b', out_size, initializer=bias_initializer)
def __init__(self, in_channels, channel_multiplier, ksize, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None): super(DepthwiseConvolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.channel_multiplier = channel_multiplier self.nobias = nobias if initialW is None: initialW = initializers.HeNormal(1. / numpy.sqrt(2)) self._W_initializer = initializers._get_initializer(initialW) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) self.bias_initilizer = initializers._get_initializer(initial_bias) if in_channels is None: self.add_uninitialized_param('b') if in_channels is None: self.add_uninitialized_param('W') else: self._initialize_params(in_channels)
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, communication_backend='auto'): chainer.utils.experimental( 'chainermn.links.MultiNodeBatchNormalization') super(MultiNodeBatchNormalization, self).__init__() self.comm = comm self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self._communication_backend = \ get_communication_backend(comm, communication_backend) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def _initialize_params(self): lateral_init = initializers._get_initializer(self.lateral_init) upward_init = initializers._get_initializer(self.upward_init) for i in six.moves.range(0, 4 * self.state_size, self.state_size): lateral_init(self.lateral.W.data[i:i + self.state_size, :]) upward_init(self.upward.W.data[i:i + self.state_size, :]) a, i, f, o = lstm._extract_gates( self.upward.b.data.reshape(1, 4 * self.state_size, 1)) _init_weight(a, self.bias_init) _init_weight(i, self.bias_init) _init_weight(f, self.forget_bias_init) _init_weight(o, self.bias_init)
def __init__(self, n_channel, initial=0, eps=1e-5): super(Normalize, self).__init__() self.eps = eps with self.init_scope(): initializer = initializers._get_initializer(initial) self.scale = variable.Parameter(initializer) self.scale.initialize((n_channel),)
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialV=None, **kwargs): super(Deconvolution2D, self).__init__() argument.check_unexpected_kwargs( kwargs, deterministic="deterministic argument is not " "supported anymore. " "Use chainer.using_config('cudnn_deterministic', value) " "context where value is either `True` or `False`.") argument.assert_kwargs_empty(kwargs) if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.outsize = (None, None) if outsize is None else outsize self.out_channels = out_channels self.nobias = nobias with self.init_scope(): V_initializer = initializers._get_initializer(initialV) self.V = variable.Parameter(V_initializer) if in_channels is not None: kh, kw = _pair(self.ksize) V_shape = (in_channels, self.out_channels, kh, kw) self.V.initialize(V_shape) self.b = None if nobias else variable.Parameter(None) self.g = variable.Parameter(None)
def _init_array(self, initializer, default_value, size): if initializer is None: initializer = default_value initializer = initializers._get_initializer(initializer) return initializers.generate_array( initializer, size, self.xp, dtype=self._highprec_dtype, device=self.device)
def _initialize_params(self): lateral_init = initializers._get_initializer(self.lateral_init) upward_init = initializers._get_initializer(self.upward_init) bias_init = initializers._get_initializer(self.bias_init) forget_bias_init = initializers._get_initializer(self.forget_bias_init) for i in six.moves.range(0, 4 * self.state_size, self.state_size): lateral_init(self.lateral.W.array[i:i + self.state_size, :]) upward_init(self.upward.W.array[i:i + self.state_size, :]) a, i, f, o = lstm._extract_gates( self.upward.b.array.reshape(1, 4 * self.state_size, 1)) bias_init(a) bias_init(i) forget_bias_init(f) bias_init(o)
def test_scalar(self): init = initializers._get_initializer(10) self.assertIsInstance(init, initializers.Constant) x = numpy.empty((2, 3), dtype=numpy.int32) init(x) expected = numpy.full((2, 3), 10, dtype=numpy.int32) numpy.testing.assert_array_equal(x, expected)
def __init__(self, in_size, out_size, wscale=1, ratio=.5, initialW=None, initial_bias=0): super(SimplifiedDropconnect, self).__init__() self.out_size = out_size self.ratio = ratio # Square root is for the compatibility with Linear Function. self._W_initializer = initializers._get_initializer( initialW, math.sqrt(wscale)) if in_size is None: self.add_uninitialized_param('W') else: self._initialize_params(in_size) if initial_bias is not None: bias_initializer = initializers._get_initializer(initial_bias) self.add_param('b', out_size, initializer=bias_initializer)
def __init__(self, ndim, in_channels, out_channels, ksize, stride=1, pad=0, outsize=None, initialW=None, initial_bias=0, use_cudnn=True): ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.use_cudnn = use_cudnn self.outsize = outsize super(DeconvolutionND, self).__init__() W_shape = (in_channels, out_channels) + ksize initialW = initializers._get_initializer(initialW) self.add_param('W', W_shape, initializer=initialW) if initial_bias is None: self.b = None else: initial_bias = initializers._get_initializer(initial_bias) self.add_param('b', out_channels, initializer=initial_bias)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, wscale=1, bias=0, nobias=False, use_cudnn=True, initialW=None, initial_bias=None, deterministic=False): super(Convolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.use_cudnn = use_cudnn self.out_channels = out_channels self.deterministic = deterministic # For backward compatibility self.initialW = initialW self.wscale = wscale # For backward compatibility, the scale of weights is proportional to # the square root of wscale. self._W_initializer = initializers._get_initializer( initialW, scale=math.sqrt(wscale)) if in_channels is None: self.add_uninitialized_param('W') else: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias bias_initilizer = initializers._get_initializer(initial_bias) self.add_param('b', out_channels, initializer=bias_initilizer)
def __init__(self, ndim, in_channels, out_channels, ksize, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, cover_all=False, use_gamma=False, Ip=1, factor=None): super(SNConvolutionND, self).__init__() ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.cover_all = cover_all self.use_gamma = use_gamma self.Ip = Ip self.u = np.random.normal(size=(1, out_channels)).astype(dtype="f") self.register_persistent('u') self.factor = factor with self.init_scope(): W_shape = (out_channels, in_channels) + ksize self.W = variable.Parameter( initializers._get_initializer(initialW), W_shape) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels) if self.use_gamma: W_mat = self.W.data.reshape(self.W.shape[0], -1) _, s, _ = np.linalg.svd(W_mat) self.gamma = variable.Parameter(s[0], (1, ) * len(self.W.shape))
def __init__(self, in_channels, hidden_channel, initialW=None): super().__init__() ksize = (1, 1, 1) self.W_shape = (hidden_channel, in_channels) + ksize self.stride = 1 self.pad = 0 self.nobias = True self.b = None self.initialW = initialW with self.init_scope(): self.W = Parameter(_get_initializer(self.initialW), self.W_shape)
def __init__(self, left_size, right_size, out_size, initialW=None): super(BilinearDiag, self).__init__() self.in_sizes = (left_size, right_size) assert left_size == right_size with self.init_scope(): shape = (out_size, left_size) if isinstance(initialW, (numpy.ndarray, cuda.ndarray)): assert initialW.shape == shape self.W = variable.Parameter( initializers._get_initializer(initialW), shape)
def __init__(self, initial_param1=None, initial_param2=None, alpha=1, gpu=False): super(MultiLoss, self).__init__() self.alpha = alpha self.max_loss_detection = None self.max_loss_segmentation = None with self.init_scope(): if initial_param1 is None: initial_param1 = 0 if initial_param2 is None: initial_param2 = 0 param1_initializer = initializers._get_initializer(initial_param1) self.param1 = variable.Parameter(param1_initializer) param2_initializer = initializers._get_initializer(initial_param2) self.param2 = variable.Parameter(param2_initializer)
def test_numpy_array(self): c = numpy.array([1, 2, 3]) init = initializers._get_initializer(c) self.assertIsInstance(init, initializers.Constant) x = numpy.empty((3,), dtype=numpy.int32) init(x) expected = numpy.array([1, 2, 3], dtype=numpy.int32) numpy.testing.assert_array_equal(x, expected)
def test_numpy_array(self): c = numpy.array([1, 2, 3]) init = initializers._get_initializer(c) self.assertIsInstance(init, initializers.Constant) x = numpy.empty((3, ), dtype=numpy.int32) init(x) expected = numpy.array([1, 2, 3], dtype=numpy.int32) numpy.testing.assert_array_equal(x, expected)
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None): chainer.utils.experimental( 'chainermn.links.MultiNodeBatchNormalization') if chainer.__version__.startswith('1.'): raise RuntimeError('MultiNodeBatchNormalization works only with ' 'chainer >= 2.0.0.') super(MultiNodeBatchNormalization, self).__init__() self.comm = comm self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, size, decay=0.9, eps=2e-5, dtype=np.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None): super(ComplexBatchNorm, self).__init__() self.avg_mean = np.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = np.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self.moving_Vrr = Variable(cupy.full(size/2, 1./np.sqrt(2), dtype=dtype)) self.moving_Vii = Variable(cupy.full(size/2, 1./np.sqrt(2), dtype=dtype)) self.moving_Vri = Variable(cupy.zeros(size/2, dtype=dtype)) self.moving_real_mean = Variable(cupy.zeros(size/2, dtype=dtype)) self.moving_imag_mean = Variable(cupy.zeros(size/2, dtype=dtype)) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma_rr = 1./np.sqrt(2) initial_gamma_ri = 0 initial_gamma_rr = initializers._get_initializer(initial_gamma_rr) initial_gamma_rr.dtype = dtype initial_gamma_ii = initializers._get_initializer(initial_gamma_rr) initial_gamma_ii.dtype = dtype initial_gamma_ri = initializers._get_initializer(initial_gamma_ri) initial_gamma_ri.dtype = dtype self.gamma_rr = variable.Parameter(initial_gamma_rr, size/2) self.gamma_ii = variable.Parameter(initial_gamma_ii, size/2) self.gamma_ri = variable.Parameter(initial_gamma_ri, size/2) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, in_size, out_size=None, nobias=False, initialW=None, initial_bias=None): super(IncompleteBinaryLinear, self).__init__() if out_size is None: in_size, out_size = None, in_size self.out_size = out_size with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_size is not None: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_size)
def __init__(self, in_size, out_size=None, nobias=False, initialW=None, initial_bias=None): super(Linear, self).__init__() if out_size is None: in_size, out_size = None, in_size self.out_size = out_size with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_size is not None: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_size)
def __init__(self, axis=1, shape=None, initial_bias=None): super(Bias, self).__init__() # Add b parameter if given. if shape is not None: with self.init_scope(): if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, shape) self.axis = axis
def __init__(self, in_size, out_size, bias=0, nobias=False, initialW=None, initial_bias=None): super(Linear, self).__init__() # For backward compatibility self.initialW = initialW self.out_size = out_size self._W_initializer = initializers._get_initializer(initialW) if in_size is None: self.add_uninitialized_param('W') else: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias bias_initializer = initializers._get_initializer(initial_bias) self.add_param('b', out_size, initializer=bias_initializer)
def __init__(self, n_vocab, word_embedding_size, fix_embedding): super(SentenceEmbedding, self).__init__() with self.init_scope(): self.embed = L.EmbedID(n_vocab, word_embedding_size, ignore_label=-1) M_initializer = I._get_initializer(None) # This corresponds to tranpose(M) in original paper self.M = chainer.variable.Parameter(initializer=M_initializer, shape=(word_embedding_size, word_embedding_size)) self.fix_embedding = fix_embedding
def __init__(self, units, in_units, drop_rate=0.5): super(CondenseLinear, self).__init__() drop_in_units = int(in_units * drop_rate) with self.init_scope(): self.dense = L.Linear(in_size=drop_in_units, out_size=units) self.index = initializers.generate_array( initializer=initializers._get_initializer(0), shape=(drop_in_units, ), xp=self.xp, dtype=np.int32) self.register_persistent('index')
def __init__(self, ndim, in_channels, out_channels, ksize, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None): super(DeconvolutionND, self).__init__() ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.outsize = outsize with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer, (in_channels, out_channels) + ksize) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels)
def test_callable(self): def initializer(arr): arr[...] = 100 init = initializers._get_initializer(initializer) self.assertTrue(callable(init)) x = numpy.empty((2, 3), dtype=numpy.int32) init(x) expected = numpy.full((2, 3), 100, dtype=numpy.int32) numpy.testing.assert_array_equal(x, expected)
def __init__(self, ndim, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None, dilate=1, groups=1): super(DeconvolutionND, self).__init__() if ksize is None: out_channels, ksize, in_channels = \ in_channels, out_channels, None self.out_channels = out_channels self.ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.outsize = outsize self.dilate = conv_nd.as_tuple(dilate, ndim) self.groups = int(groups) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels)
def __init__(self, size=None, decay=0.9, eps=2e-5, dtype=None, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, axis=None): super(BatchNormalization, self).__init__() if size is None and axis is None: raise RuntimeError('size or axis is required') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps if isinstance(axis, int): axis = (axis, ) self.axis = axis self._dtype = chainer.get_dtype(dtype) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 gamma_initializer = \ initializers._get_initializer(initial_gamma) gamma_initializer.dtype = self._dtype self.gamma = variable.Parameter(gamma_initializer) if use_beta: if initial_beta is None: initial_beta = 0 beta_initializer = initializers._get_initializer(initial_beta) beta_initializer.dtype = self._dtype self.beta = variable.Parameter(beta_initializer) if size is not None: self._initialize_params(size)
def __init__(self, ndim, in_channels, out_channels, ksize, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, cover_all=False): super(ConvolutionND, self).__init__() ksize = conv_nd.as_tuple(ksize, ndim) self.stride = stride self.pad = pad self.cover_all = cover_all with self.init_scope(): W_shape = (out_channels, in_channels) + ksize self.W = variable.Parameter( initializers._get_initializer(initialW), W_shape) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 initial_bias = initializers._get_initializer(initial_bias) self.b = variable.Parameter(initial_bias, out_channels)
def __init__(self, in_size, out_size=None, nobias=False, initial_mu=None, initial_sigma=None, factorized=False): super().__init__() if out_size is None: in_size, out_size = None, in_size self.out_size = out_size self.factorized = factorized if initial_mu is None: if self.factorized: initial_mu = initializers.LeCunUniform(scale=np.sqrt(1 / 3)) else: initial_mu = initializers.LeCunUniform(scale=1.0) if initial_sigma is None: if self.factorized: initial_sigma = FanConstant(scale=0.5) else: initial_sigma = 0.017 with self.init_scope(): mu_initializer = initializers._get_initializer(initial_mu) sigma_initializer = initializers._get_initializer(initial_sigma) self.mu_w = variable.Parameter(mu_initializer) self.sigma_w = variable.Parameter(sigma_initializer) if nobias: self.mu_b = None self.sigma_b = None else: # `LeCunUniform` does not allow one-dim size initialization self.mu_b = variable.Parameter(mu_initializer, (1, out_size)) self.sigma_b = variable.Parameter(sigma_initializer, out_size) if in_size is not None: self._initialize_params(in_size)
def __init__(self, in_size, slot_size, memory_size, initialW=None): """ in_size: hidden_state h_size """ super().__init__() self.slot_size = slot_size self.memory_size = memory_size with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.memory = variable.Parameter(W_initializer) self.projection_matrix = variable.Parameter(W_initializer)
def __init__(self, in_channels, out_channels, in_size=None, ksize=None, stride=1, nobias=False, initialW=None, initial_bias=None, **kwargs): super(LocalConvolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.nobias = nobias self.out_channels = out_channels with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if nobias: self.b = None else: if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer) if in_channels is not None and in_size is not None: self._initialize_params(in_channels, _pair(in_size))
def __init__(self, in_size, out_size, ratio=.5, nobias=False, initialW=None, initial_bias=None): super(SimplifiedDropconnect, self).__init__() self.out_size = out_size self.ratio = ratio if initialW is None: initialW = initializers.HeNormal(1. / numpy.sqrt(2)) self.add_param('W', initializer=initializers._get_initializer( initialW)) if in_size is not None: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) bias_initializer = initializers._get_initializer(initial_bias) self.add_param('b', out_size, initializer=bias_initializer)
def __init__(self, size, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, norm_grad=False): super(InstanceNormalization, self).__init__() self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self.norm_grad = norm_grad with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = Parameter_scale(initial_gamma, size, norm_grad=self.norm_grad) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = Parameter_scale(initial_beta, size, norm_grad=self.norm_grad)
def __init__(self, in_size, out_size, ratio=.5, nobias=False, initialW=None, initial_bias=None): super(SimplifiedDropconnect, self).__init__() self.out_size = out_size self.ratio = ratio if initialW is None: initialW = initializers.HeNormal(1. / numpy.sqrt(2)) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_size is not None: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_size)
def __init__(self, in_size, out_size=None, nobias=False, initialW=None, initial_bias=None): # type: (tp.Optional[int], tp.Optional[int], bool, tp.Optional[types.InitializerSpec], tp.Optional[types.InitializerSpec]) -> None # NOQA super(Linear, self).__init__() if out_size is None: in_size, out_size = None, in_size self.out_size = out_size with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) # type: variable.Variable # NOQA if in_size is not None: self._initialize_params(in_size) if nobias: self.b = None # type: tp.Optional[variable.Variable] else: if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_size)
def __init__(self, in_size, out_size, ratio=.5, nobias=False, initialW=None, initial_bias=None): super(SimplifiedDropconnect, self).__init__() self.out_size = out_size self.ratio = ratio if initialW is None: initialW = initializers.HeNormal(1. / numpy.sqrt(2)) self._W_initializer = initializers._get_initializer(initialW) if in_size is None: self.add_uninitialized_param('W') else: self._initialize_params(in_size) if nobias: self.b = None else: if initial_bias is None: initial_bias = initializers.Constant(0) bias_initializer = initializers._get_initializer(initial_bias) self.add_param('b', out_size, initializer=bias_initializer)
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None, **kwargs): super(Deconvolution2D, self).__init__() dilate, groups, = argument.parse_kwargs( kwargs, ('dilate', 1), ('groups', 1), deterministic='deterministic argument is not supported anymore. ' 'Use chainer.using_config(\'cudnn_deterministic\', value) ' 'context where value is either `True` or `False`.') if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.dilate = _pair(dilate) self.outsize = (None, None) if outsize is None else outsize self.out_channels = out_channels self.groups = int(groups) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if isinstance(initial_bias, (numpy.ndarray, cuda.ndarray)): assert initial_bias.shape == (out_channels,) if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_channels)
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, outsize=None, initialW=None, initial_bias=None, group=1, **kwargs): super(Deconvolution2D, self).__init__() argument.check_unexpected_kwargs( kwargs, deterministic="deterministic argument is not " "supported anymore. " "Use chainer.using_config('cudnn_deterministic', value) " "context where value is either `True` or `False`.") argument.assert_kwargs_empty(kwargs) if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.outsize = (None, None) if outsize is None else outsize self.out_channels = out_channels self.group = int(group) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) if in_channels is not None: self._initialize_params(in_channels) if nobias: self.b = None else: if isinstance(initial_bias, (numpy.ndarray, cuda.ndarray)): assert initial_bias.shape == (out_channels,) if initial_bias is None: initial_bias = 0 bias_initializer = initializers._get_initializer(initial_bias) self.b = variable.Parameter(bias_initializer, out_channels)
def __init__(self, size, decay=0.9, eps=2e-5, dtype=numpy.float32, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, initial_avg_mean=None, initial_avg_var=None, enforce_compute=False): super(BatchNormalization2, self).__init__() if initial_avg_mean is not None: self.avg_mean = numpy.array(initial_avg_mean, dtype=dtype) else: self.avg_mean = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_mean') if initial_avg_var is not None: self.avg_var = numpy.array(initial_avg_var, dtype=dtype) else: self.avg_var = numpy.zeros(size, dtype=dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self.enforce_compute=enforce_compute with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=None, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, communication_backend='auto'): chainer.utils.experimental( 'chainermn.links.MultiNodeBatchNormalization') super(MultiNodeBatchNormalization, self).__init__() self._highprec_dtype = chainer.get_dtype( dtype, map_mixed16=numpy.float32) self.comm = comm self.avg_mean = numpy.zeros(size, dtype=self._highprec_dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=self._highprec_dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self._communication_backend = \ chainermn_batch_normalization.get_communication_backend( comm, communication_backend) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = self._highprec_dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = self._highprec_dtype self.beta = variable.Parameter(initial_beta, size)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, bias=0, nobias=False, use_cudnn=True, initialW=None, initial_bias=None, deterministic=False): super(Convolution2D, self).__init__() self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) self.use_cudnn = use_cudnn self.out_channels = out_channels self.deterministic = deterministic # For backward compatibility self.initialW = initialW self._W_initializer = initializers._get_initializer(initialW) if in_channels is None: self.add_uninitialized_param('W') else: self._initialize_params(in_channels) if nobias: self.b = None else: if initial_bias is None: initial_bias = bias bias_initilizer = initializers._get_initializer(initial_bias) self.add_param('b', out_channels, initializer=bias_initilizer)