当前位置: 首页>>代码示例>>Python>>正文


Python cuda.to_gpu方法代码示例

本文整理汇总了Python中chainer.cuda.to_gpu方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.to_gpu方法的具体用法?Python cuda.to_gpu怎么用?Python cuda.to_gpu使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer.cuda的用法示例。


在下文中一共展示了cuda.to_gpu方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test(self, cgp, model_file, comp_graph='comp_graph.dot', batchsize=256):
        chainer.cuda.get_device(0).use()  # Make a specified GPU current
        model = CGP2CNN(cgp, self.n_class)
        print('\tLoad model from', model_file)
        serializers.load_npz(model_file, model)
        model.to_gpu(0)
        test_accuracy, test_loss = self.__test(model, batchsize)
        print('\tparamNum={}'.format(model.param_num))
        print('\ttest mean loss={}, test accuracy={}'.format(test_loss / self.test_data_num, test_accuracy / self.test_data_num))

        if comp_graph is not None:
            with open(comp_graph, 'w') as o:
                g = computational_graph.build_computational_graph((model.loss,))
                o.write(g.dump())
                del g
                print('\tCNN graph generated ({}).'.format(comp_graph))

        return test_accuracy, test_loss 
开发者ID:sg-nm,项目名称:cgp-cnn,代码行数:20,代码来源:cnn_train.py

示例2: give_conditionalized_cell

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def give_conditionalized_cell(self, src_batch, src_mask, noise_on_prev_word=False,
                                  demux=False):

        if self.lexical_probability_dictionary is not None:
            lexicon_probability_matrix = compute_lexicon_matrix(
                src_batch, self.lexical_probability_dictionary, self.Vo)
            if self.xp != np:
                lexicon_probability_matrix = cuda.to_gpu(
                    lexicon_probability_matrix, cuda.get_device(
                        self.dec.lin_o.W.data))
        else:
            lexicon_probability_matrix = None

        fb_concat = self.enc(src_batch, src_mask)

        mb_size, nb_elems, Hi = fb_concat.data.shape

        return self.dec.give_conditionalized_cell(fb_concat, src_mask,
                                                  noise_on_prev_word=noise_on_prev_word,
                                                  lexicon_probability_matrix=lexicon_probability_matrix,
                                                  lex_epsilon=self.lex_epsilon, demux=demux) 
开发者ID:fabiencro,项目名称:knmt,代码行数:23,代码来源:encoder_decoder.py

示例3: get

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def get(self, n=None, shuffle=True, aug_trans=False, aug_flip=False, gpu=-1):
        if shuffle:
            ind = np.random.permutation(self.data.shape[0])
        else:
            ind = np.arange(self.data.shape[0])
        if n is None:
            n = self.data.shape[0]
        index = ind[:n]
        batch_data = self.data[index]
        batch_label = self.label[index]
        if aug_trans or aug_flip:
            batch_data = self._augmentation(batch_data, aug_trans, aug_flip)
        if gpu > -1:
            return cuda.to_gpu(batch_data, device=gpu), \
                   cuda.to_gpu(batch_label, device=gpu)
        else:
            return batch_data, batch_label 
开发者ID:takerum,项目名称:vat_chainer,代码行数:19,代码来源:data.py

示例4: check_log_prob

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def check_log_prob(self, is_gpu):
        smp = self.sample_for_test()
        if is_gpu:
            log_prob1 = self.gpu_dist.log_prob(cuda.to_gpu(smp)).data
        else:
            log_prob1 = self.cpu_dist.log_prob(smp).data

        onebyone_smp = smp.reshape(self.sample_shape + (-1,) + (self.k,))
        onebyone_smp = numpy.rollaxis(onebyone_smp, -2, 0)
        onebyone_smp = onebyone_smp.reshape(
            (-1,) + self.sample_shape + (self.k,))
        log_prob2 = []
        for one_params, one_smp in zip(
                self.scipy_onebyone_params_iter(), onebyone_smp):
            log_prob2.append(self.scipy_dist.logpmf(one_smp, **one_params))
        log_prob2 = _numpy_stack(log_prob2, axis=-1)
        log_prob2 = log_prob2.reshape(self.sample_shape + self.shape)
        testing.assert_allclose(log_prob1, log_prob2) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_one_hot_categorical.py

