本文整理汇总了Python中chainer.cuda.elementwise方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.elementwise方法的具体用法?Python cuda.elementwise怎么用?Python cuda.elementwise使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.cuda
的用法示例。
在下文中一共展示了cuda.elementwise方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def forward_gpu(self, inputs):
n = len(inputs)
ptrs = cuda.cupy.asarray([x.data.ptr for x in inputs],
dtype=cuda.cupy.int64)
ws = cuda.cupy.asarray(self.weights, dtype=cuda.cupy.float32)
y = cuda.elementwise(
'T x0, int64 xs, raw W ws, int32 n_xs',
'T y',
'float** xs_ = (float**) xs;'
'y = 0;'
'for (size_t j = 0; j < n_xs; ++j) {'
' y += xs_[j][i] * ws[j];'
'}',
'weighted_sum_arrays'.format(n))(inputs[0],
ptrs.data.ptr,
ws,
len(ptrs))
return y,
示例2: update_core_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def update_core_gpu(self, param):
grad = param.grad
if grad is None:
return
hp = self.hyperparam
eps = grad.dtype.type(hp.eps)
if hp.eps != 0 and eps == 0:
raise ValueError(
'eps of Adam optimizer is too small for {} ({})'.format(
grad.dtype.name, hp.eps))
cuda.elementwise(
'T grad, T lr, T one_minus_beta1, T one_minus_beta2, T eps, \
T eta, T weight_decay_rate',
'T param, T m, T v',
'''m += one_minus_beta1 * (grad - m);
v += one_minus_beta2 * (grad * grad - v);
param -= eta * lr * (m / (sqrt(v) + eps) +
weight_decay_rate * param);''',
'adam')(grad, self.lr, 1 - hp.beta1,
1 - hp.beta2, hp.eps,
hp.eta, hp.weight_decay_rate,
param.data, self.state['m'], self.state['v'])
示例3: forward
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def forward(self, x):
if hasattr(self, 'mask'):
y = x[0] * self.mask
else:
scale = x[0].dtype.type(1. / (1 - self.dropout_ratio))
xp = cuda.get_array_module(*x)
mask = xp.ones(x[0].shape, dtype=numpy.float32)
rand = xp.random.rand(*x[0].shape[:2])
mask[rand <= self.dropout_ratio] = 0
if xp == numpy:
self.mask = mask * scale
y = x[0] * self.mask
else:
self.mask, y = cuda.elementwise(
'T x, T mask1, T scale', 'T mask, T y',
'''
mask = mask1 * scale;
y = x * mask;
''',
'spatial_dropout_fwd',
)(x[0], mask, scale)
return y,
示例4: backward
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def backward(self, inputs, gy):
xp = cuda.get_array_module(inputs)
x, W, b = inputs
gy, = gy
if xp is numpy:
gx = W * gy
gW = x * gy
else:
gx, gW = cuda.elementwise(
'T x, T W, T b, T gy', 'T gx, T gW',
'gx = W * gy; gW = x * gy;', 'affine_bwd'
)(x, W, b, gy)
gb = gy
gW = xp.sum(gW, axis=(0, 2, 3), keepdims=True)
gb = xp.sum(gb, axis=(0, 2, 3), keepdims=True)
return gx, gW, gb
示例5: update_core_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def update_core_gpu(self, param):
grad = param.grad
if grad is None:
return
cuda.elementwise(
'T grad, T lr, T alpha, T eps',
'T param, T ms',
'''ms = alpha * ms + (1 - alpha) * grad * grad;
param -= lr * grad / sqrt(ms + eps);''',
'rmsprop')(grad, self.hyperparam.lr, self.hyperparam.alpha,
self.hyperparam.eps, param.array, self.state['ms'])
示例6: __call__
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def __call__(self, rule, param):
if param.name == 'b':
return
p, g = param.array, param.grad
if p is None or g is None:
return
with cuda.get_device_from_array(p) as dev:
if int(dev) == -1:
g += self.rate * p
else:
kernel = cuda.elementwise(
'T p, T decay', 'T g', 'g += decay * p', 'weight_decay')
kernel(p, self.rate, g)
示例7: _noise_function
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def _noise_function(self, r):
if self._kernel is None:
self._kernel = cuda.elementwise(
'', 'T r',
'''r = copysignf(sqrtf(fabsf(r)), r);''',
'noise_func')
self._kernel(r)
示例8: forward_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def forward_gpu(self, inputs):
n = len(inputs)
ptrs = cuda.cupy.asarray([x.data.ptr for x in inputs],
dtype=cuda.cupy.int64)
y = cuda.elementwise(
'T x0, int64 xs, int32 n_xs',
'T y',
'float** xs_ = (float**) xs;'
'y = 0;'
'for (size_t j = 0; j < n_xs; ++j) {'
' y += xs_[j][i];'
'}',
'sum_arrays'.format(n))(inputs[0], ptrs.data.ptr, len(ptrs))
return y,
示例9: backward_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def backward_gpu(self, inputs, grad_outputs):
x, y, z = inputs
gw, = grad_outputs
gx, gy = cuda.elementwise(
'T x, T y, T gw',
'T gx, T gy',
'''
gx = y * gw;
gy = x * gw;
''',
'muladd_bwd')(x, y, gw)
gz = gw
return gx, gy, gz
示例10: update_one_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def update_one_gpu(self, param, state):
cuda.elementwise(
'T grad, T lr, T alpha, T eps',
'T param, T ms',
'''ms = alpha * ms + (1 - alpha) * grad * grad;
param -= lr * grad / sqrt(ms + eps);''',
'rmsprop')(param.grad, self.lr, self.alpha, self.eps,
param.data, state['ms'])
示例11: __call__
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def __call__(self, opt):
if cuda.available:
kernel = cuda.elementwise(
'T p, T decay', 'T g', 'g += decay * p', 'weight_decay')
rate = self.rate
for name, param in opt.target.namedparams():
if name == 'b' or name.endswith('/b'):
continue
p, g = param.data, param.grad
with cuda.get_device(p) as dev:
if int(dev) == -1:
g += rate * p
else:
kernel(p, rate, g)
示例12: forward
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def forward(self, inputs):
c_prev, x = inputs
a, i, f, o = _extract_gates(x)
batch = len(x)
if isinstance(x, numpy.ndarray):
self.a = numpy.tanh(a)
self.i = _sigmoid(i)
self.f = _sigmoid(f)
self.o = _sigmoid(o)
c_next = numpy.empty_like(c_prev)
c_next[:batch] = self.a * self.i + self.f * c_prev[:batch]
ungated_h = numpy.tanh(c_next[:batch])
o_gate = self.o
else:
c_next = cuda.cupy.empty_like(c_prev)
ungated_h = cuda.cupy.empty_like(c_next[:batch])
o_gate = cuda.cupy.empty_like(c_next[:batch])
cuda.elementwise(
'T c_prev, T a, T i_, T f, T o', 'T c, T ungated_h, T o_gate',
'''
COMMON_ROUTINE;
c = aa * ai + af * c_prev;
ungated_h = tanh(c);
o_gate = ao;
''',
'lstm_fwd', preamble=_preamble)(
c_prev[:batch], a, i, f, o, c_next[:batch], ungated_h, o_gate)
c_next[batch:] = c_prev[batch:]
self.c = c_next[:batch]
return c_next, ungated_h, o_gate
示例13: update_one_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def update_one_gpu(self, param, state):
cuda.elementwise(
'T grad, T lr, T one_minus_beta1, T one_minus_beta2, T eps',
'T param, T m, T v',
'''m += one_minus_beta1 * (grad - m);
v += one_minus_beta2 * (grad * grad - v);
param -= lr * m / (sqrt(v) + eps);''',
'adam')(param.grad, self.lr, 1 - self.beta1, 1 - self.beta2,
self.eps, param.data, state['m'], state['v'])
self.num_step += 1
示例14: forward_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def forward_gpu(self, x):
self.sigma_a_plus_b, y = cuda.elementwise(
'T x1, T x2, T x3', 'T sigma_a_plus_b, T y',
'''
sigma_a_plus_b = tanh((x1 + x2) * 0.5) * 0.5 + 0.5;// 1 / (1 + exp(-(x1 + x2)));
y = x3 * sigma_a_plus_b;
''',
'sigmoid_a_plus_b_by_h_fwd')(x[0], x[1], x[2])
return y,
示例15: backward_gpu
# 需要导入模块: from chainer import cuda [as 别名]
# 或者: from chainer.cuda import elementwise [as 别名]
def backward_gpu(self, x, gy):
gx1, gx2, gx3 = cuda.elementwise(
'T sigma_a_plus_b, T h, T gy', 'T gx1, T gx2, T gx3',
'''
gx3 = gy * sigma_a_plus_b;
gx1 = h * gx3 * (1-sigma_a_plus_b);
gx2 = gx1;
''',
'sigmoid_bwd')(self.sigma_a_plus_b, x[2], gy[0])
return gx1, gx2, gx3,