Exemplo n.º 1
0
    def test_raises_on_malformed(self):
        # simple smoke test from the example file
        tree1 = ((0, 1), ((1, 2), ((0, 1), ), ((-1, 0), )),
                 ((-3, 0), ((2, 3), ), ((1, 2), )))

        tree2 = ((16, 3, 2), ((0, 1), ((5, 3), ), ((2, 6), )), ((2, 9), ))

        trees = [tree1, tree2]

        # function to extract the left child of a node
        def left_child(x):
            assert isinstance(x, tuple)
            if len(x) == 1:
                # leaf.
                return None
            return x[1]

        # function to extract the right child of node
        def right_child(x):
            assert isinstance(x, tuple)
            if len(x) == 1:
                # leaf.
                return None
            return x[2]

        # function to transform a node into a (feature) vector,
        # should be a numpy array.
        def transformer(x):
            return np.array(x[0])

        with self.assertRaises(TreeConvolutionError):
            prepare_trees(trees, transformer, left_child, right_child)
Exemplo n.º 2
0
    def test_example(self):
        # simple smoke test from the example file
        tree1 = ((0, 1), ((1, 2), ((0, 1), ), ((-1, 0), )),
                 ((-3, 0), ((2, 3), ), ((1, 2), )))

        tree2 = ((16, 3), ((0, 1), ((5, 3), ), ((2, 6), )), ((2, 9), ))

        trees = [tree1, tree2]

        # function to extract the left child of a node
        def left_child(x):
            assert isinstance(x, tuple)
            if len(x) == 1:
                # leaf.
                return None
            return x[1]

        # function to extract the right child of node
        def right_child(x):
            assert isinstance(x, tuple)
            if len(x) == 1:
                # leaf.
                return None
            return x[2]

        # function to transform a node into a (feature) vector,
        # should be a numpy array.
        def transformer(x):
            return np.array(x[0])

        prepared_trees = prepare_trees(trees, transformer, left_child,
                                       right_child)
        net = nn.Sequential(tcnn.BinaryTreeConv(2, 16), tcnn.TreeLayerNorm(),
                            tcnn.TreeActivation(nn.ReLU()),
                            tcnn.BinaryTreeConv(16, 8), tcnn.TreeLayerNorm(),
                            tcnn.TreeActivation(nn.ReLU()),
                            tcnn.BinaryTreeConv(8, 4), tcnn.TreeLayerNorm(),
                            tcnn.TreeActivation(nn.ReLU()),
                            tcnn.DynamicPooling())

        # output: torch.Size([2, 4])
        shape = tuple(net(prepared_trees).shape)
        self.assertEqual(shape, (2, 4))
Exemplo n.º 3
0
    assert isinstance(x, tuple)
    if len(x) == 1:
        # leaf.
        return None
    return x[1]

def right_child(x):
    assert isinstance(x, tuple)
    if len(x) == 1:
        # leaf.
        return None
    return x[2]

def transformer(x):
    return np.array(x[0])

net = nn.Sequential(
    tcnn.BinaryTreeConv(10, 16),
    tcnn.TreeLayerNorm(),
    tcnn.TreeActivation(nn.ReLU()),
    tcnn.BinaryTreeConv(16, 8),
    tcnn.TreeLayerNorm(),
    tcnn.TreeActivation(nn.ReLU()),
    tcnn.BinaryTreeConv(8, 4),
    tcnn.TreeLayerNorm(),
    tcnn.TreeActivation(nn.ReLU()),
    tcnn.DynamicPooling()
)

prepared_trees = prepare_trees(trees, transformer, left_child, right_child)
print(net(prepared_trees))
Exemplo n.º 4
0
# prepared_tredddes_train = prepare_trees(trees, transformer, left_child, right_child)
# training_set = WatDivDataset(prepared_tredddes_train, np.array([1., 2., 3.]))
# training_generator = torch.utils.data.DataLoader(training_set, **params)
#
# prepared_tredddes_val = prepare_trees(treesVal, transformer, left_child, right_child)
# validation_set = WatDivDataset(prepared_tredddes_val, np.array([1., 2., 3.]))
# validation_generator = torch.utils.data.DataLoader(validation_set, **params)
#
# for train_step, (trees, target) in enumerate(training_generator):
#     print(trees, target)

#scale target in log scale and StandardScaller
scalery, y_train_log_std, y_val_log_std, y_test_log_std = prepare_log_std_target(
    y_train, y_val, y_test)

prepared_trees_train = prepare_trees(x_train.values, transformer, left_child,
                                     right_child)
prepared_trees_val = prepare_trees(x_val.values, transformer, left_child,
                                   right_child)
prepared_trees_test = prepare_trees(x_test.values, transformer, left_child,
                                    right_child)
# Parameters

# Generators
training_set = WatDivDataset(prepared_trees_train,
                             y_train_log_std.reshape(-1, 1))
training_generator = torch.utils.data.DataLoader(training_set, **params)

validation_set = WatDivDataset(prepared_trees_val,
                               y_val_log_std.reshape(-1, 1))
validation_generator = torch.utils.data.DataLoader(validation_set, **params)