我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用chainer.cuda.ndarray()。
def visualize_layer_activations(model, im, layer_idx): """Compute the activations for each feature map for the given layer for this particular image. Note that the input x should be a mini-batch of size one, i.e. a single image. """ if model._device_id is not None and model._device_id >= 0: # Using GPU im = cuda.cupy.array(im) activations = model.activations(Variable(im), layer_idx) if isinstance(activations, cuda.ndarray): activations = cuda.cupy.asnumpy(activations) # Rescale to [0, 255] activations -= activations.min() activations /= activations.max() activations *= 255 return activations.astype(np.uint8)
def to_cpu(self): """Copies parameter variables and persistent values to CPU. This method does not handle non-registered attributes. If some of such attributes must be copied to CPU, the link implementation must override this method to do so. Returns: self """ if self._cpu: return self d = self.__dict__ for name in self._params: d[name].to_cpu() for name in self._persistent: value = d[name] if isinstance(value, cuda.ndarray): d[name] = value.get() self._cpu = True return self
def __init__(self, in_size, out_size, pool_size, wscale=1, initialW=None, initial_bias=0): linear_out_size = out_size * pool_size if initialW is not None: initialW = initialW.reshape(linear_out_size, in_size) if initial_bias is not None: if numpy.isscalar(initial_bias): initial_bias = numpy.full( (linear_out_size,), initial_bias, dtype=numpy.float32) elif isinstance(initial_bias, (numpy.ndarray, cuda.ndarray)): initial_bias = initial_bias.reshape(linear_out_size) else: raise ValueError( 'initial bias must be float, ndarray, or None') super(Maxout, self).__init__( linear=linear.Linear( in_size, linear_out_size, wscale, nobias=initial_bias is None, initialW=initialW, initial_bias=initial_bias)) self.out_size = out_size self.pool_size = pool_size
def forward(self, inputs): """Applies forward propagation to input arrays. It delegates the procedure to :meth:`forward_cpu` or :meth:`forward_gpu` by default. Which it selects is determined by the type of input arrays. Implementations of :class:`Function` must implement either CPU/GPU methods or this method. Args: inputs: Tuple of input array(s). Returns: Tuple of output array(s). .. warning:: Implementations of :class:`Function` must take care that the return value must be a tuple even if it returns only one array. """ if any(isinstance(x, cuda.ndarray) for x in inputs): return self.forward_gpu(inputs) else: return self.forward_cpu(inputs)
def forward_cpu(self, inputs): """Applies forward propagation to input arrays on CPU. Args: inputs: Tuple of :class:`numpy.ndarray` object(s). Returns: tuple: Tuple of :class:`numpy.ndarray` object(s). .. warning:: Implementations of :class:`Function` must take care that the return value must be a tuple even if it returns only one array. """ raise NotImplementedError()
def backward_cpu(self, inputs, grad_outputs): """Applies backprop to output gradient arrays on CPU. Args: inputs: Tuple of input :class:`numpy.ndarray` object(s). grad_outputs: Tuple of output gradient :class:`numpy.ndarray` object(s). Returns: tuple: Tuple of input gradient :class:`numpy.ndarray` object(s). Some or all of them can be ``None``, if the function is not differentiable on corresponding inputs. .. warning:: Implementations of :class:`Function` must take care that the return value must be a tuple even if it returns only one array. """ return tuple(None for _ in inputs)
def backward_gpu(self, inputs, grad_outputs): """Applies backprop to output gradient arrays on GPU. Args: inputs: Tuple of input :class:`cupy.ndarray` object(s). grad_outputs: Tuple of output gradient :class:`cupy.ndarray` object(s). Returns: tuple: Tuple of input gradient :class:`cupy.ndarray` object(s). Some or all of them can be ``None``, if the function is not differentiable on corresponding inputs. .. warning:: Implementations of :class:`Function` must take care that the return value must be a tuple even if it returns only one array. """ return tuple(None for _ in inputs)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, wscale=1, bias=0, nobias=False, outsize=None, use_cudnn=True, initialV=None, dtype=np.float32): kh, kw = _pair(ksize) self.stride = _pair(stride) self.pad = _pair(pad) self.outsize = (None, None) if outsize is None else outsize self.use_cudnn = use_cudnn self.dtype = dtype self.nobias = nobias self.out_channels = out_channels self.in_channels = in_channels V_shape = (in_channels, out_channels, kh, kw) super(Deconvolution2D, self).__init__(V=V_shape) if isinstance(initialV, (np.ndarray, cuda.ndarray)): assert initialV.shape == (in_channels, out_channels, kh, kw) initializers.init_weight(self.V.data, initialV, scale=math.sqrt(wscale)) if nobias: self.b = None else: self.add_uninitialized_param("b") self.add_uninitialized_param("g")
def __init__(self, left_size, right_size, out_size, nobias=(False, False, False), initialW=None, initial_bias=None): super(Biaffine, self).__init__() self.in_sizes = (left_size, right_size) self.out_size = out_size self.nobias = nobias with self.init_scope(): shape = (left_size + int(not(self.nobias[0])), right_size + int(not(self.nobias[1])), out_size) if isinstance(initialW, (np.ndarray, cuda.ndarray)): assert initialW.shape == shape self.W = variable.Parameter( initializers._get_initializer(initialW), shape) if not self.nobias[2]: if initial_bias is None: initial_bias = 0 self.b = variable.Parameter(initial_bias, (self.out_size,)) else: self.b = None
def check_add_deconv_layers(self, nobias=True): """Add a deconvolutional layer for each convolutional layer already defined in the network.""" if len(self.deconv_blocks) == len(self.conv_blocks): return for conv_block in self.conv_blocks: deconv_block = [] for conv in conv_block: out_channels, in_channels, kh, kw = conv.W.data.shape if isinstance(conv.W.data, cuda.ndarray): initialW = cuda.cupy.asnumpy(conv.W.data) else: initialW = conv.W.data deconv = L.Deconvolution2D(out_channels, in_channels, (kh, kw), stride=conv.stride, pad=conv.pad, initialW=initialW, nobias=nobias) if isinstance(conv.W.data, cuda.ndarray): deconv.to_gpu() self.add_link('de{}'.format(conv.name), deconv) deconv_block.append(deconv) self.deconv_blocks.append(deconv_block)
def sample(self, trainer): x = trainer.updater.sample() x = x.data if cuda.available and isinstance(x, cuda.ndarray): x = cuda.to_cpu(x) return x
def __call__(self, key, value): ret = value if isinstance(value, cuda.ndarray): value = cuda.to_cpu(value) arr = numpy.asarray(value) compression = None if arr.size <= 1 else self.compression self.group.create_dataset(key, data=arr, compression=compression) return ret
def __call__(self, key, value): dataset = self.group[key] if isinstance(value, numpy.ndarray): dataset.read_direct(value) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset)) else: value = type(value)(numpy.asarray(dataset)) return value
def __call__(self, key, value): key = key.lstrip('/') ret = value if isinstance(value, cuda.ndarray): value = value.get() arr = numpy.asarray(value) self.target[self.path + key] = arr return ret
def __call__(self, key, value): key = key.lstrip('/') dataset = self.npz[self.path + key] if isinstance(value, numpy.ndarray): numpy.copyto(value, dataset) elif isinstance(value, cuda.ndarray): value.set(numpy.asarray(dataset)) else: value = type(value)(numpy.asarray(dataset)) return value
def __init__(self, left_size, right_size, out_size, nobias=False, initialW=None, initial_bias=None): super(Bilinear, self).__init__(W=(left_size, right_size, out_size)) self.in_sizes = (left_size, right_size) self.nobias = nobias # TODO(Kenta OONO): I do not know appropriate way of # initializing weights in tensor network. # This initialization is a modification of # that of Linear function. if isinstance(initialW, (numpy.ndarray, cuda.ndarray)): assert initialW.shape == self.W.data.shape initializers.init_weight(self.W.data, initialW) if not self.nobias: self.add_param('V1', (left_size, out_size)) self.add_param('V2', (right_size, out_size)) self.add_param('b', out_size) if isinstance(initial_bias, tuple): V1, V2, b = initial_bias elif initial_bias is None: V1 = V2 = None b = 0 else: raise ValueError('initial_bias must be tuple or None') if isinstance(V1, (numpy.ndarray, cuda.ndarray)): assert V1.shape == self.V1.data.shape if isinstance(V2, (numpy.ndarray, cuda.ndarray)): assert V2.shape == self.V2.data.shape if isinstance(b, (numpy.ndarray, cuda.ndarray)): assert b.shape == self.b.data.shape initializers.init_weight(self.V1.data, V1) initializers.init_weight(self.V2.data, V2) initializers.init_weight(self.b.data, b)
def __init__(self, array): super(Parameter, self).__init__() self.add_param('W', array.shape, dtype=array.dtype) self.W.data = array if isinstance(array, cuda.ndarray): self.to_gpu(array)
def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0, wscale=1, bias=0, nobias=False, outsize=None, use_cudnn=True, initialW=None, initial_bias=None): kh, kw = _pair(ksize) self.stride = _pair(stride) self.pad = _pair(pad) self.outsize = (None, None) if outsize is None else outsize self.use_cudnn = use_cudnn W_shape = (in_channels, out_channels, kh, kw) super(Deconvolution2D, self).__init__(W=W_shape) if isinstance(initialW, (numpy.ndarray, cuda.ndarray)): assert initialW.shape == (in_channels, out_channels, kh, kw) # For backward compatibility, the scale of weights is proportional to # the square root of wscale. initializers.init_weight(self.W.data, initialW, scale=math.sqrt(wscale)) if nobias: self.b = None else: self.add_param('b', out_channels) if isinstance(initial_bias, (numpy.ndarray, cuda.ndarray)): assert initial_bias.shape == (out_channels,) if initial_bias is None: initial_bias = bias initializers.init_weight(self.b.data, initial_bias)
def prepare(self): """Prepares for an update. This method initializes missing optimizer states (e.g. for newly added parameters after the set up), and copies arrays in each state dictionary to CPU or GPU according to the corresponding parameter array. """ states = self._states for name, param in self.target.namedparams(): if name not in states: state = {} self.init_state(param, state) states[name] = state else: state = states[name] with cuda.get_device(param.data) as dev: if int(dev) == -1: # cpu for key, value in six.iteritems(state): if isinstance(value, cuda.ndarray): state[key] = value.get() else: # gpu cupy = cuda.cupy for key, value in six.iteritems(state): if isinstance(value, numpy.ndarray): state[key] = cuda.to_gpu(value) elif (isinstance(value, cupy.ndarray) and value.device != dev): state[key] = cupy.copy(value)
def init_state_gpu(self, param, state): """Initializes the optimizer state on GPU. This method is called from :meth:`init_state` by default. Args: param (~chainer.Variable): Parameter variable. Its data array is of type :class:`cupy.ndarray`. state (dict): State dictionary. .. seealso:: :meth:`init_state` """ pass
def accumulate_grads(self, grads): """Accumulates gradients from other source. This method just adds given gradient arrays to gradients that this optimizer holds. It is typically used in data-parallel optimization, where gradients for different shards are computed in parallel and aggregated by this method. This method correctly treats multiple GPU devices. Args: grads (Iterable): Iterable of gradient arrays to be accumulated. .. deprecated:: v1.5 Use the :meth:`chainer.Link.addgrads` method of the target link instead. """ for param, g_src in zip(self.target.params(), grads): g_dst = param.grad if isinstance(g_dst, numpy.ndarray): g_dst += cuda.to_cpu(g_src) continue with cuda.get_device(g_dst): if (isinstance(g_src, cuda.ndarray) and g_dst.device != g_src.device): g_dst += cuda.copy(g_src, out_device=g_dst.device) else: g_dst += cuda.to_gpu(g_src)
def update_one(self, param, state): """Updates a parameter based on the corresponding gradient and state. This method calls appropriate one from :meth:`update_param_cpu` or :meth:`update_param_gpu`. Args: param (~chainer.Variable): Parameter variable. state (dict): State dictionary. """ if isinstance(param.data, numpy.ndarray): self.update_one_cpu(param, state) else: self.update_one_gpu(param, state)
def backward(self, inputs, grad_outputs): # In this function, `grad_outputs` contains cuda arrays even when # `inputs` only contains numpy arrays. if isinstance(inputs[0], cuda.ndarray): return self.backward_gpu(inputs, grad_outputs) else: return self.backward_cpu(inputs, grad_outputs)
def _check_constant_type(value): if numpy.isscalar(value): return elif isinstance(value, (numpy.ndarray, cuda.ndarray)): return else: raise ValueError( 'value must be scalar, ndarray, or Variable')
def empty_like(x): if cuda.available and isinstance(x, cuda.ndarray): return cuda.cupy.empty_like(x) else: return numpy.empty_like(x)
def _get_type(name, index, array, accept_none): var = '{0}[{1}]'.format(name, index) if accept_none and array is None: # case that gradient is not given return Variable(TypeInfo((), None), var) assert(isinstance(array, numpy.ndarray) or isinstance(array, cuda.ndarray)) return Variable(TypeInfo(array.shape, array.dtype), var)
def backward(self, inputs, grad_outputs): """Applies backprop to output gradient arrays. It delegates the procedure to :meth:`backward_cpu` or :meth:`backward_gpu` by default. Which it selects is determined by the type of input arrays and output gradient arrays. Implementations of :class:`Function` must implement either CPU/GPU methods or this method, if the function is intended to be backprop-ed. Args: inputs: Tuple of input arrays. grad_outputs: Tuple of output gradient arrays. Returns: tuple: Tuple of input gradient arrays. Some or all of them can be ``None``, if the function is not differentiable on inputs. .. warning:: Implementations of :class:`Function` must take care that the return value must be a tuple even if it returns only one array. """ if any(isinstance(x, cuda.ndarray) for x in inputs + grad_outputs): return self.backward_gpu(inputs, grad_outputs) else: return self.backward_cpu(inputs, grad_outputs)
def forward_preprocess(self, function, in_data): """Callback function invoked before forward propagation. Args: function(~chainer.Function): Function object to which the function hook is registered. in_data(tuple of numpy.ndarray or tuple of cupy.ndarray): Input data of forward propagation. """ pass
def forward_postprocess(self, function, in_data): """Callback function invoked after forward propagation. Args: function(~chainer.Function): Function object to which the function hook is registered. in_data(tuple of numpy.ndarray or tuple of cupy.ndarray): Input data of forward propagation. """ pass # backward
def backward_postprocess(self, function, in_data, out_grad): """Callback function invoked after backward propagation. Args: function(~chainer.Function): Function object to which the function hook is registered. in_data(tuple of numpy.ndarray or tuple of cupy.ndarray): Input of forward propagation. out_grad(tuple of numpy.ndarray or tuple of cupy.ndarray): Gradient data of backward propagation. """ pass
def __init__(self, data, volatile=flag.OFF, name=None): if not isinstance(data, (numpy.ndarray, cuda.ndarray)): msg = '''numpy.ndarray or cuda.ndarray are expected. Actual: {0}'''.format(type(data)) raise TypeError(msg) self.data = data self.rank = 0 self._volatile = flag.Flag(volatile) self._grad = None self.creator = None self.name = name
def addgrad(self, var): """Accumulates the gradient array from given source variable. This method just runs ``self.grad += var.grad``, except that the accumulation is even done across the host and different devices. Args: var (Variable): Source variable. """ src = var._grad dst = self._grad if src is None: raise ValueError('Source gradient is not set.') if dst is None: raise ValueError('Target graidient is not set.') xp = cuda.get_array_module(dst) if xp is numpy: dst += cuda.to_cpu(src) elif isinstance(src, numpy.ndarray): dst += cuda.to_gpu(src, device=dst) else: dst_dev = dst.device if dst_dev == src.device: dst += src else: with dst_dev: dst += xp.copy(src)
def test_array_gpu(self): self._check_array(cuda.ndarray([1, 2]), 'constant array')
def __init__(self, *args, dropout=0.0): embeds = [] self.size = 0 for i, _args in enumerate(args): if isinstance(_args, dict): vocab_size = _args.get('in_size', None) embed_size = _args.get('out_size', None) embeddings = _args.get('initialW', None) if vocab_size is None or embed_size is None: if embeddings is None: raise ValueError('embeddings or in_size/out_size ' 'must be specified') vocab_size, embed_size = embeddings.shape _args['in_size'] = vocab_size _args['out_size'] = embed_size else: if isinstance(_args, np.ndarray): vocab_size, embed_size = _args.shape embeddings = _args elif isinstance(_args, tuple) and len(embeddings) == 2: vocab_size, embed_size = _args embeddings = None else: raise ValueError('embeddings must be ' 'np.ndarray or tuple(len=2)') _args = {'in_size': vocab_size, 'out_size': embed_size, 'initialW': embeddings} embeds.append(EmbedID(**_args)) self.size += embed_size super(Embed, self).__init__(*embeds) assert dropout == 0 or type(dropout) == float self._dropout_ratio = dropout
def to_numpy(self, x): if isinstance(x, Variable) == True: x = x.data if isinstance(x, cuda.ndarray) == True: x = cuda.to_cpu(x) return x
def to_numpy(self, x): if isinstance(x, Variable) == True: x.to_cpu() x = x.data if isinstance(x, cuda.ndarray) == True: x = cuda.to_cpu(x) return x
def __call__(self, trainer): x = self.x dpi = self.dpi updater = trainer.updater filename = os.path.join(trainer.out, '{0:08d}.png'.format( updater.iteration)) # Inference to update model internal grid x = updater.converter(x, updater.device) model = updater.get_optimizer('main').target.predictor model(x) # Get grids from previous inference grid = model.st.grid.data if isinstance(grid, cuda.ndarray): grid = cuda.to_cpu(grid) if isinstance(x, cuda.ndarray): x = cuda.to_cpu(x) n, c, w, h = x.shape x_plots = math.ceil(math.sqrt(n)) y_plots = x_plots if n % x_plots == 0 else x_plots - 1 plt.figure(figsize=(w*x_plots/dpi, h*y_plots/dpi), dpi=dpi) for i, im in enumerate(x): plt.subplot(y_plots, x_plots, i+1) if c == 1: plt.imshow(im[0]) else: plt.imshow(im.transpose((1, 2, 0))) plt.axis('off') plt.gca().set_xticks([]) plt.gca().set_yticks([]) plt.gray() # Get the 4 corners of the transformation grid to draw a box g = grid[i] vs = np.empty((4, 2), dtype=np.float32) vs[0] = g[:, 0, 0] vs[1] = g[:, 0, w-1] vs[2] = g[:, h-1, w-1] vs[3] = g[:, h-1, 0] vs += 1 # [-1, 1] -> [0, 2] vs /= 2 vs[:, 0] *= h vs[:, 1] *= w bbox = plt.Polygon(vs, True, color='r', fill=False, linewidth=0.8, alpha=0.8) plt.gca().add_patch(bbox) bbox.set_clip_on(False) # Allow drawing outside axes plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.2, hspace=0.2) plt.savefig(filename, dpi=dpi*2, facecolor='black') plt.clf() plt.close()
def numerical_grad(f, inputs, grad_outputs, eps=1e-3): """Computes numerical gradient by finite differences. This function is used to implement gradient check. For usage example, see unit tests of :mod:`chainer.functions`. Args: f (function): Python function with no arguments that runs forward computation and returns the result. inputs (tuple of arrays): Tuple of arrays that should be treated as inputs. Each element of them is slightly modified to realize numerical gradient by finite differences. grad_outputs (tuple of arrays): Tuple of arrays that are treated as output gradients. eps (float): Epsilon value of finite differences. Returns: tuple: Numerical gradient arrays corresponding to ``inputs``. """ assert eps > 0 inputs = tuple(inputs) grad_outputs = tuple(grad_outputs) gpu = any(isinstance(x, cuda.ndarray) for x in inputs + grad_outputs) cpu = any(isinstance(x, numpy.ndarray) for x in inputs + grad_outputs) if gpu and cpu: raise RuntimeError('Do not mix GPU and CPU arrays in `numerical_grad`') if gpu: xp = cuda.cupy else: xp = numpy grads = tuple(xp.zeros_like(x) for x in inputs) for x, gx in zip(inputs, grads): for i in numpy.ndindex(x.shape): orig = x[i].copy() # hold original value x[i] = orig + eps ys1 = _copy_arrays(f()) x[i] = orig - eps ys2 = _copy_arrays(f()) x[i] = orig for y1, y2, gy in zip(ys1, ys2, grad_outputs): if gy is not None: dot = ((y1 - y2) * gy).sum() gx[i] += dot / (2 * eps) return grads