def copyparams(self, link, copy_persistent=True): """Copies all parameters from given link. This method copies data arrays of all parameters in the hierarchy. The copy is even done across the host and devices. Note that this method does not copy the gradient arrays. *From v5.0.0:* this method also copies the persistent values (e.g. the moving statistics of :class:`~chainer.links.BatchNormalization`). If the persistent value is an ndarray, the elements are copied. Otherwise, it is copied using :func:`copy.deepcopy`. The old behavior (not copying persistent values) can be reproduced with ``copy_persistent=False``. Args: link (Link): Source link object. copy_persistent (bool): If ``True``, persistent values are also copied. ``True`` by default. """ src = link.__dict__ dst = self.__dict__ for name in self._params: dst[name].copydata(src[name]) if copy_persistent: array_types = chainer.get_array_types() for name in self._persistent: d = dst[name] s = src[name] if isinstance(d, array_types) and isinstance(s, array_types): cuda.copyto(d, s) else: dst[name] = copy.deepcopy(s)
def copydata(self, var): """Copies the data array from given source variable. This method copies the data array from given variable to this variable. The copy is done even if the arrays reside on different devices, including across the host and a GPU device. If this variable has an uninitialized data array, this method initializes it by the data array of the given variable. Similarly, if the given variable has an uninitialized data array, this method initializes it by the data array of this variable (``self``). If both are uninitialized, this method does nothing. Args: var (Variable): Source variable. """ src = var.data dst = self.data if src is None: if dst is None: return var.initialize(self.shape) src = var.data elif dst is None: self.initialize(src.shape) dst = self.data cuda.copyto(dst, src)
def test_fail_on_invalid_dst(self): src = numpy.zeros(1) dst = None with self.assertRaises(TypeError): cuda.copyto(dst, src)
def test_gpu_to_another_gpu(self): src = cuda.cupy.arange(1, 5, dtype=numpy.float32) with cuda.get_device_from_id(1): dst = cuda.cupy.zeros_like(src) cuda.copyto(dst, src) cuda.cupy.testing.assert_array_equal(dst, src)
def test_gpu_to_gpu(self): src = cuda.cupy.arange(1, 5, dtype=numpy.float32) dst = cuda.cupy.zeros_like(src) cuda.copyto(dst, src) cuda.cupy.testing.assert_array_equal(dst, src)