示例5: test_forward_cpu_gpu_equal

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_index_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_max_align_2d(
            x_cpu, rois_cpu, roi_index_cpu,
            outsize=self.outsize, spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_index_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_max_align_2d(
            x_gpu, rois_gpu, roi_index_gpu,
            outsize=self.outsize, spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:test_roi_max_align_2d.py

示例6: test_forward_cpu_gpu_equal

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        roi_indices_cpu = chainer.Variable(self.roi_indices)
        y_cpu = functions.roi_average_align_2d(
            x_cpu, rois_cpu, roi_indices_cpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        roi_indices_gpu = chainer.Variable(cuda.to_gpu(self.roi_indices))
        y_gpu = functions.roi_average_align_2d(
            x_gpu, rois_gpu, roi_indices_gpu, outsize=self.outsize,
            spatial_scale=self.spatial_scale,
            sampling_ratio=self.sampling_ratio,
        )
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:test_roi_average_align_2d.py

示例7: forward_gpu

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def forward_gpu(self, obs):
        """
        Performs forward pass on CPU, returns Q values
        """
        # unpack
        ohist, ahist = obs
        # move to gpu
        ohist, ahist = self.to_gpu(ohist), self.to_gpu(ahist)
        # transfer inputs into Chainer format
        ohist, ahist = self.chainer_var(ohist, volatile=True), self.chainer_var(ahist, volatile=True)
        # evaluate
        qvals = self.source_net(ohist, ahist)
        return qvals.data.get()

    #################################################################  
    #################### Utility Functions ##########################
    ################################################################# 
开发者ID:sisl,项目名称:Chimp,代码行数:19,代码来源:chainer_backend.py

示例8: convert

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def convert(batch, device):
    if device is None:
        def to_device(x):
            return x
    elif device < 0:
        to_device = cuda.to_cpu
    else:
        def to_device(x):
            return cuda.to_gpu(x, device, cuda.Stream.null)

    return tuple(
        [to_device(d['lefts']) for d in batch] +
        [to_device(d['rights']) for d in batch] +
        [to_device(d['dests']) for d in batch] +
        [to_device(d['labels']) for d in batch] +
        [to_device(d['words']) for d in batch] +
        [to_device(d['leaf_labels']) for d in batch]
    ) 
开发者ID:pfnet,项目名称:pfio,代码行数:20,代码来源:train_recursive_minibatch.py

示例9: __test

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def __test(self, model, batchsize):
        model.train = False
        test_accuracy = test_loss = 0
        for i in six.moves.range(0, self.test_data_num, batchsize):
            x = chainer.Variable(cuda.to_gpu(self.x_test[i:i + batchsize]), volatile=True)
            t = chainer.Variable(cuda.to_gpu(self.y_test[i:i + batchsize]), volatile=True)
            loss = model(x, t)
            test_loss += float(loss.data) * len(t.data)
            test_accuracy += float(model.accuracy.data) * len(t.data)
        model.train = True
        return test_accuracy, test_loss 
开发者ID:sg-nm,项目名称:cgp-cnn,代码行数:13,代码来源:cnn_train.py

示例10: to_device

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def to_device(x, device_id):
    if device_id is None:
        return x
    if device_id < 0 and isinstance(x, cupy.ndarray):
        return cuda.to_cpu(x)
    if device_id >= 0 and isinstance(x, numpy.ndarray):
        return cuda.to_gpu(x, device_id)
    return x 
开发者ID:musyoku,项目名称:chainer-gqn,代码行数:10,代码来源:__init__.py

示例11: test_forward_gpu

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_forward_gpu(self):
        self.check_forward(cuda.to_gpu(self.x)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:4,代码来源:test_invert_gradients.py

示例12: test_forward_gpu

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_forward_gpu(self):
        xs_gpu = [chainer.cuda.to_gpu(x) for x in self.xs]
        self.check_forward(xs_gpu) 
开发者ID:chainer,项目名称:chainerrl,代码行数:5,代码来源:test_weighted_sum_arrays.py

示例13: test_backward_gpu

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_backward_gpu(self):
        xs_gpu = [chainer.cuda.to_gpu(x) for x in self.xs]
        self.check_backward(xs_gpu, cuda.to_gpu(self.gy)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:5,代码来源:test_weighted_sum_arrays.py

示例14: test_backward_gpu

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def test_backward_gpu(self):
        self.check_backward((cuda.to_gpu(self.diag), cuda.to_gpu(
            self.non_diag)), cuda.to_gpu(self.gy)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:5,代码来源:test_lower_triangular_matrix.py

示例15: _diagonal_idx_array

# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import to_gpu [as 别名]
def _diagonal_idx_array(batch_size, n):
    idx_offsets = np.arange(
        start=0, stop=batch_size * n * n, step=n * n, dtype=np.int32).reshape(
        (batch_size, 1))
    idx = np.ravel_multi_index(
        np.diag_indices(n), (n, n)).reshape((1, n)).astype(np.int32)
    return cuda.to_gpu(idx + idx_offsets) 
开发者ID:chainer,项目名称:chainerrl,代码行数:9,代码来源:lower_triangular_matrix.py


注:本文中的chainer.cuda.to_gpu方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。