def __init__(self, n_layers, in_size, out_size, dropout, use_bi_direction, **kwargs): argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) weights = [] direction = 2 if use_bi_direction else 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(6): if i == 0 and j < 3: w_in = in_size elif i > 0 and j < 3: w_in = out_size * direction else: w_in = out_size w = variable.Parameter( normal.Normal(numpy.sqrt(1. / w_in)), (out_size, w_in)) b = variable.Parameter(0, (out_size,)) setattr(weight, 'w%d' % j, w) setattr(weight, 'b%d' % j, b) weights.append(weight) super(NStepGRUBase, self).__init__(*weights) self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction self.rnn = rnn.n_step_bigru if use_bi_direction else rnn.n_step_gru
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 spatial_transformer_sampler(x, grid, **kwargs): """2D Spatial Transformer sampler. This is a differentiable image sampler. With a set of sampling points ``grid`` and an input feature map ``x``, this produces a sampled output feature map. This function currently only supports bilinear interpolation as a sampling kernel. When coordinates in ``grid`` is outside range :math:`[-1, 1]`, values are sampled from a zero padded input image. Notation: here is a notation for dimensionalities. - :math:`n` is the batch size. - :math:`c_I` is the number of the input channels. - :math:`h` and :math:`w` are the height and width of the input image, respectively. - :math:`h_O` and :math:`w_O` are the height and width of the output image. See detail in the following paper: `Spatial Transformer Networks \ <https://arxiv.org/abs/1506.02025>`_. .. note:: cuDNN supports SpatialTransformerSampler from version 5.0.0. Args: x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable of shape :math:`(n, c_I, h, w)`. grid (~chainer.Variable): Coordinate variable of shape :math:`(n, 2, h_O, w_O)`. Each coordinate defines the spatial location in the input where a sampling kernel is applied to get the value at a particular pixel in the output. ``grid[idx, :, i, j]`` corresponds to the coordinate that is used to sample the values for an output pixel at location :math:`(i, j)`. In the second dimension, the first coordinate corresponds to the location along the horizontal axis, and the second coordinate corresponds to the location along the vertical axis. The coordinate :math:`(-1, -1)` corresponds to the upper-left corner of the input image. Returns: ~chainer.Variable: Output feature map of shape \ :math:`(n, c_I, h_O, w_O)`. """ if kwargs: argument.check_unexpected_kwargs( kwargs, use_cudnn='The argument "use_cudnn" is not ' 'supported anymore. ' 'Use chainer.using_config(\'use_cudnn\', value) ' 'context where value can be `always`, `never`, or `auto`.') argument.assert_kwargs_empty(kwargs) return SpatialTransformerSampler()(x, grid)
def __init__(self, n_layers, in_size, out_size, dropout, use_bi_direction, activation, **kwargs): argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) weights = [] direction = 2 if use_bi_direction else 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() for j in six.moves.range(2): if i == 0 and j < 1: w_in = in_size elif i > 0 and j < 1: w_in = out_size * direction else: w_in = out_size weight.add_param('w%d' % j, (out_size, w_in)) weight.add_param('b%d' % j, (out_size,)) getattr(weight, 'w%d' % j).data[...] = numpy.random.normal( 0, numpy.sqrt(1. / w_in), (out_size, w_in)) getattr(weight, 'b%d' % j).data[...] = 0 weights.append(weight) super(NStepRNNBase, self).__init__(*weights) self.n_layers = n_layers self.dropout = dropout self.activation = activation self.out_size = out_size self.direction = direction self.rnn = rnn.n_step_birnn if use_bi_direction else rnn.n_step_rnn
def __call__(self, x, **kwargs): """__call__(self, x, finetune=False) Invokes the forward propagation of BatchNormalization. In training mode, the BatchNormalization computes moving averages of mean and variance for evaluation during training, and normalizes the input using batch statistics. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', False)``. See :func:`chainer.using_config`. Args: x (Variable): Input variable. finetune (bool): If it is in the training mode and ``finetune`` is ``True``, BatchNormalization runs in fine-tuning mode; it accumulates the input array to compute population statistics for normalization, and normalizes the input using batch statistics. """ argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') finetune, = argument.parse_kwargs(kwargs, ('finetune', False)) if hasattr(self, 'gamma'): gamma = self.gamma else: with cuda.get_device_from_id(self._device_id): gamma = variable.Variable(self.xp.ones( self.avg_mean.shape, dtype=x.dtype)) if hasattr(self, 'beta'): beta = self.beta else: with cuda.get_device_from_id(self._device_id): beta = variable.Variable(self.xp.zeros( self.avg_mean.shape, dtype=x.dtype)) if configuration.config.train: if finetune: self.N += 1 decay = 1. - 1. / self.N else: decay = self.decay ret = functions.batch_normalization( x, gamma, beta, eps=self.eps, running_mean=self.avg_mean, running_var=self.avg_var, decay=decay) else: # Use running average statistics or fine-tuned statistics. mean = variable.Variable(self.avg_mean) var = variable.Variable(self.avg_var) ret = functions.fixed_batch_normalization( x, gamma, beta, mean, var, self.eps) return ret
def zoneout(h, x, ratio=.5, **kwargs): """zoneout(h, x, ratio=.5) Drops elements of input variable and sets to previous variable randomly. This function drops input elements randomly with probability ``ratio`` and instead sets dropping element to their previous variable. In testing mode , it does nothing and just returns ``x``. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: h (~chainer.Variable): Previous variable. x (~chainer.Variable): Input variable. ratio (float): Zoneout ratio. Returns: ~chainer.Variable: Output variable. See the paper: `Zoneout: Regularizing RNNs by Randomly Preserving Hidden \ Activations <https://arxiv.org/abs/1606.01305>`_. """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if configuration.config.train: return Zoneout(ratio)(h, x) return x
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 dropout(x, ratio=.5, **kwargs): """dropout(x, ratio=.5) Drops elements of input variable randomly. This function drops input elements randomly with probability ``ratio`` and scales the remaining elements by factor ``1 / (1 - ratio)``. In testing mode, it does nothing and just returns ``x``. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. ratio (float): Dropout ratio. Returns: ~chainer.Variable: Output variable. See the paper by G. Hinton: `Improving neural networks by preventing \ co-adaptation of feature detectors <https://arxiv.org/abs/1207.0580>`_. """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if configuration.config.train: return Dropout(ratio)(x) return x
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, activation=relu.relu, *args, **kwargs): # If `args` is not empty, users assume the API for v1 and # specify `wscale` as a positonal argument, which we want # to detect and forbid with an explicit error message. msg = ('wscale is not supported anymore. ' 'Use conv_init and bias_init argument to change ' 'the scale of initial parameters.') if args: raise TypeError(msg) argument.check_unexpected_kwargs(kwargs, wscale=msg) conv_init, bias_init = argument.parse_kwargs( kwargs, ('conv_init', None), ('bias_init', None)) if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None assert len(out_channels) > 0 convs = [convolution_2d.Convolution2D( in_channels, out_channels[0], ksize, stride, pad, initialW=conv_init, initial_bias=bias_init)] for n_in, n_out in zip(out_channels, out_channels[1:]): convs.append(convolution_2d.Convolution2D( n_in, n_out, 1, initialW=conv_init, initial_bias=bias_init)) super(MLPConvolution2D, self).__init__(*convs) self.activation = activation
def forward(self, x, layers=None, **kwargs): """forward(self, x, layers=['prob']) Computes all the feature maps specified by ``layers``. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. It should be prepared by ``prepare`` function. layers (list of str): The list of layer names you want to extract. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['prob'] if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) h = x activations = {} inception_4a_cache = None inception_4d_cache = None target_layers = set(layers) for key, funcs in self.functions.items(): if len(target_layers) == 0: break if key == 'loss1_fc2': h = inception_4a_cache elif key == 'loss2_fc2': h = inception_4d_cache for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) if key == 'inception_4a': inception_4a_cache = h elif key == 'inception_4d': inception_4d_cache = h return activations
def dropout(x, ratio=.5, **kwargs): """dropout(x, ratio=.5) Drops elements of input variable randomly. This function drops input elements randomly with probability ``ratio`` and scales the remaining elements by factor ``1 / (1 - ratio)``. In testing mode, it does nothing and just returns ``x``. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', boolean)``. See :func:`chainer.using_config`. Args: x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \ :class:`cupy.ndarray`): Input variable. A :math:`(s_1, s_2, ..., s_N)` -shaped float array. ratio (float): Dropout ratio. The ``ratio`` must be ``0.0 <= ratio < 1.0``. Returns: ~chainer.Variable: Output variable. See the paper by G. Hinton: `Improving neural networks by preventing \ co-adaptation of feature detectors <https://arxiv.org/abs/1207.0580>`_. .. admonition:: Example >>> x = np.array([[-1, 0], [2, -3], [-2, 1]], np.float32) >>> with chainer.using_config('train', True): ... y = F.dropout(x) >>> y.data array([[-2., 0.], [ 4., -6.], [-0., 2.]], dtype=float32) >>> with chainer.using_config('train', True): ... y = F.dropout(x, ratio=0.0) \ # dropout returns original input if ratio=0.0 >>> (x == y.data).all() True >>> with chainer.using_config('train', False): ... y = F.dropout(x) \ # dropout in test mode returns original input >>> (x == y.data).all() True """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if configuration.config.train: return Dropout(ratio).apply((x,))[0] return chainer.as_variable(x)
def spatial_transformer_grid(theta, output_shape, **kwargs): """2D Spatial Transformer grid. This function generates coordinates of the points sampled from an image to perform warping described in `Spatial Transformer Networks \ <https://arxiv.org/abs/1506.02025>`_. Given a coordinate in the warped image :math:`(x_i^t, y_i^t)`, the point sampled from the source image :math:`(x_i^s, y_i^s)` are calculated by the following equation. .. note:: cuDNN supports SpatialTransformerGrid from version 5.0.0. .. math:: \\left(\\begin{matrix} x_i^s \\\\ y_i^s \\end{matrix}\\right) = \\left(\\begin{matrix} \\theta_{11} & \\theta_{12} & \\theta_{13} \\\\ \\theta_{21} & \\theta_{22} & \\theta_{23} \\end{matrix}\\right) \\left(\\begin{matrix} x_i^t \\\\ y_i^t \\\\ 1 \\end{matrix}\\right) Notation: here is a notation for dimensionalities. - :math:`n` is the batch size. - :math:`h_O` and :math:`w_O` are the height and the width of the output image. Args: theta (:class:`~chainer.Variable` or :ref:`ndarray`): An array of shape :math:`(n, 2, 3)`. This is a batch of :math:`2 \\times 3` matrix used for the warping described above. output_shape (tuple): A tuple of 2 elements: :math:`h_O, w_O`. Returns: ~chainer.Variable: A variable of shape :math:`(n, 2, h_O, w_O)`. In the 2nd dimension, the first element is the coordinate along the x axis, and the second element is the coordinate along the y axis. All the coordinates in the image are scaled to fit range :math:`[-1, 1]`. This means that the coordinate :math:`(-1, -1)` corresponds to the upper-left corner of the input image. """ if kwargs: argument.check_unexpected_kwargs( kwargs, use_cudnn='The argument "use_cudnn" is not ' 'supported anymore. ' 'Use chainer.using_config(\'use_cudnn\', value) ' 'context where value can be `always`, `never`, or `auto`.') argument.assert_kwargs_empty(kwargs) return SpatialTransformerGrid(output_shape)(theta)
def forward(self, inputs, outputs, disable=(), **kwargs): """forward(self, inputs, outputs, disable=()) Executes a sub-network of the network. This function acts as an interpreter of the network definition for Caffe. On execution, it interprets each layer one by one, and if the bottom blobs are already computed, then emulates the layer and stores output blobs as :class:`~chainer.Variable` objects. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: inputs (dict): A dictionary whose key-value pairs indicate initial correspondences between blob names and :class:`~chainer.Variable` objects. outputs (Iterable): A list of blob names whose corresponding :class:`~chainer.Variable` objects are returned. disable (Iterable): A list of layer names that will be ignored during the forward computation. Returns: tuple: A tuple of output :class:`~chainer.Variable` objects corresponding to elements of the `outputs` argument. """ if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) variables = dict(inputs) disable = set(disable) for func_name, bottom, top in self.layers: if (func_name in disable or func_name not in self.forwards or any(blob not in variables for blob in bottom)): continue func = self.forwards[func_name] input_vars = tuple(variables[blob] for blob in bottom) output_vars = func(*input_vars) if not isinstance(output_vars, collections_abc.Iterable): output_vars = output_vars, for var, name in zip(output_vars, top): variables[name] = var self.variables = variables return tuple(variables[blob] for blob in outputs)
def __init__(self, variable, name, **kwargs): argument.check_unexpected_kwargs( kwargs, grad='unexpected keyword argument "grad": ' 'pass the gradient to Variable instead' ) self._variable = weakref.ref(variable) self.name = name self._requires_grad = variable.requires_grad vdata = variable.data self._set_data_type(vdata)
def __call__(self, hx, cx, xs, **kwargs): """__call__(self, hx, cx, xs) Calculate all hidden states and cell states. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: hx (~chainer.Variable or None): Initial hidden states. If ``None`` is specified zero-vector is used. cx (~chainer.Variable or None): Initial cell states. If ``None`` is specified zero-vector is used. xs (list of ~chianer.Variable): List of input sequences. Each element ``xs[i]`` is a :class:`chainer.Variable` holding a sequence. """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) assert isinstance(xs, (list, tuple)) indices = n_step_rnn.argsort_list_descent(xs) xs = n_step_rnn.permutate_list(xs, indices, inv=False) if hx is None: hx = self.init_hx(xs) else: hx = permutate.permutate(hx, indices, axis=1, inv=False) if cx is None: cx = self.init_hx(xs) else: cx = permutate.permutate(cx, indices, axis=1, inv=False) trans_x = transpose_sequence.transpose_sequence(xs) ws = [[w.w0, w.w1, w.w2, w.w3, w.w4, w.w5, w.w6, w.w7] for w in self] bs = [[w.b0, w.b1, w.b2, w.b3, w.b4, w.b5, w.b6, w.b7] for w in self] hy, cy, trans_y = self.rnn( self.n_layers, self.dropout, hx, cx, ws, bs, trans_x) hy = permutate.permutate(hy, indices, axis=1, inv=True) cy = permutate.permutate(cy, indices, axis=1, inv=True) ys = transpose_sequence.transpose_sequence(trans_y) ys = n_step_rnn.permutate_list(ys, indices, inv=True) return hy, cy, ys
def extract(self, images, layers=None, size=(224, 224), **kwargs): """extract(self, images, layers=['pool5'], size=(224, 224)) Extracts all the feature maps of given images. The difference of directly executing ``forward`` is that it directly accepts images as an input and automatically transforms them to a proper variable. That is, it is also interpreted as a shortcut method that implicitly calls ``prepare`` and ``forward`` functions. Unlike ``predict`` method, this method does not override ``chainer.config.train`` and ``chainer.config.enable_backprop`` configuration. If you want to extract features without updating model parameters, you need to manually set configuration when calling this method as follows: .. code-block:: python # model is an instance of `GoogLeNet` with chainer.using_config('train', False): with chainer.using_config('enable_backprop', False): feature = model.extract([image]) Args: images (iterable of PIL.Image or numpy.ndarray): Input images. layers (list of str): The list of layer names you want to extract. size (pair of ints): The resolution of resized images used as an input of CNN. All the given images are not resized if this argument is ``None``, but the resolutions of all the images should be the same. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['pool5'] if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config', volatile='volatile argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) x = concat_examples([prepare(img, size=size) for img in images]) x = Variable(self.xp.asarray(x)) return self(x, layers=layers)
def __init__(self, stride=1, pad=0, outsize=None, requires_x_grad=True, **kwargs): 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) self.sy, self.sx = _pair(stride) self.ph, self.pw = _pair(pad) self.outh, self.outw = (None, None) if outsize is None else outsize self.requires_x_grad = requires_x_grad
def deconvolution_2d(x, V, g, b=None, stride=1, pad=0, outsize=None, **kwargs): 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) func = Deconvolution2DFunction(stride, pad, outsize) if b is None: return func(x, V, g) else: return func(x, V, g, b)
def extract(self, images, layers=None, size=(224, 224), **kwargs): """extract(self, images, layers=['pool5'], size=(224, 224)) Extracts all the feature maps of given images. The difference of directly executing ``__call__`` is that it directly accepts images as an input and automatically transforms them to a proper variable. That is, it is also interpreted as a shortcut method that implicitly calls ``prepare`` and ``__call__`` functions. .. warning:: ``test`` and ``volatile`` arguments are not supported anymore since v2. Instead, use ``chainer.using_config('train', train)`` and ``chainer.using_config('enable_backprop', not volatile)`` respectively. See :func:`chainer.using_config`. Args: images (iterable of PIL.Image or numpy.ndarray): Input images. layers (list of str): The list of layer names you want to extract. size (pair of ints): The resolution of resized images used as an input of CNN. All the given images are not resized if this argument is ``None``, but the resolutions of all the images should be the same. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['pool5'] argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config', volatile='volatile argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) x = concat_examples([prepare(img, size=size) for img in images]) x = Variable(self.xp.asarray(x)) return self(x, layers=layers)
def __call__(self, x, gamma, beta, **kwargs): """__call__(self, x, c, finetune=False) Invokes the forward propagation of BatchNormalization. In training mode, the BatchNormalization computes moving averages of mean and variance for evaluatino during training, and normalizes the input using batch statistics. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: x (Variable): Input variable. gamma (Variable): Input variable of gamma of shape finetune (bool): If it is in the training mode and ``finetune`` is ``True``, BatchNormalization runs in fine-tuning mode; it accumulates the input array to compute population statistics for normalization, and normalizes the input using batch statistics. """ argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') finetune, = argument.parse_kwargs(kwargs, ('finetune', False)) with cuda.get_device_from_id(self._device_id): _gamma = variable.Variable(self.xp.ones( self.avg_mean.shape, dtype=x.dtype)) with cuda.get_device_from_id(self._device_id): _beta = variable.Variable(self.xp.zeros( self.avg_mean.shape, dtype=x.dtype)) if configuration.config.train: if finetune: self.N += 1 decay = 1. - 1. / self.N else: decay = self.decay ret = chainer.functions.batch_normalization(x, _gamma, _beta, eps=self.eps, running_mean=self.avg_mean, running_var=self.avg_var, decay=decay) else: # Use running average statistics or fine-tuned statistics. mean = variable.Variable(self.avg_mean) var = variable.Variable(self.avg_var) ret = batch_normalization.fixed_batch_normalization( x, _gamma, _beta, mean, var, self.eps) shape = ret.shape ndim = len(shape) gamma = F.broadcast_to(F.reshape(gamma, list(gamma.shape) + [1] * (ndim - len(gamma.shape))), shape) beta = F.broadcast_to(F.reshape(beta, list(beta.shape) + [1] * (ndim - len(beta.shape))), shape) self.output = gamma * ret + beta return self.output
def _call(self, hs, xs, **kwargs): """Calls RNN function. Args: hs (list of ~chainer.Variable or None): Lisit of hidden states. Its length depends on its implementation. If ``None`` is specified zero-vector is used. xs (list of ~chainer.Variable): List of input sequences. Each element ``xs[i]`` is a :class:`chainer.Variable` holding a sequence. Returns: tuple: hs """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) assert isinstance(xs, (list, tuple)) xp = cuda.get_array_module(*(list(hs) + list(xs))) indices = argsort_list_descent(xs) indices_array = xp.array(indices) xs = permutate_list(xs, indices, inv=False) hxs = [] for hx in hs: if hx is None: hx = self.init_hx(xs) else: hx = permutate.permutate(hx, indices_array, axis=1, inv=False) hxs.append(hx) trans_x = transpose_sequence.transpose_sequence(xs) args = [self.n_layers, self.dropout] + hxs + \ [self.ws, self.bs, trans_x] result = self.rnn(*args) hys = [ permutate.permutate(h, indices_array, axis=1, inv=True) for h in result[:-1] ] trans_y = result[-1] ys = transpose_sequence.transpose_sequence(trans_y) ys = permutate_list(ys, indices, inv=True) return hys, ys
def make_extension(trigger=None, default_name=None, priority=None, finalizer=None, initializer=None, on_error=None, **kwargs): """Decorator to make given functions into trainer extensions. This decorator just adds some attributes to a given function. The value of the attributes are given by the arguments of this decorator. See :class:`Extension` for details of trainer extensions. Most of the default values of arguments also follow those for this class. Args: trigger: Default trigger of the extension. default_name: Default name of the extension. The name of a given function is used by default. priority (int): Default priority of the extension. finalizer: Finalizer function of this extension. It is called at the end of the training loop. initializer: Initializer function of this extension. It is called at the beginning of the training loop. on_error: Error handler callback function of this extension. It is called after an error is raised during the trainer loop. """ if kwargs: msg = ('invoke_before_training has been removed since Chainer v2.0.0. ' 'Use initializer= instead.') argument.check_unexpected_kwargs(kwargs, invoke_before_training=msg) argument.assert_kwargs_empty(kwargs) if trigger is None: trigger = Extension.trigger if priority is None: priority = Extension.priority def decorator(ext): ext.trigger = trigger ext.default_name = default_name or ext.__name__ ext.priority = priority ext.finalize = finalizer ext.on_error = on_error ext.initialize = initializer return ext return decorator
def __init__(self, in_size, out_size, c_ratio=0.5, h_ratio=0.5, **kwargs): argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) super(StatefulZoneoutLSTM, self).__init__() self.state_size = out_size self.c_ratio = c_ratio self.h_ratio = h_ratio self.reset_state() with self.init_scope(): self.upward = linear.Linear(in_size, 4 * out_size) self.lateral = linear.Linear(out_size, 4 * out_size, nobias=True)
def __init__(self, stride=1, pad=0, outsize=None, **kwargs): 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`.", requires_x_grad="requires_x_grad argument is not supported " "anymore. Just remove the argument. Note that whether to compute " "the gradient w.r.t. x is automatically decided during " "backpropagation.") argument.assert_kwargs_empty(kwargs) self.sy, self.sx = _pair(stride) self.ph, self.pw = _pair(pad) self.outh, self.outw = (None, None) if outsize is None else outsize
def __call__(self, hx, xs, **kwargs): """__call__(self, hx, xs) Calculate all hidden states and cell states. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: hx (~chainer.Variable or None): Initial hidden states. If ``None`` is specified zero-vector is used. xs (list of ~chainer.Variable): List of input sequences. Each element ``xs[i]`` is a :class:`chainer.Variable` holding a sequence. """ argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) assert isinstance(xs, (list, tuple)) xp = cuda.get_array_module(hx, *xs) indices = argsort_list_descent(xs) indices_array = xp.array(indices) xs = permutate_list(xs, indices, inv=False) if hx is None: hx = self.init_hx(xs) else: hx = permutate.permutate(hx, indices_array, axis=1, inv=False) trans_x = transpose_sequence.transpose_sequence(xs) ws = [[w.w0, w.w1] for w in self] bs = [[w.b0, w.b1] for w in self] hy, trans_y = self.rnn( self.n_layers, self.dropout, hx, ws, bs, trans_x, activation=self.activation) hy = permutate.permutate(hy, indices_array, axis=1, inv=True) ys = transpose_sequence.transpose_sequence(trans_y) ys = permutate_list(ys, indices, inv=True) return hy, ys
def extract(self, images, layers=None, size=(224, 224), **kwargs): """extract(self, images, layers=['pool5'], size=(224, 224)) Extracts all the feature maps of given images. The difference of directly executing ``__call__`` is that it directly accepts images as an input and automatically transforms them to a proper variable. That is, it is also interpreted as a shortcut method that implicitly calls ``prepare`` and ``__call__`` functions. .. warning:: ``train`` and ``volatile`` arguments are not supported anymore since v2. Instead, use ``chainer.using_config('train', train)`` and ``chainer.using_config('enable_backprop', not volatile)`` respectively. See :func:`chainer.using_config`. Args: images (iterable of PIL.Image or numpy.ndarray): Input images. layers (list of str): The list of layer names you want to extract. size (pair of ints): The resolution of resized images used as an input of CNN. All the given images are not resized if this argument is ``None``, but the resolutions of all the images should be the same. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['pool5'] argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config', volatile='volatile argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) x = concat_examples([prepare(img, size=size) for img in images]) x = Variable(self.xp.asarray(x)) return self(x, layers=layers)
def forward(self, inputs, outputs, disable=(), **kwargs): """forward(self, inputs, outputs, disable=()) Executes a sub-network of the network. This function acts as an interpreter of the network definition for Caffe. On execution, it interprets each layer one by one, and if the bottom blobs are already computed, then emulates the layer and stores output blobs as :class:`~chainer.Variable` objects. Args: inputs (dict): A dictionary whose key-value pairs indicate initial correspondences between blob names and :class:`~chainer.Variable` objects. outputs (Iterable): A list of blob names whose corresponding :class:`~chainer.Variable` objects are returned. disable (Iterable): A list of layer names that will be ignored during the forward computation. Returns: tuple: A tuple of output :class:`~chainer.Variable` objects corresponding to elements of the `outputs` argument. """ if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) variables = dict(inputs) disable = set(disable) for func_name, bottom, top in self.layers: if (func_name in disable or func_name not in self.forwards or any(blob not in variables for blob in bottom)): continue func = self.forwards[func_name] input_vars = tuple(variables[blob] for blob in bottom) output_vars = func(*input_vars) if not isinstance(output_vars, (tuple, list)): output_vars = output_vars, for var, name in zip(output_vars, top): variables[name] = var self.variables = variables return tuple(variables[blob] for blob in outputs)
def __init__(self, n_layers, in_size, out_size, dropout, **kwargs): if kwargs: argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config', use_bi_direction='use_bi_direction is not supported anymore', activation='activation is not supported anymore') argument.assert_kwargs_empty(kwargs) weights = [] if self.use_bi_direction: direction = 2 else: direction = 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(self.n_weights): if i == 0 and j < self.n_weights // 2: w_in = in_size elif i > 0 and j < self.n_weights // 2: w_in = out_size * direction else: w_in = out_size w = variable.Parameter( normal.Normal(numpy.sqrt(1. / w_in)), (out_size, w_in)) b = variable.Parameter(0, (out_size,)) setattr(weight, 'w%d' % j, w) setattr(weight, 'b%d' % j, b) weights.append(weight) super(NStepRNNBase, self).__init__(*weights) self.ws = [[getattr(layer, 'w%d' % i) for i in six.moves.range(self.n_weights)] for layer in self] self.bs = [[getattr(layer, 'b%d' % i) for i in six.moves.range(self.n_weights)] for layer in self] self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction
def __init__(self, n_layers, in_size, out_size, dropout, **kwargs): if kwargs: argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config', use_bi_direction='use_bi_direction is not supported anymore', activation='activation is not supported anymore') argument.assert_kwargs_empty(kwargs) weights = [] if self.use_bi_direction: direction = 2 else: direction = 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(self.n_weights): if i == 0 and j < self.n_weights // 2: w_in = in_size elif i > 0 and j < self.n_weights // 2: w_in = out_size * direction else: w_in = out_size w = variable.Parameter( normal.Normal(numpy.sqrt(1. / w_in)), (out_size, w_in)) b = variable.Parameter(0, (out_size, )) setattr(weight, 'w%d' % j, w) setattr(weight, 'b%d' % j, b) weights.append(weight) super(NStepRNNBase, self).__init__(*weights) self.ws = [[ getattr(layer, 'w%d' % i) for i in six.moves.range(self.n_weights) ] for layer in self] self.bs = [[ getattr(layer, 'b%d' % i) for i in six.moves.range(self.n_weights) ] for layer in self] self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction
def __call__(self, x, **kwargs): argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') finetune, = argument.parse_kwargs(kwargs, ('finetune', False)) # reshape input x for instance normalization shape_org = x.shape B, C = shape_org[:2] shape_ins = (1, B * C) + shape_org[2:] x_reshaped = functions.reshape(x, shape_ins) gamma = self.gamma if gamma is None: with chainer.using_device(self.device): gamma = self.xp.ones(self.avg_mean.shape, dtype=self._dtype) beta = self.beta if beta is None: with chainer.using_device(self.device): beta = self.xp.zeros(self.avg_mean.shape, dtype=self._dtype) gamma = functions.tile(gamma, (B, )) beta = functions.tile(beta, (B, )) mean = self.xp.tile(self.avg_mean, (B, )) var = self.xp.tile(self.avg_var, (B, )) # instance normalization is always done in training mode if finetune: self.N += 1 decay = 1. - 1. / self.N else: decay = self.decay ret = functions.batch_normalization(x_reshaped, gamma, beta, eps=self.eps, running_mean=mean, running_var=var, decay=decay) self.avg_mean = mean.reshape(B, C).mean(axis=0) self.avg_var = var.reshape(B, C).mean(axis=0) # ret is normalized input x return functions.reshape(ret, shape_org)
def __init__(self, coeffs, stride=1, pad=0, cover_all=False, requires_x_grad=True, bcoeffs=None, ocoeffs=None, **kwargs): 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) self.coeffs = coeffs self.ocoeffs = ocoeffs self.bcoeffs = bcoeffs self.sy, self.sx = _pair(stride) self.ph, self.pw = _pair(pad) self.cover_all = cover_all self.requires_x_grad = requires_x_grad
def __init__(self, stride=1, pad=0, cover_all=False, **kwargs): 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`.", requires_x_grad="requires_x_grad argument is not supported " "anymore. Just remove the argument. Note that whether to compute " "the gradient w.r.t. x is automatically decided during " "backpropagation.") dilate, = argument.parse_kwargs(kwargs, ('dilate', 1)) self.sy, self.sx = _pair(stride) self.ph, self.pw = _pair(pad) self.cover_all = cover_all self.dy, self.dx = _pair(dilate)
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 _call(self, hs, xs, **kwargs): """Calls RNN function. Args: hs (list of ~chainer.Variable or None): Lisit of hidden states. Its length depends on its implementation. If ``None`` is specified zero-vector is used. xs (list of ~chainer.Variable): List of input sequences. Each element ``xs[i]`` is a :class:`chainer.Variable` holding a sequence. Returns: tuple: hs """ if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) assert isinstance(xs, (list, tuple)) xp = cuda.get_array_module(*(list(hs) + list(xs))) indices = argsort_list_descent(xs) indices_array = xp.array(indices) xs = permutate_list(xs, indices, inv=False) hxs = [] for hx in hs: if hx is None: hx = self.init_hx(xs) else: hx = permutate.permutate(hx, indices_array, axis=1, inv=False) hxs.append(hx) trans_x = transpose_sequence.transpose_sequence(xs) args = [self.n_layers, self.dropout] + hxs + \ [self.ws, self.bs, trans_x] result = self.rnn(*args) hys = [permutate.permutate(h, indices_array, axis=1, inv=True) for h in result[:-1]] trans_y = result[-1] ys = transpose_sequence.transpose_sequence(trans_y) ys = permutate_list(ys, indices, inv=True) return hys, ys
def forward(self, x, layers=None, **kwargs): """forward(self, x, layers=['prob']) Computes all the feature maps specified by ``layers``. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', False)`` to run in test mode. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. It should be prepared by ``prepare`` function. layers (list of str): The list of layer names you want to extract. If ``None``, 'prob' will be used as layers. Returns: Dictionary of ~chainer.Variable: A dictionary in which the key contains the layer and the value contains the corresponding feature map variable. """ if layers is None: layers = ['prob'] if kwargs: argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config' ) argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) for key, funcs in self.functions.items(): if len(target_layers) == 0: break for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) return activations
def forward(self, x, layers=None, **kwargs): """forward(self, x, layers=['prob']) Computes all the feature maps specified by ``layers``. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', False)`` to run in test mode. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. It should be prepared by ``prepare`` function. layers (list of str): The list of layer names you want to extract. If ``None``, 'prob' will be used as layers. Returns: Dictionary of ~chainer.Variable: A dictionary in which the key contains the layer and the value contains the corresponding feature map variable. """ if layers is None: layers = ['prob'] if kwargs: argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) for key, funcs in self.functions.items(): if len(target_layers) == 0: break for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) return activations
def __init__(self, stride=1, pad=0, outsize=None, **kwargs): 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`.", requires_x_grad="requires_x_grad argument is not supported " "anymore. Just remove the argument. Note that whether to compute " "the gradient w.r.t. x is automatically decided during " "backpropagation." ) dilate, = argument.parse_kwargs(kwargs, ('dilate', 1)) self.sy, self.sx = _pair(stride) self.ph, self.pw = _pair(pad) self.outh, self.outw = (None, None) if outsize is None else outsize self.dy, self.dx = _pair(dilate)
def __init__(self, in_channels, out_channels, units, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, **kwargs): super().__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.units = units self.ksize = ksize self.stride = _pair(stride) self.pad = _pair(pad) 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, (units, out_channels // units))
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, nobias=False, initialW=None, initial_bias=None, **kwargs): super(EqualizedConvolution2d, 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, groups = argument.parse_kwargs(kwargs, ('dilate', 1), ('groups', 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 self.groups = int(groups) with self.init_scope(): W_initializer = initializers._get_initializer(initialW) self.W = variable.Parameter(W_initializer) self.scale_param = variable.Parameter(-1, (1, 1)) 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_channels:int, out_channels:int, filtr:(tuple,list), sqrt=False,noB=0, KCD=False, verbose=False,stride=1, pad=0, initW=initializers.GlorotUniform(scale=1.2,dtype=np.float32), initB=initializers.GlorotUniform(scale=1.2,dtype=np.float32),bias_dept=2, **kwargs): """ input channels, number of outputs window """ super(Convar2D, 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 filter is None: # out_channels, ksize, in_channels = in_channels, out_channels, None self.filter = filtr self.sqrt=sqrt self.noB=noB self.V=verbose self.KCD=KCD 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(initW) self.W = variable.Parameter(initW) if in_channels is not None: self._initialize_params(in_channels) if noB: self.b = None else: if initB is None: initB = 0 #bias_initializer = initializers._get_initializer(initB) self.b = variable.Parameter(initB, (self.out_channel))#out_channels)
def calc_activation(self, x, layers=None, **kwargs): """calc_activation(self, x, layers=['fc6']) Computes all the feature maps specified by ``layers``. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. It should be prepared by ``prepare`` function. layers (list of str): The list of layer names you want to extract. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['fc6'] argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) functions_ordered_dict = self.base.functions() for key, funcs in functions_ordered_dict.items(): #print(key) #print(funcs) #print('h',h.shape,type(h)) if len(target_layers) == 0: break for func in funcs: #print(func) h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) #print('calc_activation,finished') #print(activations) return activations
def __init__(self, in_channels, out_channels, ksize=None, stride=1, pad=0, activation=relu.relu, *args, **kwargs): # If `args` is not empty, users assume the API for v1 and # specify `wscale` as a positonal argument, which we want # to detect and forbid with an explicit error message. msg = ('wscale is not supported anymore. ' 'Use conv_init and bias_init argument to change ' 'the scale of initial parameters.') if args: raise TypeError(msg) argument.check_unexpected_kwargs(kwargs, wscale=msg) conv_init, bias_init = argument.parse_kwargs(kwargs, ('conv_init', None), ('bias_init', None)) if ksize is None: out_channels, ksize, in_channels = in_channels, out_channels, None assert len(out_channels) > 0 convs = [ convolution_2d.Convolution2D(in_channels, out_channels[0], ksize, stride, pad, initialW=conv_init, initial_bias=bias_init) ] for n_in, n_out in zip(out_channels, out_channels[1:]): convs.append( convolution_2d.Convolution2D(n_in, n_out, 1, initialW=conv_init, initial_bias=bias_init)) super(MLPConvolution2D, self).__init__(*convs) self.activation = activation
def __init__(self, data=None, **kwargs): argument.check_unexpected_kwargs( kwargs, volatile='volatile argument is not supported anymore. ' 'Use chainer.using_config') name, grad, requires_grad \ = argument.parse_kwargs( kwargs, ('name', None), ('grad', None), ('requires_grad', True)) if (data is not None and not isinstance(data, (numpy.ndarray, cuda.ndarray))): msg = '''numpy.ndarray or cuda.ndarray are expected. Actual: {0}'''.format(type(data)) raise TypeError(msg) # Use a list as a data structure to hold the data array indirectly to # abstract its initialized/uninitialized state. self._data = [data] self._requires_grad = requires_grad self._node = VariableNode(self, name, grad)
def __init__(self, n_layers, states, rnn_dir, rnn_mode, **kwargs): argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if rnn_dir not in _rnn_dirs: candidate_list = ','.join(_rnn_dirs.keys()) raise ValueError('Invalid rnn_dir: "%s". Please select from [%s]' % (rnn_dir, candidate_list)) if rnn_mode not in _rnn_modes: candidate_list = ','.join(_rnn_modes.keys()) raise ValueError('Invalid rnn_mode: "%s". Please select from [%s]' % (rnn_mode, candidate_list)) self.rnn_dir = _rnn_dirs[rnn_dir] self.rnn_mode = _rnn_modes[rnn_mode] self.rnn_direction = _rnn_params_direction[self.rnn_dir] self.n_layers = n_layers self.states = states self.use_cell = _rnn_params_use_cell[self.rnn_mode] self.n_W = _rnn_n_params[self.rnn_mode]
def __init__(self, n_layers, in_size, out_size, dropout, initialW, initial_bias, use_bi_direction, **kwargs): argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if initial_bias is None: initial_bias = initializers.constant.Zero() initialW = initializers._get_initializer(initialW) weights = [] direction = 2 if use_bi_direction else 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(8): if i == 0 and j < 4: w_in = in_size elif i > 0 and j < 4: w_in = out_size * direction else: w_in = out_size name_w = 'w{}'.format(j) name_b = 'b{}'.format(j) w = variable.Parameter(initialW, (out_size, w_in)) b = variable.Parameter(initial_bias, (out_size,)) setattr(weight, name_w, w) setattr(weight, name_b, b) weights.append(weight) super(NStepLSTMBase, self).__init__(*weights) self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction self.rnn = rnn.n_step_bilstm if use_bi_direction else rnn.n_step_lstm
def __init__(self, n_layers, in_size, out_size, dropout, initialW, initial_bias, use_bi_direction, **kwargs): argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if initial_bias is None: initial_bias = initializers.constant.Zero() initialW = initializers._get_initializer(initialW) weights = [] direction = 2 if use_bi_direction else 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(8): if i == 0 and j < 4: w_in = in_size elif i > 0 and j < 4: w_in = out_size * direction else: w_in = out_size name_w = 'w{}'.format(j) name_b = 'b{}'.format(j) w = variable.Parameter(initialW, (out_size, w_in)) b = variable.Parameter(initial_bias, (out_size, )) setattr(weight, name_w, w) setattr(weight, name_b, b) weights.append(weight) super(NStepLSTMBase, self).__init__(*weights) self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction self.rnn = rnn.n_step_bilstm if use_bi_direction else rnn.n_step_lstm
def __call__(self, x, layers=['prob'], **kwargs): """__call__(self, x, layers=['prob']) Computes all the feature maps specified by ``layers``. .. warning:: ``test`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: x (~chainer.Variable): Input variable. layers (list of str): The list of layer names you want to extract. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) for key, funcs in self.functions.items(): if len(target_layers) == 0: break for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) return activations
def forward(self, x, layers=None, **kwargs): """forward(self, x, layers=['prob']) Computes all the feature maps specified by ``layers``. Args: x (~chainer.Variable): Input variable. It should be prepared by ``prepare`` function. layers (list of str): The list of layer names you want to extract. Returns: Dictionary of ~chainer.Variable: A directory in which the key contains the layer name and the value contains the corresponding feature map variable. """ if layers is None: layers = ['prob'] if kwargs: argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) for key, funcs in self.functions.items(): if not target_layers: break for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) return activations
def forward(self, x, layers=None, **kwargs): if layers is None: layers = ['prob'] if kwargs: argument.check_unexpected_kwargs( kwargs, test='test argument is not supported anymore. ' 'Use chainer.using_config' ) argument.assert_kwargs_empty(kwargs) h = x activations = {} target_layers = set(layers) for key, funcs in self.functions.items(): if len(target_layers) == 0: break for func in funcs: h = func(h) if key in target_layers: activations[key] = h target_layers.remove(key) return activations
def extend(self, extension, name=None, trigger=None, priority=None, **kwargs): argument.check_unexpected_kwargs( kwargs, invoke_before_training='invoke_before_training has been removed ' 'since Chainer v2.0.0. Use initializer= instead.') argument.assert_kwargs_empty(kwargs) if name is None: name = getattr(extension, 'name', None) if name is None: name = getattr(extension, 'default_name', None) if name is None: name = getattr(extension, '__name__', None) if name is None: raise TypeError('name is not given for the extension') if name == 'training': raise ValueError( 'the name "training" is prohibited as an extension name') if trigger is None: trigger = getattr(extension, 'trigger', (1, 'iteration')) trigger = trigger_module.get_trigger(trigger) if priority is None: priority = getattr( extension, 'priority', extension_module.PRIORITY_READER) modified_name = name ordinal = 0 while modified_name in self._extensions: ordinal += 1 modified_name = '%s_%d' % (name, ordinal) extension.name = modified_name self._extensions[modified_name] = _ExtensionEntry( extension, priority, trigger)
def zoneout(h, x, ratio=.5, **kwargs): """zoneout(h, x, ratio=.5) Drops elements of input variable and sets to previous variable randomly. This function drops input elements randomly with probability ``ratio`` and instead sets dropping element to their previous variable. In testing mode , it does nothing and just returns ``x``. .. warning:: ``train`` argument is not supported anymore since v2. Instead, use ``chainer.using_config('train', train)``. See :func:`chainer.using_config`. Args: h (:class:`~chainer.Variable` or :ref:`ndarray`): Previous variable. x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable. ratio (float): Zoneout ratio. Returns: ~chainer.Variable: Output variable. See the paper: `Zoneout: Regularizing RNNs by Randomly Preserving Hidden \ Activations <https://arxiv.org/abs/1606.01305>`_. """ if kwargs: argument.check_unexpected_kwargs( kwargs, train='train argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) if configuration.config.train: return Zoneout(ratio).apply((h, x))[0] return x
def __init__(self, n_layers, in_size, out_size, dropout, use_bi_direction, **kwargs): argument.check_unexpected_kwargs( kwargs, use_cudnn='use_cudnn argument is not supported anymore. ' 'Use chainer.using_config') argument.assert_kwargs_empty(kwargs) weights = [] direction = 2 if use_bi_direction else 1 for i in six.moves.range(n_layers): for di in six.moves.range(direction): weight = link.Link() with weight.init_scope(): for j in six.moves.range(8): if i == 0 and j < 4: w_in = in_size elif i > 0 and j < 4: w_in = out_size * direction else: w_in = out_size w = variable.Parameter( normal.Normal(numpy.sqrt(1. / w_in)), (out_size, w_in)) b = variable.Parameter(0, (out_size, )) setattr(weight, 'w%d' % j, w) setattr(weight, 'b%d' % j, b) weights.append(weight) super(NStepLSTMBase, self).__init__(*weights) self.n_layers = n_layers self.dropout = dropout self.out_size = out_size self.direction = direction self.rnn = rnn.n_step_bilstm if use_bi_direction else rnn.n_step_lstm