def test_reduction(self): rule_prob0 = torch.FloatTensor([[0.8, 0.2], [0.5, 0.5], [0.5, 0.5]]) rule_prob = rnn.pad_sequence([rule_prob0]) token_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5], [0.1, 0.2, 0.8], [0.5, 0.4, 0.1]]) token_prob = rnn.pad_sequence([token_prob0]) reference_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5, 0.0], [0.0, 0.5, 0.4, 0.1], [0.0, 0.0, 0.0, 1.0]]) reference_prob = rnn.pad_sequence([reference_prob0]) loss0 = EntropyLoss() loss1 = EntropyLoss(reduction="sum") loss2 = EntropyLoss(reduction="none") objective0 = loss0( rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ) objective1 = loss1( rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ) objective2 = loss2( rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ) assert (1, ) == objective2.shape assert np.allclose(objective0.item(), objective1.item())
def test_reduction(self): gt0 = torch.LongTensor([[0, -1, -1], [-1, 2, -1], [-1, -1, 3]]) gt = rnn.pad_sequence([gt0], padding_value=-1) rule_prob0 = torch.FloatTensor([[0.8, 0.2], [0.5, 0.5], [0.5, 0.5]]) rule_prob = rnn.pad_sequence([rule_prob0]) token_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5], [0.1, 0.2, 0.8], [0.5, 0.4, 0.1]]) token_prob = rnn.pad_sequence([token_prob0]) reference_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5, 0.0], [0.0, 0.5, 0.4, 0.1], [0.0, 0.0, 0.0, 1.0]]) reference_prob = rnn.pad_sequence([reference_prob0]) loss0 = Loss() loss1 = Loss(reduction="sum") loss2 = Loss(reduction="none") objective0 = loss0(rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ground_truth_actions=gt) objective1 = loss1(rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ground_truth_actions=gt) objective2 = loss2(rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ground_truth_actions=gt) assert (1, ) == objective2.shape assert np.allclose(objective0.item(), objective1.item())
def test_shape(self, decoder): query = torch.rand(2, 2) action0 = torch.LongTensor([[0, 0, 0], [1, 1, 1], [1, 1, 1]]) action1 = torch.LongTensor([[0, 0, 0]]) actions = rnn.pad_sequence([action0, action1]) # (2, 2, 3) prev_action0 = torch.rand(3, 3) prev_action1 = torch.rand(1, 3) prev_actions = rnn.pad_sequence([prev_action0, prev_action1]) history = torch.rand(10, 2, 5) h_0 = torch.rand(2, 5) c_0 = torch.rand(2, 5) output, history, h_n, c_n = decoder( input_feature=query, actions=actions, action_features=prev_actions, history=history, hidden_state=h_0, state=c_0, ) assert (3, 2, 5) == output.data.shape assert np.array_equal([[1, 1], [1, 0], [1, 0]], output.mask.numpy()) assert (13, 2, 5) == history.shape assert (2, 5) == h_n.shape assert (2, 5) == c_n.shape
def test_shape(self): decoder = Decoder(3, 2, 5, 7, 0.0) query0 = torch.rand(3, 2) query1 = torch.rand(1, 2) query = rnn.pad_sequence([query0, query1]) action0 = torch.LongTensor([[0, 0, 0], [1, 1, 1], [1, 1, 1]]) action1 = torch.LongTensor([[0, 0, 0]]) actions = rnn.pad_sequence([action0, action1]) # (2, 2, 3) prev_action0 = torch.rand(3, 3) prev_action1 = torch.rand(1, 3) prev_actions = rnn.pad_sequence([prev_action0, prev_action1]) history = torch.rand(10, 2, 5) h_0 = torch.rand(2, 5) c_0 = torch.rand(2, 5) output, contexts, history, h_n, c_n = decoder( nl_query_features=query, actions=actions, action_features=prev_actions, history=history, hidden_state=h_0, state=c_0, ) assert (3, 2, 5) == output.data.shape assert np.array_equal([[1, 1], [1, 0], [1, 0]], output.mask.numpy()) assert (3, 2, 2) == contexts.data.shape assert np.array_equal([[1, 1], [1, 0], [1, 0]], contexts.mask.numpy()) assert (13, 2, 5) == history.shape assert (2, 5) == h_n.shape assert (2, 5) == c_n.shape
def test_shape(self): e = ActionsEmbedding(1, 2, 3, 4, 5) out = e( pad_sequence([torch.zeros(13, 3, dtype=torch.long)]), pad_sequence([torch.zeros(13, 3, dtype=torch.long)]), ) assert (13, 1, 14) == out.data.shape
def test_attention_input(self, decoder_with_attention_input): input0 = torch.rand(3, 2) input1 = torch.rand(1, 2) input = rnn.pad_sequence([input0, input1]) action0 = torch.rand(3, 3) action1 = torch.rand(1, 3) action = rnn.pad_sequence([action0, action1]) h_0 = torch.rand(2, 5) c_0 = torch.rand(2, 5) output, h_n, c_n = decoder_with_attention_input(input_feature=input, action_features=action, hidden_state=h_0, state=c_0) assert (3, 2, 5) == output.data.shape assert np.array_equal([[1, 1], [1, 0], [1, 0]], output.mask.numpy()) assert (2, 5) == h_n.shape assert (2, 5) == c_n.shape output2, h_n2, c_n2 = decoder_with_attention_input( input_feature=rnn.pad_sequence([input1]), action_features=rnn.pad_sequence([action1]), hidden_state=h_0[1:, :], state=c_0[1:, :]) assert np.allclose(output.data[:1, 1, :].detach().numpy(), output2.data[:, 0, :].detach().numpy())
def test_shape(self): reader = Encoder(2, 3, 1, 0.0, 5) in0 = torch.rand(5, 3) in0 = pad_sequence([in0], 0) in1 = torch.rand(5, 2) in1 = pad_sequence([in1], 0) out = reader(word_nl_feature=in0, char_nl_feature=in1) assert (5, 1, 3) == out.data.shape assert (5, 1) == out.mask.shape
def test_shape(self): e = NlEmbedding(2, 3, 5, 7, 11) w0 = torch.zeros(13).long() w1 = torch.zeros(11).long() w = pad_sequence([w0, w1], padding_value=-1) c0 = torch.zeros(13, 5).long() c1 = torch.zeros(11, 5).long() c = pad_sequence([c0, c1], padding_value=-1) e_w, e_c = e(w, c) assert (13, 2, 11) == e_w.data.shape assert (13, 2, 7) == e_c.data.shape
def test_shape_eval(self): predictor = Predictor(2, 3, 5, 7, 11) f = torch.Tensor(11, 2) nl = torch.Tensor(13, 3) predictor.eval() rule, token, reference = predictor( reference_features=pad_sequence([nl]), action_features=pad_sequence([f])) assert (1, 5) == rule.shape assert (1, 7) == token.shape assert (1, 13) == reference.shape
def test_shape(self): block = DecoderBlock(1, 3, 5, 1, 0.0) query0 = torch.Tensor(7, 1) nl0 = torch.Tensor(11, 1) ast0 = torch.Tensor(7, 1) out, w0, w1 = block(pad_sequence([query0], 0), pad_sequence([nl0], 0), pad_sequence([ast0], 0)) assert (7, 1, 5) == out.data.shape assert (7, 1) == out.mask.shape assert (1, 7, 11) == w0.shape assert (1, 7, 7) == w1.shape
def test_mask(self): block = EncoderBlock(2, 3, 1, 0.0, 0) in00 = torch.rand(5, 3) in01 = torch.rand(7, 3) in1 = torch.rand(7, 2, 2) out0, weight0 = block(pad_sequence([in00, in01], 0), in1) out1, weight1 = block(pad_sequence([in00], 0), in1[:5, :1, :]) out0 = out0.data[:5, :1, :] weight0 = weight0[:1, :5, :5] out1 = out1.data assert np.allclose(out0.detach().numpy(), out1.detach().numpy()) assert np.allclose(weight0.detach().numpy(), weight1.detach().numpy())
def test_mask(self): reader = Encoder(2, 3, 1, 0.0, 5) in00 = torch.rand(5, 3) in01 = torch.rand(7, 3) in10 = torch.rand(5, 2) in11 = torch.rand(7, 2) out0 = reader(word_nl_feature=pad_sequence([in00, in01], 0), char_nl_feature=pad_sequence([in10, in11])) out1 = reader(word_nl_feature=pad_sequence([in00], 0), char_nl_feature=pad_sequence([in10])) out0 = out0.data[:5, :1, :] out1 = out1.data assert np.allclose(out0.detach().numpy(), out1.detach().numpy())
def test_prog(self): predictor = Predictor(2, 3, 5, 7, 11) f = torch.rand(11, 2) nl = torch.rand(13, 3) rule, token, reference = predictor( reference_features=pad_sequence([nl]), action_features=pad_sequence([f])) prob = torch.cat([rule.data, token.data, reference.data], dim=2) prob = prob.detach().numpy() assert np.all(prob >= 0.0) assert np.all(prob <= 1.0) total = np.sum(prob, axis=2) assert np.allclose(1.0, total)
def test_mask(self): torch.manual_seed(0) decoder = Decoder(1, 1, 5, 3, 3, 1, 0.0, 5, 5) in00 = torch.rand(5, 1) in01 = torch.rand(7, 1) depth = torch.randint(1, [7, 2]) in10 = torch.rand(5, 1) in11 = torch.rand(7, 1) adj = torch.randint(1, [2, 7, 7]).bool().float() query00 = torch.zeros(5, 1) query01 = torch.zeros(7, 1) nl0 = torch.rand(11, 1) out0 = decoder(nl_query_features=pad_sequence([nl0, nl0], 0), action_query_features=pad_sequence([query00, query01], 0), action_features=pad_sequence([in00, in01], 0), action_rule_features=pad_sequence([in10, in11], 0), depthes=depth, adjacency_matrix=adj) out1 = decoder(nl_query_features=pad_sequence([nl0], 0), action_query_features=pad_sequence([query00], 0), action_features=pad_sequence([in00], 0), action_rule_features=pad_sequence([in10], 0), depthes=depth[:5, :1], adjacency_matrix=adj[:1, :5, :5]) out0 = out0.data[:5, :1, :] out1 = out1.data assert np.allclose(out0.detach().numpy(), out1.detach().numpy())
def test_dependency(self): torch.manual_seed(0) block = ActionSequenceReaderBlock(2, 3, 1, 3, 0.0, 0) in0 = torch.rand(3, 3) depth = torch.randint(3, [3, 1]) in1 = torch.rand(3, 1, 2) adj = torch.randint(1, [1, 3, 3]).bool().long() out0, weight0 = block(pad_sequence([in0], 0), depth, in1, adj) out1, weight1 = block(pad_sequence([in0[:2, :]], 0), depth[:2, :], in1[:2, :, :], adj[:, :2, :2]) out0 = out0.data[:2, :, :] weight0 = weight0[:1, :2, :2] out1 = out1.data assert np.allclose(out0.detach().numpy(), out1.detach().numpy()) assert np.allclose(weight0.detach().numpy(), weight1.detach().numpy())
def test_mask(self): torch.manual_seed(0) block = ActionSequenceReaderBlock(2, 3, 1, 3, 0.0, 0) in00 = torch.rand(5, 3) in01 = torch.rand(7, 3) depth = torch.randint(5, [7, 2]) in1 = torch.rand(7, 2, 2) adj = torch.randint(1, [1, 7, 7]).bool().long() out0, weight0 = block(pad_sequence([in00, in01], 0), depth, in1, adj) out1, weight1 = block(pad_sequence([in00], 0), depth[:5, :1], in1[:5, :1, :], adj[:1, :5, :5]) out0 = out0.data[:5, :1, :] weight0 = weight0[:1, :5, :5] out1 = out1.data assert np.allclose(out0.detach().numpy(), out1.detach().numpy()) assert np.allclose(weight0.detach().numpy(), weight1.detach().numpy())
def test_pointer_net(self): value0 = torch.FloatTensor([[1]]) value1 = torch.FloatTensor([[1], [1], [1], [1]]) value = pad_sequence([value0, value1]) key0 = torch.FloatTensor([[0]]) key1 = torch.FloatTensor([[0]]) key = pad_sequence([key0, key1]).data layer = PointerNet(1, 1, 2) log_output = layer(key, value) # (1, 2, 4) output = torch.exp(log_output) output *= value.mask.permute(1, 0).view(1, 2, 4).float() assert (1, 2, 4) == output.shape assert np.allclose([[1, 0, 0, 0], [0.25, 0.25, 0.25, 0.25]], output.detach().numpy())
def test_mask(self): encoder = BidirectionalLSTM(3, 14) q0 = torch.rand(2, 3) q1 = torch.rand(3, 3) query = rnn.pad_sequence([q0, q1]) output = encoder(query) assert np.all(output.data[2, 0, :].detach().numpy() == 0)
def test_shape(self): encoder = BidirectionalLSTM(3, 14) q0 = torch.rand(1, 3) q1 = torch.rand(3, 3) query = rnn.pad_sequence([q0, q1]) output = encoder(query) assert (3, 2, 14) == output.data.shape
def test_empty_sequence(self): encoder = Encoder(torch.nn.Linear(2, 1)) ref, input = encoder( test_case_tensor=torch.rand(1, 4, 1), variables_tensor=pad_sequence([torch.rand(0, 0, 1)]), test_case_feature=torch.arange(1).reshape(1, 1, 1).expand(1, 4, 1) ) assert (1, 2) == input.shape assert (0, 1, 1) == ref.data.shape
def test_value(self): rule_prob0 = torch.FloatTensor([[0.5, 0.5]]) rule_prob1 = torch.FloatTensor([[1.0, 0.0]]) rule_prob = rnn.pad_sequence([rule_prob0, rule_prob1]) token_prob0 = torch.FloatTensor([[0.0, 0.0]]) token_prob1 = torch.FloatTensor([[0.0, 0.0]]) token_prob = rnn.pad_sequence([token_prob0, token_prob1]) reference_prob0 = torch.FloatTensor([[0.0, 0.0]]) reference_prob1 = torch.FloatTensor([[0.0, 0.0]]) reference_prob = rnn.pad_sequence([reference_prob0, reference_prob1]) loss = EntropyLoss(reduction="none") objective = loss( rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ) assert objective[0].item() > objective[1].item()
def test_shape(self): block = EncoderBlock(2, 3, 1, 0.0, 0) in0 = torch.Tensor(5, 3) in0 = pad_sequence([in0], 0) in1 = torch.Tensor(5, 1, 2) out, weight = block(in0, in1) assert (5, 1, 3) == out.data.shape assert (5, 1) == out.mask.shape assert (1, 5, 5) == weight.shape
def test_shape(self): rule_prob0 = torch.FloatTensor([[0.8, 0.2], [0.5, 0.5], [0.5, 0.5]]) rule_prob = rnn.pad_sequence([rule_prob0]) token_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5], [0.1, 0.2, 0.8], [0.5, 0.4, 0.1]]) token_prob = rnn.pad_sequence([token_prob0]) reference_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5, 0.0], [0.0, 0.5, 0.4, 0.1], [0.0, 0.0, 0.0, 1.0]]) reference_prob = rnn.pad_sequence([reference_prob0]) loss = EntropyLoss() objective = loss( rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ) assert () == objective.shape
def test_reference_embed(self): e = PreviousActionsEmbedding(1, 2, 3) input = torch.zeros(13, 3, dtype=torch.long) input[0, 2] = 0 input[1, 2] = 1 with torch.no_grad(): out = e(pad_sequence([input])).data assert np.allclose(out[0].numpy(), out[1].numpy())
def test_shape(self): gt0 = torch.LongTensor([[0, -1, -1], [-1, 2, -1], [-1, -1, 3]]) gt = rnn.pad_sequence([gt0], padding_value=-1) rule_prob0 = torch.FloatTensor([[0.8, 0.2], [0.5, 0.5], [0.5, 0.5]]) rule_prob = rnn.pad_sequence([rule_prob0]) token_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5], [0.1, 0.2, 0.8], [0.5, 0.4, 0.1]]) token_prob = rnn.pad_sequence([token_prob0]) reference_prob0 = torch.FloatTensor([[0.1, 0.4, 0.5, 0.0], [0.0, 0.5, 0.4, 0.1], [0.0, 0.0, 0.0, 1.0]]) reference_prob = rnn.pad_sequence([reference_prob0]) loss = Loss() objective = loss(rule_probs=rule_prob, token_probs=token_prob, reference_probs=reference_prob, ground_truth_actions=gt) assert () == objective.shape
def test_embedding_sequence(self): x0 = torch.LongTensor([0, 1]) x1 = torch.LongTensor([0, 1, 1]) x = pad_sequence([x0, x1], padding_value=-1) embedding = EmbeddingWithMask(2, 2, -1) torch.nn.init.eye_(embedding.weight) output = embedding(x) assert np.allclose([[1, 0], [0, 1], [0, 0]], output.data[:, 0, :].detach().numpy()) assert np.array_equal([[1, 1], [1, 1], [0, 1]], output.mask.detach().numpy())
def test_shape(self): decoder = Decoder(1, 1, 5, 3, 3, 1, 0.0, 5, 5) in0 = torch.rand(5, 1) in0 = pad_sequence([in0], 0) depth = torch.Tensor(5, 1) in1 = torch.rand(5, 1) in1 = pad_sequence([in1], 0) adj = torch.Tensor(1, 5, 5) query0 = torch.rand(5, 1) nl0 = torch.Tensor(11, 1) out = decoder( action_query_features=pad_sequence([query0], 0), nl_query_features=pad_sequence([nl0], 0), action_features=in0, action_rule_features=in1, depthes=depth, adjacency_matrix=adj, ) assert (5, 1, 3) == out.data.shape assert (5, 1) == out.mask.shape
def test_shape(self): block = ActionSequenceReaderBlock(2, 3, 1, 3, 0.0, 0) in0 = torch.Tensor(5, 3) in0 = pad_sequence([in0], 0) depth = torch.Tensor(5, 1) in1 = torch.Tensor(5, 1, 2) adj = torch.Tensor(1, 5, 5) out, weight = block(in0, depth, in1, adj) assert (5, 1, 3) == out.data.shape assert (5, 1) == out.mask.shape assert (1, 5, 5) == weight.shape
def test_probs(self): embedding = ActionsEmbedding(1, 1, 1, 1, 1) predictor = Predictor(embedding, 1, 2, 3, 5) feature0 = torch.rand(2, 3) feature1 = torch.rand(1, 3) feature = rnn.pad_sequence([feature0, feature1]) context0 = torch.rand(2, 2) context1 = torch.rand(1, 2) context = rnn.pad_sequence([context0, context1]) ref0 = torch.rand(3, 2) ref1 = torch.rand(1, 2) reference = rnn.pad_sequence([ref0, ref1]) rule_pred, token_pred, reference_pred = predictor( reference_features=reference, action_features=feature, action_contexts=context) probs = \ torch.sum(rule_pred.data, dim=2) + torch.sum(token_pred.data, dim=2) + \ torch.sum(reference_pred.data, dim=2) assert np.allclose([[1, 1], [1, 1]], probs.detach().numpy())
def test_nl_mask(self): predictor = Predictor(2, 3, 5, 7, 11) f0 = torch.rand(11, 2) f1 = torch.rand(13, 2) nl0 = torch.rand(13, 3) nl1 = torch.rand(15, 3) rule0, token0, ref0 = predictor( reference_features=pad_sequence([nl0]), action_features=pad_sequence([f0])) rule1, token1, ref1 = predictor( reference_features=pad_sequence([nl0, nl1]), action_features=pad_sequence([f0, f1])) rule1 = rule1.data[:11, :1, :] token1 = token1.data[:11, :1, :] ref1 = ref1.data[:11, :1, :13] assert np.allclose(rule0.data.detach().numpy(), rule1.detach().numpy()) assert np.allclose(token0.data.detach().numpy(), token1.detach().numpy()) assert np.allclose(ref0.data.detach().numpy(), ref1.detach().numpy())