Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_forward_gpu_test(self):
self.rnn.to_gpu()
with chainer.using_config('use_cudnn', 'always'), \
chainer.using_config('train', False):
self.check_forward(
cuda.to_gpu(self.h),
[cuda.to_gpu(x) for x in self.xs])
def check_backward(self, x_data, y_grad, use_cudnn='always'):
def f(x):
return functions.clipped_relu(x, self.z)
with chainer.using_config('use_cudnn', use_cudnn):
gradient_check.check_backward(
f, x_data, y_grad, dtype=numpy.float64,
**self.check_backward_options)
def check_forward(self, x_data):
with chainer.using_config('train', not self.test):
x = chainer.Variable(x_data)
y = self.link(x)
self.assertEqual(y.data.dtype, self.dtype)
sigma_batch = numpy.sqrt(self.var)
running_sigma = numpy.sqrt(self.running_var)
r = numpy.clip(sigma_batch / running_sigma, 1.0 / self.rmax, self.rmax)
d = numpy.clip((self.mean - self.running_mean) / running_sigma,
-self.dmax, self.dmax)
y_expect = _batch_renormalization(
self.expander, self.gamma, self.beta, self.x, self.mean,
self.var, self.link.eps, self.test,
r[self.expander], d[self.expander])
testing.assert_allclose(
y_expect, y.data, **self.check_forward_optionss)
def test_backward_cpu(self):
with chainer.using_config('use_cudnn', 'never'):
self.check_backward(self.x, self.t)
def check_tuple_data_model(gpu, param):
# This test only uses pairs (0, 1), (2, 3), ... (2m, 2m+1)
communicator, rank_next, rank_prev = create_communicator(gpu)
n, d = 100, 10
X = np.random.randn(n, d).astype(param.dtype)
Y = (np.random.rand(n) * 2).astype(np.int32)
with chainer.using_config('dtype', param.dtype):
if communicator.rank % 2 == 0:
if communicator.rank == communicator.size - 1:
# in case 2m is the right end with odd number of nodes
return
model = L.Classifier(
TupleDataParent(communicator, d, rank_next))
elif communicator.rank % 2 == 1:
model = TupleDataChild(communicator, d, rank_prev)
assert model is not None
if gpu:
model.to_gpu()
X = chainer.cuda.to_gpu(X)
Y = chainer.cuda.to_gpu(Y)
for i in range(n):
def test_invalid_config(self):
with chainer.using_config('use_cudnn', True):
self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
with chainer.using_config('use_cudnn', False):
self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
with chainer.using_config('use_cudnn', 'on'):
self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
def setUp(self):
self.x = cuda.cupy.random.uniform(-1, 1, (4, 3)).astype(numpy.float32)
self.t = cuda.cupy.random.randint(0, 3, (4, 3)).astype(numpy.int32)
with chainer.using_config('use_cudnn', self.use_cudnn):
self.expect = chainer.should_use_cudnn('==always')
def make_image(gen, stage, seed=0, rows=10, cols=10):
import numpy as np
from chainer import Variable
# np.random.seed(seed)
n_images = rows * cols
xp = gen.xp
z = Variable(xp.asarray(gen.make_hidden(n_images)))
with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
x = gen(z, stage=stage)
x = chainer.cuda.to_cpu(x.data)
np.random.seed()
x = np.asarray(np.clip(x * 127.5 + 127.5, 0.0, 255.0), dtype=np.uint8)
_, _, h, w = x.shape
x = x.reshape((rows, cols, 3, h, w))
x = x.transpose(0, 3, 1, 4, 2)
x = x.reshape((rows * h, cols * w, 3))
preview_path = "testsome.png"
# if not os.path.exists(preview_dir):
# os.makedirs(preview_dir)
Image.fromarray(x).save(preview_path)
model.save(model_filename)
loader.save_stats(stats_directory)
report("Epoch {}".format(epoch))
report(loader.get_statistics())
# ノイズ無しデータでバリデーション
batch_iter_dev = loader.get_development_batch_iterator(batchsizes_dev, augmentation=AugmentationOption(), gpu=using_gpu)
total_iterations_dev = batch_iter_dev.get_total_iterations()
buckets_errors = [[] for i in range(loader.get_num_buckets())]
for batch_index, (x_batch, x_length_batch, t_batch, t_length_batch, bigram_batch, bucket_id) in enumerate(batch_iter_dev):
try:
with chainer.using_config("train", False):
with chainer.no_backprop_mode():
# print(xp.mean(x_batch, axis=3), xp.var(x_batch, axis=3))
printr("Computing CER ... {}/{}".format(batch_index + 1, total_iterations_dev))
y_batch = model(x_batch, split_into_variables=False)
y_batch = xp.argmax(y_batch.data, axis=2)
error = compute_minibatch_error(y_batch, t_batch, ID_BLANK, vocab_token_ids, vocab_id_tokens)
buckets_errors[bucket_id].append(error)
except Exception as e:
printr("")
printc("{} (bucket {})".format(str(e), bucket_id + 1), color="red")
avg_errors_dev = []
for errors in buckets_errors:
avg_errors_dev.append(sum(errors) / len(errors) * 100)
def update_Z_speech(self, var_propose_distribution=1e-4):
"""
Parameters:
var_propose_distribution: float
the variance of the propose distribution
Results:
self.Z_speech_DT: self.xp.array [ n_latent x T ]
the latent variable of each speech
"""
self.WHG_noise_FTM = (self.lambda_NFT[1:][..., None] * self.G_NFM[1:, :, None]).sum(axis=0)
self.UVG_FTM = (self.U_F[:, None] * self.V_T[None])[:, :, None] * self.G_NFM[0, :, None]
if "backprop" in self.mode_update_Z: # acceptance rate is calculated from likelihood
for it in range(self.n_Z_iteration):
with chainer.using_config('train', False):
self.z_optimizer_speech.update(self.loss_func_Z, self.z_link_speech.z, self.speech_VAE, 0)
self.Z_speech_DT = self.z_link_speech.z.data.T
self.power_speech_FT = self.speech_VAE.decode_cupy(self.Z_speech_DT)
if "sampling" in self.mode_update_Z:
log_var = self.xp.log(self.xp.ones_like(self.Z_speech_DT).astype(self.xp.float32) * var_propose_distribution)
Z_speech_old_DT = self.Z_speech_DT
power_old_FTM = self.speech_VAE.decode_cupy(Z_speech_old_DT)[:, :, None]
for it in range(self.n_Z_iteration):
Z_speech_new_DT = chf.gaussian(Z_speech_old_DT, log_var).data
lambda_old_FTM = power_old_FTM * self.UVG_FTM + self.WHG_noise_FTM
power_new_FTM = self.speech_VAE.decode_cupy(Z_speech_new_DT)[:, :, None]
lambda_new_FTM = power_new_FTM * self.UVG_FTM + self.WHG_noise_FTM
acceptance_rate = self.xp.exp((self.Qx_power_FTM * (1 / lambda_old_FTM - 1 / lambda_new_FTM)).sum(axis=2).sum(axis=0) + self.xp.log( ( lambda_old_FTM / lambda_new_FTM ).prod(axis=2).prod(axis=0) ) )