Esempio n. 1
0
    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
        backends.copyto(dst, src)
Esempio n. 2
0
    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
        backends.copyto(dst, src)
Esempio n. 3
0
    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):
                    backends.copyto(d, s)
                else:
                    dst[name] = copy.deepcopy(s)
Esempio n. 4
0
 def test_fail_on_invalid_dst(self):
     src = numpy.zeros(1)
     dst = None
     with self.assertRaises(TypeError):
         backends.copyto(dst, src)
Esempio n. 5
0
 def test_gpu_to_another_gpu(self):
     src = cuda.cupy.array(self.src_data)
     with cuda.get_device_from_id(1):
         dst = self._get_dst()
     backends.copyto(dst, src)
     cuda.cupy.testing.assert_array_equal(dst, src)
Esempio n. 6
0
 def test_from_ideep(self):
     src = intel64.ideep.array(self.src_data)
     dst = self._get_dst()
     assert isinstance(src, intel64.mdarray)
     backends.copyto(dst, src)
     numpy.testing.assert_array_equal(cuda.to_cpu(dst), self.src_data)
Esempio n. 7
0
 def test_from_gpu(self):
     src = cuda.cupy.array(self.src_data)
     dst = self._get_dst()
     backends.copyto(dst, src)
     numpy.testing.assert_array_equal(cuda.to_cpu(dst), self.src_data)