Exemplo n.º 1
0
    def architecture_children(self):
        children = self.raw_children()
        gate = children["gate"]
        transform = children["transform"]

        # prepare gates
        transform_gate = tn.SequentialNode(
            self.name + "_transformgate",
            [
                gate,
                # add initial value as bias instead
                # TODO parameterize
                tn.AddConstantNode(self.name + "_biastranslation", value=-4),
                tn.SigmoidNode(self.name + "_transformgatesigmoid")
            ])
        # carry gate = 1 - transform gate
        carry_gate = tn.SequentialNode(self.name + "_carrygate", [
            tn.ReferenceNode(self.name + "_transformgateref",
                             reference=transform_gate.name),
            tn.MultiplyConstantNode(self.name + "_invert", value=-1),
            tn.AddConstantNode(self.name + "_add", value=1)
        ])

        # combine with gates
        gated_transform = tn.ElementwiseProductNode(
            self.name + "_gatedtransform", [transform_gate, transform])
        gated_carry = tn.ElementwiseProductNode(
            self.name + "_gatedcarry",
            [carry_gate, tn.IdentityNode(self.name + "_carry")])
        res = tn.ElementwiseSumNode(self.name + "_res",
                                    [gated_carry, gated_transform])
        return [res]
Exemplo n.º 2
0
def GeometricMeanOutNode(name, epsilon=1e-8, **kwargs):
    return tn.SequentialNode(name, [
        tn.ReLUNode(name + "_relu"),
        tn.AddConstantNode(name + "_add", value=epsilon),
        TimesoutNode(name + "_to", **kwargs),
        tn.SqrtNode(name + "_sqrt"),
        tn.AddConstantNode(name + "_sub", value=-(epsilon**2))
    ])
Exemplo n.º 3
0
def test_remove_parents():
    network1 = tn.SequentialNode("seq", [
        tn.InputNode("i", shape=()),
        tn.HyperparameterNode("hp1",
                              tn.HyperparameterNode(
                                  "hp2", tn.AddConstantNode("ac"), value=1),
                              value=2)
    ]).network()

    network2 = canopy.transforms.remove_parents(network1, "ac")

    nt.assert_equal(tn.AddConstantNode("ac"), network2.root_node)
Exemplo n.º 4
0
    def architecture_children(self):
        gate_node = tn.SequentialNode(
            self.name + "_gate_seq",
            [
                batch_fold.AddAxisNode(self.name + "_add_axis", axis=2),
                batch_fold.FoldUnfoldAxisIntoBatchNode(
                    self.name + "_batch_fold",
                    # NOTE: using dnn conv, since pooling is normally strided
                    # and the normal conv is slow with strides
                    tn.DnnConv2DWithBiasNode(self.name + "_conv",
                                             num_filters=1),
                    axis=1),
                batch_fold.RemoveAxisNode(self.name + "_remove_axis", axis=2),
                tn.SigmoidNode(self.name + "_gate_sigmoid")
            ])

        inverse_gate_node = tn.SequentialNode(self.name + "_max_gate", [
            tn.ReferenceNode(self.name + "_gate_ref",
                             reference=gate_node.name),
            tn.MultiplyConstantNode(self.name + "_", value=-1),
            tn.AddConstantNode(self.name + "_add1", value=1)
        ])

        mean_node = tn.ElementwiseProductNode(
            self.name + "_mean_product",
            [tn.MeanPool2DNode(self.name + "_mean_pool"), gate_node])

        max_node = tn.ElementwiseProductNode(
            self.name + "_max_product",
            [tn.MaxPool2DNode(self.name + "_max_pool"), inverse_gate_node])

        return [
            tn.ElementwiseSumNode(self.name + "_sum", [mean_node, max_node])
        ]
