本文整理汇总了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
示例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)
示例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
示例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)
示例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))
示例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))
示例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 ##########################
#################################################################
示例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]
)
示例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
示例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
示例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))
示例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)
示例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))
示例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))
示例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)