Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
----------
x : chainer.Variable or numpy.ndarray or cupy.ndarray
Input variable.
groups : int
Number of groups.
Returns
-------
chainer.Variable or numpy.ndarray or cupy.ndarray
Resulted variable.
"""
batch, channels, height, width = x.shape
channels_per_group = channels // groups
x = F.reshape(x, shape=(batch, groups, channels_per_group, height, width))
x = F.swapaxes(x, axis1=1, axis2=2)
x = F.reshape(x, shape=(batch, channels, height, width))
return x
c_new, z_new = self.lstm0(c_list[0], z_list[0], ey)
c_list_new.append(c_new)
z_list_new.append(z_new)
if self.dlayers > 1:
c_new, z_new = self.lstm1(c_list[1], z_list[1], z_list_new[-1])
c_list_new.append(c_new)
z_list_new.append(z_new)
# for l in six.moves.range(1, self.dlayers):
# c_new, z_new = self['lstm%d' % l](c_list[l], z_list[l], z_list_new[-1])
# c_list_new.append(c_new)
# z_list_new.append(z_new)
c_list = c_list_new
z_list = z_list_new
z_all.append(z_list[-1])
z_all = F.reshape(F.stack(z_all, axis=1),
(batch * olength, self.dunits))
# compute loss
y_all = self.output(z_all)
# EDIT(hamaji): `np.flatten` implemented by ourselves.
# self.loss = F.softmax_cross_entropy(y_all, F.flatten(pad_ys_out))
self.loss = F.softmax_cross_entropy(y_all, _flatten(pad_ys_out))
# -1: eos, which is removed in the loss computation
# EDIT(hamaji): `np.mean` implemented by a naive loop.
# self.loss *= (np.mean([len(x) for x in ys_in]) - 1)
self.loss *= _mean(ys_in) - 1
# EDIT(hamaji): No need to compute accuracy.
# acc = F.accuracy(y_all, F.flatten(pad_ys_out), ignore_label=-1)
# logging.info('att loss:' + str(self.loss.data))
# EDIT(hamaji): Skip verbose logging.
# # show predicted character sequence for debug
if self.recurrent:
qout, _ = self.model.n_step_forward(
batch_state,
exp_batch['recurrent_state'],
output_mode='concat',
)
else:
qout = self.model(batch_state)
batch_actions = exp_batch['action']
batch_q = F.reshape(qout.evaluate_actions(
batch_actions), (batch_size, 1))
with chainer.no_backprop_mode():
batch_q_target = F.reshape(
self._compute_target_values(exp_batch),
(batch_size, 1))
return batch_q, batch_q_target
def __init__(self, n_actions, max_episode_steps):
super().__init__()
with self.init_scope():
self.embed = L.EmbedID(max_episode_steps + 1, 3136)
self.image2hidden = chainerrl.links.Sequence(
L.Convolution2D(None, 32, 8, stride=4),
F.relu,
L.Convolution2D(None, 64, 4, stride=2),
F.relu,
L.Convolution2D(None, 64, 3, stride=1),
functools.partial(F.reshape, shape=(-1, 3136)),
)
self.hidden2out = chainerrl.links.Sequence(
L.Linear(None, 512),
F.relu,
L.Linear(None, n_actions),
DiscreteActionValue,
)
def predict(self, roi_features): # B, T, F, C, H, W
with chainer.cuda.get_device_from_array(roi_features.data) as device:
fc_output = self.forward(roi_features) # B, T, F, 2048
mini_batch, seq_len, box_num, _ = fc_output.shape
fc_output = F.reshape(fc_output, shape=(-1, self.box_dim))
fc_output = self.score_fc(fc_output) # B * T * F, class_num
pred = fc_output.reshape(mini_batch, seq_len, box_num, -1) # B, T, F, class_num
pred = chainer.cuda.to_cpu(pred.data) # B, T, F, class_num
pred = (pred > 0).astype(np.int32)
return pred # B, T, F, class_num
def __call__(self, x):
minibatch_size = x.shape[0]
activation = F.reshape(self.t(x), (-1, self.n_kernels, self.kernel_dim))
activation_ex = F.expand_dims(activation, 3)
activation_ex_t = F.expand_dims(F.transpose(activation, (1, 2, 0)), 0)
activation_ex, activation_ex_t = F.broadcast(activation_ex, activation_ex_t)
diff = activation_ex - activation_ex_t
xp = chainer.cuda.get_array_module(x.data)
eps = F.expand_dims(xp.eye(minibatch_size, dtype=xp.float32), 1)
eps = F.broadcast_to(eps, (minibatch_size, self.n_kernels, minibatch_size))
sum_diff = F.sum(abs(diff), axis=2)
sum_diff = F.broadcast_to(sum_diff, eps.shape)
abs_diff = sum_diff + eps
minibatch_features = F.sum(F.exp(-abs_diff), 2)
return F.concat((x, minibatch_features), axis=1)
def evaluate(self, eval_iter):
stats = defaultdict(lambda: 0)
# progress_bar = ProgressBar(eval_iter.size, unit_iteration=True)
for i in range(eval_iter.size):
batch = eval_iter.next_batch(self.model.xp)
length, batch_size, _ = batch.vecs.shape
ys = self.model(batch.vecs, train=False)
ts = F.reshape(batch.results, (length * batch_size, -1))
mask = F.reshape(batch.mask, (length * batch_size, ))
stats['loss'] = self.loss(ys, ts, mask).data.tolist()
batch_stats = self.metric(ys.data, ts.data, mask.data)
for k, v in batch_stats.items():
stats[k] += v
# progress_bar(*eval_iter.epoch_detail)
eval_iter.finalize(reset=True)
# progress_bar.finalize()
for k, v in stats.items():
stats[k] = v / eval_iter.size
return stats
def compute_loss(self, seq_list, encoded_input, mask_input, reduce="mean"):
logits = self.compute_logits(seq_list, encoded_input, mask_input)
padded_target_with_eos = pad_data(seq_list, pad_value=-1, add_eos=self.eos_idx)
padded_target_with_eos = self.move_np_array_to_correct_device(padded_target_with_eos)
loss = F.softmax_cross_entropy(F.reshape(logits, (-1, self.V+1)), padded_target_with_eos.reshape(-1,), reduce=reduce)
return loss
def forward(self, x):
h = F.max_pooling_2d(F.relu(self.norm1(self.conv1(x))), 3, stride=2, pad=1) # 64 * 57 * 57
hint_s57c64_0 = F.reshape(F.average_pooling_2d(h,57),(64,))
h = F.max_pooling_2d(F.relu(self.norm2(self.conv2(h))), 3, stride=2, pad=1) # 192 * 29 * 29
hint_s29c192_0 = F.reshape(F.average_pooling_2d(h, 29), (192,))
h = self.inc3a(h) # 256 * 29 * 29
hint_s29c256_0 = F.reshape(F.average_pooling_2d(h, 29), (256,))
h = self.inc3b(h) # 320 * 29 * 29
hint_s29c320_0 = F.reshape(F.average_pooling_2d(h, 29), (320,))
h = self.inc3c(h) # 576 * 15 * 15
hint_s15c576_0 = F.reshape(F.average_pooling_2d(h, 15), (576,))
h = self.inc4a(h) # 576 * 15 * 15
hint_s15c576_1 = F.reshape(F.average_pooling_2d(h, 15), (576,))
h = self.inc4b(h) # 576 * 15 * 15
hint_s15c576_2 = F.reshape(F.average_pooling_2d(h, 15), (576,))
h = self.inc4c(h) # 576 * 15 * 15
hint_s15c576_3 = F.reshape(F.average_pooling_2d(h, 15), (576,))
def __call__(self, x):
xp = chainer.cuda.get_array_module(x.data)
batchsize = x.shape[0]
M = F.reshape(self.T(x), (-1, self.num_kernels, self.ndim_kernel))
M = F.expand_dims(M, 3)
M_T = F.transpose(M, (3, 1, 2, 0))
M, M_T = F.broadcast(M, M_T)
norm = F.sum(abs(M - M_T), axis=2)
eraser = F.broadcast_to(xp.eye(batchsize, dtype=x.dtype).reshape((batchsize, 1, batchsize)), norm.shape)
c_b = F.exp(-(norm + 1e6 * eraser))
o_b = F.sum(c_b, axis=2)
return F.concat((x, o_b), axis=1)