Python chainer.cuda 模块,get_device_from_array() 实例源码

我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用chainer.cuda.get_device_from_array()

项目:DSTC6-End-to-End-Conversation-Modeling    作者:dialogtekgeek    | 项目源码 | 文件源码
def update(self, s, i):
        """Update decoder state

        Args:
            s (any): Current (hidden, cell) states.  If ``None`` is specified 
                     zero-vector is used.
            i (int): input label.
        Return:
            (~chainer.Variable) updated decoder state
        """
        if cuda.get_device_from_array(s[0].data).id >= 0:
            xp = cuda.cupy
        else:
            xp = np

        v = chainer.Variable(xp.array([i],dtype=np.int32))
        x = self.embed(v)
        if s is not None:
            hy, cy, dy = self.lstm(s[0], s[1], [x])
        else:
            hy, cy, dy = self.lstm(None, None, [x])

        return hy, cy, dy
项目:DSTC6-End-to-End-Conversation-Modeling    作者:dialogtekgeek    | 项目源码 | 文件源码
def update(self, s, i):
        """Update decoder state

        Args:
            s (any): Current (hidden, cell) states.  If ``None`` is specified 
                     zero-vector is used.
            i (int): input label.
        Return:
            (~chainer.Variable) updated decoder state
        """
        if cuda.get_device_from_array(s[0].data).id >= 0:
            xp = cuda.cupy
        else:
            xp = np

        v = chainer.Variable(xp.array([i],dtype=np.int32))
        x = self.embed(v)
        if s is not None:
            hy, cy, dy = self.lstm(s[0], s[1], [x])
        else:
            hy, cy, dy = self.lstm(None, None, [x])

        return hy, cy, dy
项目:DSTC6-End-to-End-Conversation-Modeling    作者:dialogtekgeek    | 项目源码 | 文件源码
def update(self, s, i):
        """Update decoder state

        Args:
            s (any): Current (hidden, cell) states.  If ``None`` is specified 
                     zero-vector is used.
            i (int): input label.
        Return:
            (~chainer.Variable) updated decoder state
        """
        if cuda.get_device_from_array(s[0].data).id >= 0:
            xp = cuda.cupy
        else:
            xp = np

        v = chainer.Variable(xp.array([i],dtype=np.int32))
        x = self.embed(v)
        if s is not None:
            hy, cy, dy = self.lstm(s[0], s[1], [x])
        else:
            hy, cy, dy = self.lstm(None, None, [x])

        return hy, cy, dy
项目:DSTC6-End-to-End-Conversation-Modeling    作者:dialogtekgeek    | 项目源码 | 文件源码
def update(self, s, i):
        """Update decoder state

        Args:
            s (any): Current (hidden, cell) states.  If ``None`` is specified 
                     zero-vector is used.
            i (int): input label.
        Return:
            (~chainer.Variable) updated decoder state
        """
        if cuda.get_device_from_array(s[0].data).id >= 0:
            xp = cuda.cupy
        else:
            xp = np

        v = chainer.Variable(xp.array([i],dtype=np.int32))
        x = self.embed(v)
        if s is not None:
            hy, cy, dy = self.lstm(s[0], s[1], [x])
        else:
            hy, cy, dy = self.lstm(None, None, [x])

        return hy, cy, dy
项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
def init_state(self, param):
        xp = cuda.get_array_module(param.data)
        with cuda.get_device_from_array(param.data):
            self.state['ms'] = xp.zeros_like(param.data)
项目:chainer-neural-style    作者:dsanno    | 项目源码 | 文件源码
def init_state(self, param):
        with cuda.get_device_from_array(param.data):
            self.state['s'] = []
项目:adversarial-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def _sum_sqnorm(arr):
    sq_sum = collections.defaultdict(float)
    for x in arr:
        with cuda.get_device_from_array(x) as dev:
            x = x.ravel()
            s = x.dot(x)
            sq_sum[int(dev)] += s
    return sum([float(i) for i in six.itervalues(sq_sum)])
项目:adversarial-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def __call__(self, opt):
        norm = np.sqrt(_sum_sqnorm([p.grad for p in opt.target.params(False)]))
        if norm == 0:
            return
        rate = self.threshold / norm
        if rate < 1:
            for param in opt.target.params(False):
                grad = param.grad
                with cuda.get_device_from_array(grad):
                    grad *= rate
项目:chainercv    作者:chainer    | 项目源码 | 文件源码
def __call__(self, rule, param):
        g = param.grad
        with cuda.get_device_from_array(g):
            g *= self.rate
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
def backward(self, inputs, grad_outputs):
        assert self.comm.size == len(grad_outputs)

        xp = cuda.get_array_module(*inputs)
        with cuda.get_device_from_array(*inputs):
            gys = tuple([gy for gy in grad_outputs])
            gx = self.comm.alltoall(gys)
            gx = [xp.array(_gx) for _gx in gx]
            return tuple(gx)
项目:chainermn    作者:chainer    | 项目源码 | 文件源码
def backward(self, inputs, grad_outputs):
        xp = cuda.get_array_module(*inputs)
        with cuda.get_device_from_array(*inputs):
            grad = self.comm.recv(self.peer_rank, self.peer_tag)
            if isinstance(grad, tuple):
                return tuple([xp.array(gy) for gy in grad])
            else:
                return xp.array(grad),