Exemplo n.º 5
0
def test_auxiliary_cost_node():
    network = tn.HyperparameterNode(
        "hp",
        tn.SequentialNode("seq", [
            tn.InputNode("x", shape=(3, 4, 5)),
            tn.AuxiliaryCostNode(
                "cost1", {"target": tn.InputNode("y1", shape=(3, 4, 5))}),
            tn.AddConstantNode("a1", value=2),
            tn.AuxiliaryCostNode(
                "cost2", {"target": tn.InputNode("y2", shape=(3, 4, 5))}),
            tn.MultiplyConstantNode("m1", value=2),
            tn.AuxiliaryCostNode(
                "cost3", {"target": tn.InputNode("y3", shape=(3, 4, 5))}),
            tn.ConstantNode("const", value=0),
            tn.InputElementwiseSumNode("cost")
        ]),
        cost_reference="cost",
        cost_function=treeano.utils.squared_error,
    ).network()
    fn = network.function(["x", "y1", "y2", "y3"], ["cost"])
    x = np.random.rand(3, 4, 5).astype(fX)
    ys = [np.random.rand(3, 4, 5).astype(fX) for _ in range(3)]

    def mse(x, y):
        return ((x - y)**2).mean()

    expected_output = (mse(x, ys[0]) + mse(x + 2, ys[1]) +
                       mse(2 * (x + 2), ys[2]))
    np.testing.assert_allclose(fn(x, *ys)[0], expected_output, rtol=1e-5)
Exemplo n.º 6
0
def test_with_hyperparameters():
    network = tn.SequentialNode(
        "seq", [tn.InputNode("i", shape=()),
                tn.AddConstantNode("ac")]).network()

    fn = canopy.handlers.handled_fn(
        network, [canopy.handlers.with_hyperparameters("hp", value=3)],
        {"x": "i"}, {"out": "ac"})
    nt.assert_equal(3, fn({"x": 0})["out"])
Exemplo n.º 7
0
def test_override_hyperparameters1():
    network = tn.SequentialNode(
        "seq",
        [tn.InputNode("i", shape=()),
         tn.AddConstantNode("ac", value=1)]).network()

    fn = canopy.handlers.handled_fn(
        network, [canopy.handlers.override_hyperparameters(value=2)],
        {"x": "i"}, {"out": "ac"})
    nt.assert_equal(2, fn({"x": 0})["out"])
Exemplo n.º 8
0
def test_dict_children_container_schema_optional_children():
    dccs = core.DictChildrenContainerSchema(
        foo=core.ListChildrenContainer,
        bar=core.ChildContainer,
    )
    node = tn.AddConstantNode("hello")
    in_map = {"foo": [node, node]}
    cc = dccs(in_map)
    # test that .children returns the same as the input
    nt.assert_equal(cc.children, in_map)
Exemplo n.º 9
0
def test_graph_node_no_input():
    network = tn.GraphNode(
        "g",
        [(tn.InputNode("i", shape=()),
          tn.MultiplyConstantNode("m1", value=2),
          tn.AddConstantNode("a1", value=2)),
         [{"from": "i", "to": "a1"},
          {"from": "a1", "to": "m1"},
          {"from": "m1"}]]
    ).network()
    fn = network.function(["i"], ["g"])
    nt.assert_equal([10], fn(3))
Exemplo n.º 10
0
def test_graph_node_no_output_key():
    network = tn.SequentialNode(
        "s",
        [tn.InputNode("i", shape=()),
         tn.GraphNode("g",
                      [(tn.MultiplyConstantNode("m1", value=2),
                        tn.AddConstantNode("a1", value=2)),
                       [{"to": "a1"},
                        {"from": "a1", "to": "m1"},
                        {"from": "m1",  "to_key": "foo"}]])]
    ).network()
    fn = network.function(["i"], ["s"])
    nt.assert_equal([3], fn(3))
Exemplo n.º 11
0
def test_dict_children_container_schema_serialization():
    dccs = core.DictChildrenContainerSchema(
        foo=core.ListChildrenContainer,
        bar=core.ChildContainer,
    )
    node = tn.AddConstantNode("hello")
    in_map = {"foo": [node, node], "bar": node}
    cc1 = dccs(in_map)
    cc2 = core.children_container_from_data(
        core.children_container_to_data(cc1))

    nt.assert_equal(cc1.__class__, cc2.__class__)
    nt.assert_equal(cc1.__dict__, cc2.__dict__)
Exemplo n.º 12
0
def test_add_hyperparameters():
    network1 = treeano.Network(tn.SequentialNode(
        "seq", [tn.InputNode("i", shape=()),
                tn.AddConstantNode("ac")]),
                               default_hyperparameters={"value": 2})
    fn1 = network1.function(["i"], ["ac"])
    nt.assert_equal(2, fn1(0)[0])
    network2 = canopy.transforms.add_hyperparameters(network1, "hp",
                                                     dict(value=3))
    print(network2.root_node)
    fn2a = network2.function(["i"], ["ac"])
    nt.assert_equal(3, fn2a(0)[0])
    fn2b = network2.function(["i"], ["hp"])
    nt.assert_equal(3, fn2b(0)[0])
