def test_parallel_one_arg(self): inputs = [gtn.scalar_graph(k) for k in [1.0, 2.0, 3.0]] outputs = gtn.negate(inputs) expected = [] for g in inputs: expected.append(gtn.negate(g)) self.assertEqual(len(outputs), len(inputs)) for i in range(0, len(expected)): self.assertTrue(gtn.equal(outputs[i], expected[i]))
def test_scalar_ops(self): g1 = gtn.Graph() g1.add_node(True) g1.add_node(False, True) g1.add_arc(0, 1, 0, 0, 1.0) # Test negate: res = gtn.negate(g1) self.assertEqual(res.item(), -1.0) gtn.backward(res) self.assertEqual(g1.grad().item(), -1.0) g1.zero_grad() g2 = gtn.Graph() g2.add_node(True) g2.add_node(False, True) g2.add_arc(0, 1, 0, 0, 3.0) # Test add: res = gtn.add(g1, g2) self.assertEqual(res.item(), 4.0) gtn.backward(res) self.assertEqual(g1.grad().item(), 1.0) self.assertEqual(g2.grad().item(), 1.0) g1.zero_grad() g2.zero_grad() # Test subtract: res = gtn.subtract(g1, g2) self.assertEqual(res.item(), -2.0) gtn.backward(res) self.assertEqual(g1.grad().item(), 1.0) self.assertEqual(g2.grad().item(), -1.0)
def process(b): # create emission graph g_emissions = gtn.linear_graph(T, C, log_probs.requires_grad) cpu_data = log_probs[b].cpu().contiguous() g_emissions.set_weights(cpu_data.data_ptr()) # create criterion graph g_criterion = CTCLossFunction.create_ctc_graph( targets[b], blank_idx) # compose the graphs g_loss = gtn.negate( gtn.forward_score(gtn.intersect(g_emissions, g_criterion))) scale = 1.0 if reduction == "mean": L = len(targets[b]) scale = 1.0 / L if L > 0 else scale elif reduction != "none": raise ValueError("invalid value for reduction '" + str(reduction) + "'") # Save for backward: losses[b] = g_loss scales[b] = scale emissions_graphs[b] = g_emissions
def process(b): # Create emissions graph: emissions = gtn.linear_graph(T, C, inputs.requires_grad) cpu_data = inputs[b].cpu().contiguous() emissions.set_weights(cpu_data.data_ptr()) target = make_chain_graph(targets[b]) target.arc_sort(True) # Create token to grapheme decomposition graph tokens_target = gtn.remove(gtn.project_output(gtn.compose(target, lexicon))) tokens_target.arc_sort() # Create alignment graph: alignments = gtn.project_input( gtn.remove(gtn.compose(tokens, tokens_target)) ) alignments.arc_sort() # Add transition scores: if transitions is not None: alignments = gtn.intersect(transitions, alignments) alignments.arc_sort() loss = gtn.forward_score(gtn.intersect(emissions, alignments)) # Normalize if needed: if transitions is not None: norm = gtn.forward_score(gtn.intersect(emissions, transitions)) loss = gtn.subtract(loss, norm) losses[b] = gtn.negate(loss) # Save for backward: if emissions.calc_grad: emissions_graphs[b] = emissions
def test_scalar_ops(self): g1 = gtn.scalar_graph(3.0) result = gtn.negate(g1) self.assertEqual(result.item(), -3.0) g2 = gtn.scalar_graph(4.0) result = gtn.add(g1, g2) self.assertEqual(result.item(), 7.0) result = gtn.subtract(g2, g1) self.assertEqual(result.item(), 1.0)
def test_parallel_func(self): B = 3 inputs1 = [gtn.scalar_graph(k) for k in [1.0, 2.0, 3.0]] inputs2 = [gtn.scalar_graph(k) for k in [1.0, 2.0, 3.0]] out = [None] * B def process(b): out[b] = gtn.add(gtn.add(inputs1[b], inputs1[b]), gtn.negate(inputs2[b])) gtn.parallel_for(process, range(B)) expected = [] for b in range(B): expected.append( gtn.add(gtn.add(inputs1[b], inputs1[b]), gtn.negate(inputs2[b]))) self.assertEqual(len(out), len(expected)) for i in range(len(expected)): self.assertTrue(gtn.equal(out[i], expected[i]))
def test_scalar_ops_grad(self): g1 = gtn.scalar_graph(3.0) result = gtn.negate(g1) gtn.backward(result) self.assertEqual(g1.grad().item(), -1.0) g1.zero_grad() g2 = gtn.scalar_graph(4.0) result = gtn.add(g1, g2) gtn.backward(result) self.assertEqual(g1.grad().item(), 1.0) self.assertEqual(g2.grad().item(), 1.0) g1.zero_grad() g2.zero_grad() result = gtn.subtract(g1, g2) gtn.backward(result) self.assertEqual(g1.grad().item(), 1.0) self.assertEqual(g2.grad().item(), -1.0) g1.zero_grad() g2.zero_grad() result = gtn.add(gtn.add(g1, g2), g1) gtn.backward(result) self.assertEqual(g1.grad().item(), 2.0) self.assertEqual(g2.grad().item(), 1.0) g1.zero_grad() g2nograd = gtn.scalar_graph(4.0, False) result = gtn.add(g1, g2nograd) gtn.backward(result) self.assertEqual(g1.grad().item(), 1.0) self.assertRaises(RuntimeError, g2nograd.grad)
def process(b): out[b] = gtn.add(gtn.add(inputs1[b], inputs1[b]), gtn.negate(inputs2[b]))