def check_forward(self, t_data, xs_data, l_length, x_length):
x = tuple(chainer.Variable(x_data) for x_data in xs_data)
t = chainer.Variable(t_data)
args = (x, t, self.blank_symbol)
if self.use_length:
args += (chainer.Variable(x_length), chainer.Variable(l_length))
loss = functions.connectionist_temporal_classification(*args)
loss_value = float(loss.data)
# compute expected value by recursive computation.
xp = cuda.get_array_module(self.x)
xt = xp.swapaxes(self.x, 0, 1)
for b in range(xt.shape[0]):
for t in range(xt.shape[1]):
xt[b][t] = numpy.exp(xt[b][t]) / numpy.sum(numpy.exp(xt[b][t]))
loss_expect = 0
batch_size = xt.shape[0]
path_length = 2 * l_length + 1
for xtb, lb, xlb, plb in zip(xt, self.l, x_length, path_length):
loss_expect += -math.log(
self.alpha(xtb, lb, int(xlb - 1), int(plb - 1)) +
self.alpha(xtb, lb, int(xlb - 1), int(plb - 2)))
loss_expect /= batch_size
self.assertAlmostEqual(loss_expect, loss_value, places=5)
评论列表
文章目录