Exemplo n.º 13
0
def test_dict_children_container_schema():
    dccs = core.DictChildrenContainerSchema(
        foo=core.ListChildrenContainer,
        bar=core.ChildContainer,
    )
    node = tn.AddConstantNode("hello")
    cc1 = dccs({"foo": [node, node], "bar": node})
    cc2 = core.children_container._DictChildrenContainerFromSchema({
        "foo":
        core.ListChildrenContainer([node, node]),
        "bar":
        core.ChildContainer(node)
    })
    # test that it makes the expected class
    nt.assert_equal(cc1, cc2)
Exemplo n.º 14
0
def forget_gate_conv_2d_node(name,
                             num_filters,
                             filter_size=(3, 3),
                             initial_bias=0):
    return tn.ElementwiseProductNode(name, [
        tn.IdentityNode(name + "_identity"),
        tn.SequentialNode(name + "_forget", [
            tn.Conv2DWithBiasNode(name + "_conv",
                                  num_filters=num_filters,
                                  filter_size=filter_size,
                                  stride=(1, 1),
                                  pad="same"),
            tn.AddConstantNode(name + "_initial_bias", value=initial_bias),
            tn.SigmoidNode(name + "_sigmoid")
        ])
    ])
Exemplo n.º 15
0
def test_graph_node():
    network = tn.GraphNode(
        "g1",
        [[tn.InputNode("i", shape=()),
          tn.GraphNode("g2",
                       [(tn.MultiplyConstantNode("m1", value=2),
                         tn.AddConstantNode("a1", value=2)),
                        [{"to": "a1"},
                         {"from": "a1", "to": "m1"},
                         {"from": "m1", "to_key": "foo"}]],
                       output_key="foo")],
         [{"from": "i", "to": "g2"},
          {"from": "g2", "to_key": "bar"}]],
        output_key="bar"
    ).network()
    fn = network.function(["i"], ["a1", "m1", "g1", "g2"])
    nt.assert_equal([5, 10, 10, 10], fn(3))
Exemplo n.º 16
0
def test_remove_nodes():
    network1 = tn.SequentialNode("seq", [
        tn.InputNode("i", shape=()),
        tn.HyperparameterNode("hp1",
                              tn.HyperparameterNode(
                                  "hp2", tn.AddConstantNode("ac"), value=1),
                              value=2)
    ]).network()
    fn1 = network1.function(["i"], ["seq"])
    nt.assert_equal(1, fn1(0)[0])
    network2 = canopy.transforms.remove_nodes(network1, {"hp2"},
                                              keep_child=True)
    fn2 = network2.function(["i"], ["seq"])
    nt.assert_equal(2, fn2(0)[0])
    network3 = canopy.transforms.remove_nodes(network1, {"ac"})
    fn3 = network3.function(["i"], ["seq"])
    nt.assert_equal(0, fn3(0)[0])
Exemplo n.º 17
0
def test_remove_parent():
    network1 = tn.SequentialNode("seq", [
        tn.InputNode("i", shape=()),
        tn.HyperparameterNode("hp1",
                              tn.HyperparameterNode(
                                  "hp2", tn.AddConstantNode("ac"), value=1),
                              value=2)
    ]).network()
    fn1 = network1.function(["i"], ["seq"])
    nt.assert_equal(1, fn1(0)[0])
    network2 = canopy.transforms.remove_parent(network1, {"ac"})
    fn2 = network2.function(["i"], ["seq"])
    nt.assert_equal(2, fn2(0)[0])

    network3 = canopy.transforms.remove_parent(network1, {"i"})

    @nt.raises(Exception)
    def fails(name):
        network3.function(["i"], [name])

    # testing that these nodes are removed
    fails("ac")
    fails("seq")
    network3.function(["i"], ["i"])
Exemplo n.º 18
0
def BiasedDoubleTimesoutNode(name, bias=1, **kwargs):
    return tn.SequentialNode(name, [
        tn.AddConstantNode(name + "_add", value=bias),
        DoubleTimesoutNode(name + "_to", **kwargs),
        tn.AddConstantNode(name + "_sub", value=-(bias**2))
    ])