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))
python类to_gpu()的实例源码
def testplot_p4m(im=None, m=0, r=0):
if im is None:
im = np.zeros((5, 5), dtype='float32')
im[0:5, 1] = 1.
im[0, 1:4] = 1.
im[2, 1:3] = 1.
from groupy.gfunc.z2func_array import Z2FuncArray
from groupy.garray.D4_array import D4Array
def rotate_flip_z2_func(im, flip, theta_index):
imf = Z2FuncArray(im)
rot = D4Array([flip, theta_index], 'int')
rot_imf = rot * imf
return rot_imf.v
im = rotate_flip_z2_func(im, m, r)
filter_e = np.array([[-1., -4., 1.],
[-2., 0., 2.],
[-1., 0., 1.]])
from groupy.gconv.chainer_gconv.p4m_conv import P4MConvZ2
from chainer import Variable
from chainer import cuda
print im.shape
imv = Variable(cuda.to_gpu(im.astype('float32').reshape(1, 1, 5, 5)))
conv = P4MConvZ2(in_channels=1, out_channels=1, ksize=3, pad=2, flat_channels=True, initialW=filter_e.reshape(1, 1, 1, 3, 3))
conv.to_gpu()
conv_imv = conv(imv)
print im.shape, conv_imv.data.shape
return im, cuda.to_cpu(conv_imv.data)
def train_epoch(train_data, train_labels, model, optimizer, batchsize, transformations, silent, gpu=0, finetune=False):
N = train_data.shape[0]
pbar = ProgressBar(0, N)
perm = np.random.permutation(N)
sum_accuracy = 0
sum_loss = 0
for i in range(0, N, batchsize):
x_batch = train_data[perm[i:i + batchsize]]
y_batch = train_labels[perm[i:i + batchsize]]
if transformations is not None:
if 'rotation' == transformations:
x_batch = rotate_transform_batch(
x_batch,
rotation=2 * np.pi
)
if gpu >= 0:
x_batch = cuda.to_gpu(x_batch.astype(np.float32))
y_batch = cuda.to_gpu(y_batch.astype(np.int32))
optimizer.zero_grads()
x = Variable(x_batch)
t = Variable(y_batch)
loss, acc = model(x, t, train=True, finetune=finetune)
if not finetune:
loss.backward()
optimizer.update()
sum_loss += float(cuda.to_cpu(loss.data)) * y_batch.size
sum_accuracy += float(cuda.to_cpu(acc.data)) * y_batch.size
if not silent:
pbar.update(i + y_batch.size)
return sum_loss, sum_accuracy
def test_forward_gpu(self):
self.check_forward(cuda.to_gpu(self.x), cuda.to_gpu(self.rois))
def test_backward_gpu(self):
self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.rois),
cuda.to_gpu(self.gy))
def ch_trans_filter(w, inds):
from chainer import cuda, Variable
from groupy.gconv.chainer_gconv.transform_filter import TransformGFilter
w_gpu = cuda.to_gpu(w)
inds_gpu = cuda.to_gpu(inds)
wv = Variable(w_gpu)
rwv = TransformGFilter(inds_gpu)(wv)
return cuda.to_cpu(rwv.data)
def __call__(self, x, t):
self.y = self.predictor(x, self.train_depth)
if hasattr(self, 'class_weight'):
if isinstance(x.data, cuda.cupy.ndarray) \
and not isinstance(self.class_weight, cuda.cupy.ndarray):
self.class_weight = cuda.to_gpu(
self.class_weight, device=x.data.device)
self.loss = softmax_cross_entropy(
self.y, t, class_weight=self.class_weight)
else:
self.loss = F.softmax_cross_entropy(self.y, t)
reporter.report({'loss': self.loss}, self)
return self.loss
def test_forward_gpu(self):
self.pooled_y.to_gpu()
self.check_forward(self.pooled_y)
def test_backward_gpu(self):
self.check_backward(cuda.to_gpu(
self.pooled_y.data), cuda.to_gpu(self.gy))
test_softmax_cross_entropy.py 文件源码
项目:chainer-segnet
作者: pfnet-research
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def test_forward_gpu(self):
self.check_forward(
cuda.to_gpu(self.x), cuda.to_gpu(self.t),
None if not self.weight_apply else cuda.to_gpu(self.class_weight))
test_softmax_cross_entropy.py 文件源码
项目:chainer-segnet
作者: pfnet-research
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_forward_gpu_no_cudnn(self):
self.check_forward(
cuda.to_gpu(self.x), cuda.to_gpu(self.t),
None if not self.weight_apply else cuda.to_gpu(self.class_weight),
False)
test_softmax_cross_entropy.py 文件源码
项目:chainer-segnet
作者: pfnet-research
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def test_backward_gpu_no_cudnn(self):
self.check_backward(
cuda.to_gpu(self.x), cuda.to_gpu(self.t),
None if not self.weight_apply else cuda.to_gpu(self.class_weight),
False)
test_softmax_cross_entropy.py 文件源码
项目:chainer-segnet
作者: pfnet-research
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def test_value_check_gpu_cudnn(self):
self.check_value_check(cuda.to_gpu(self.x), cuda.to_gpu(self.t), True)
def __init__(self, model, target, device_id=-1,
learning_rate=0.00025, momentum=.9,
minibatch_size=32, update_interval=10000):
assert isinstance(model, ChainerModel), \
'model should inherit from ChainerModel'
super(QNeuralNetwork, self).__init__(model.input_shape,
model.output_shape)
self._gpu_device = None
self._loss_val = 0
# Target model update method
self._steps = 0
self._target_update_interval = update_interval
# Setup model and target network
self._minibatch_size = minibatch_size
self._model = model
self._target = target
self._target.copyparams(self._model)
# If GPU move to GPU memory
if device_id >= 0:
with cuda.get_device(device_id) as device:
self._gpu_device = device
self._model.to_gpu(device)
self._target.to_gpu(device)
# Setup optimizer
self._optimizer = Adam(learning_rate, momentum, 0.999)
self._optimizer.setup(self._model)
def evaluate(self, environment, model=QModel.ACTION_VALUE_NETWORK):
if check_rank(environment.shape, get_rank(self._input_shape)):
environment = environment.reshape((1,) + environment.shape)
# Move data if necessary
if self._gpu_device is not None:
environment = cuda.to_gpu(environment, self._gpu_device)
if model == QModel.ACTION_VALUE_NETWORK:
output = self._model(environment)
else:
output = self._target(environment)
return cuda.to_cpu(output.data)
def train(self, x, y, actions=None):
actions = actions.astype(np.int32)
batch_size = len(actions)
if self._gpu_device:
x = cuda.to_gpu(x, self._gpu_device)
y = cuda.to_gpu(y, self._gpu_device)
actions = cuda.to_gpu(actions, self._gpu_device)
q = self._model(x)
q_subset = F.reshape(F.select_item(q, actions), (batch_size, 1))
y = y.reshape(batch_size, 1)
loss = F.sum(F.huber_loss(q_subset, y, 1.0))
self._model.cleargrads()
loss.backward()
self._optimizer.update()
self._loss_val = np.asscalar(cuda.to_cpu(loss.data))
# Keeps track of the number of train() calls
self._steps += 1
if self._steps % self._target_update_interval == 0:
# copy weights
self._target.copyparams(self._model)
def __init__(self, model, target, device_id=-1,
learning_rate=0.00025, momentum=.9,
minibatch_size=32, update_interval=10000):
assert isinstance(model, ChainerModel), \
'model should inherit from ChainerModel'
super(QNeuralNetwork, self).__init__(model.input_shape,
model.output_shape)
self._gpu_device = None
self._loss_val = 0
# Target model update method
self._steps = 0
self._target_update_interval = update_interval
# Setup model and target network
self._minibatch_size = minibatch_size
self._model = model
self._target = target
self._target.copyparams(self._model)
# If GPU move to GPU memory
if device_id >= 0:
with cuda.get_device(device_id) as device:
self._gpu_device = device
self._model.to_gpu(device)
self._target.to_gpu(device)
# Setup optimizer
self._optimizer = Adam(learning_rate, momentum, 0.999)
self._optimizer.setup(self._model)
def evaluate(self, environment, model=QModel.ACTION_VALUE_NETWORK):
if check_rank(environment.shape, get_rank(self._input_shape)):
environment = environment.reshape((1,) + environment.shape)
# Move data if necessary
if self._gpu_device is not None:
environment = cuda.to_gpu(environment, self._gpu_device)
if model == QModel.ACTION_VALUE_NETWORK:
output = self._model(environment)
else:
output = self._target(environment)
return cuda.to_cpu(output.data)
def train(self, x, y, actions=None):
actions = actions.astype(np.int32)
batch_size = len(actions)
if self._gpu_device:
x = cuda.to_gpu(x, self._gpu_device)
y = cuda.to_gpu(y, self._gpu_device)
actions = cuda.to_gpu(actions, self._gpu_device)
q = self._model(x)
q_subset = F.reshape(F.select_item(q, actions), (batch_size, 1))
y = y.reshape(batch_size, 1)
loss = F.sum(F.huber_loss(q_subset, y, 1.0))
self._model.cleargrads()
loss.backward()
self._optimizer.update()
self._loss_val = np.asscalar(cuda.to_cpu(loss.data))
# Keeps track of the number of train() calls
self._steps += 1
if self._steps % self._target_update_interval == 0:
# copy weights
self._target.copyparams(self._model)
def converter(xs, device):
if device is None:
return xs
elif device < 0:
return map(lambda x: map(lambda m: cuda.to_cpu(m), x), xs)
else:
return map(lambda x: map(
lambda m: cuda.to_gpu(m, device, cuda.Stream.null), x), xs)