def test_add_encoder0(self):
        """
    Test commit
    """
        tf.reset_default_graph()
        builder = StaticBuilder("MyModel")

        builder.addInput(10)
        cust = builder.createCustomNode(1, 1, name="Custom")
        cust_in1 = cust.addInner(3)
        cust_in2 = cust.addInner(4)
        cust.addDirectedLink(cust_in1, cust_in2)
        cust.commit()
        builder.addOutput()
    def test_BuildModel0(self):
        """
    Test building the simplest model possible.
    """
        print("\nTest 4: Building a Basic Model")
        builder = StaticBuilder(scope="Basic")
        in_name = builder.addInput(10)
        enc_name = builder.addInner(3)
        out_name = builder.addOutput()
        builder.addDirectedLink(in_name, enc_name)
        builder.addDirectedLink(enc_name, out_name)

        self.assertEqual(
            builder.num_nodes, 3, "The number of nodes has not been "
            "assigned correctly")

        builder.build()
        inn, enc, out = (builder.nodes[in_name], builder.nodes[enc_name],
                         builder.nodes[out_name])
        self.assertEqual(inn._oslot_to_otensor[0].shape.as_list()[-1],
                         enc._islot_to_itensor[0].shape.as_list()[-1],
                         "The input tensors have not been assigned correctly")
        self.assertEqual(enc._oslot_to_otensor[0].shape.as_list()[-1],
                         out._islot_to_itensor[0].shape.as_list()[-1],
                         "The input tensors have not been assigned correctly")
    def test_addDirectedLinks(self):
        """
    Test adding DirectedLinks
    """
        print("\nTest 3: Adding DirectedLinks")
        builder = StaticBuilder()
        in1 = builder.addInput(10, name="In")
        enc1 = builder.addInner(3, name="Det")
        out1 = builder.addOutput(name="Out")

        builder.addDirectedLink(in1, enc1)
        builder.addDirectedLink(enc1, out1)

        enc1 = builder.nodes[enc1]
        print('\nNode keys in builder:', list(builder.nodes.keys()))
        self.assertEqual(
            builder.num_nodes, 3, "The number of nodes has not been "
            "assigned correctly")
        self.assertEqual(
            enc1.num_declared_outputs, 1, "The number of outputs of the "
            "DeterministicNode has not been assigned correctly")
        self.assertEqual(
            enc1.num_declared_inputs, 1, "The number of inputs of the "
            "DeterministicNode has not been assigned correctly")
        self.assertIn(
            1, builder.adj_list[0], "Node 1 has not been added to the "
            "adjacency list of Node 0")
        self.assertIn(
            2, builder.adj_list[1], "Node 2 has not been added to the "
            "adjacency list of Node 1")
예제 #4
0
    def test_train_custom_builder(self):
        """
    """
        print("Running Test 0\n")
        x = 10.0 * np.random.randn(100, 2)
        y = x[:,
              0:1] + 1.5 * x[:,
                             1:]  # + 3*x[:,1:]**2 + 0.5*np.random.randn(100,1)
        dataset = {'train_features': x, 'train_input_response': y}

        # Define a builder.
        # More control on the directives of each node
        builder = StaticBuilder()
        enc_dirs = {
            'num_layers': 2,
            'num_nodes': 128,
            'activation': 'leaky_relu',
            'net_grow_rate': 1.0
        }
        input_dim, output_dim = 2, 1
        in0 = builder.addInput(input_dim, name="features")
        enc1 = builder.addInner(1, 10, directives=enc_dirs)
        enc2 = builder.addInner(1, output_dim, directives=enc_dirs)
        out0 = builder.addOutput(name="prediction")

        builder.addDirectedLink(in0, enc1)
        builder.addDirectedLink(enc1, enc2)
        builder.addDirectedLink(enc2, out0)

        # Pass the builder object to the Regression Model
        reg = Regression(builder=builder)
        reg.build()
        reg.visualize_model_graph("two_encoders.png")
        reg.train(dataset, num_epochs=50)
    def test_BuildModel2(self):
        """
    Builds a model with 2 inputs. Test ConcatNode
    """
        print("\nTest 6: Building a Model with Concat")
        builder = StaticBuilder("Concat")
        in1 = builder.addInput(10)
        in2 = builder.addInput(20)
        enc1 = builder.addInner(3, num_islots=2)
        out1 = builder.addOutput()

        builder.addDirectedLink(in1, enc1, islot=0)
        builder.addDirectedLink(in2, enc1, islot=1)
        builder.addDirectedLink(enc1, out1)

        builder.build()
    def test_BuildModel1(self):
        """
    Test building a model with 2 outputs. Test Cloning an output
    """
        print("\nTest 5: Building a Model with cloning")
        builder = StaticBuilder("Clone")
        in1 = builder.addInput(10)
        enc1 = builder.addInner(3)
        out1 = builder.addOutput(name="Out1")
        out2 = builder.addOutput(name="Out2")

        builder.addDirectedLink(in1, enc1)
        builder.addDirectedLink(enc1, out1)
        builder.addDirectedLink(enc1, out2)

        builder.build()
    def test_BuildModel3(self):
        """
    Try to break it, the algorithm... !! Guess not mdrfkr.
    """
        print("\nTest 7: Building a more complicated Model")
        builder = StaticBuilder("BreakIt")
        in1 = builder.addInput(10)
        in2 = builder.addInput(20)
        enc1 = builder.addInner(3)
        enc2 = builder.addInner(5, num_islots=2)
        out1 = builder.addOutput()
        out2 = builder.addOutput()

        builder.addDirectedLink(in1, enc1)
        builder.addDirectedLink(in2, enc2, islot=0)
        builder.addDirectedLink(enc1, enc2, islot=1)
        builder.addDirectedLink(enc1, out1)
        builder.addDirectedLink(enc2, out2)

        builder.build()
예제 #8
0
    def test_train_custom_builder2(self):
        """
    Build a custom Regression model whose Model graph has the rhombic design
    """
        print("Running Test 1\n")
        x = 10.0 * np.random.randn(100, 2)
        y = x[:,
              0:1] + 1.5 * x[:,
                             1:]  # + 3*x[:,1:]**2 + 0.5*np.random.randn(100,1)
        dataset = {'train_features': x, 'train_input_response': y}

        # Define a builder, control the graph
        # Like a lot more control...
        builder = StaticBuilder()
        enc_dirs = {
            'num_layers': 2,
            'num_nodes': 128,
            'activation': 'leaky_relu',
            'net_grow_rate': 1.0
        }
        input_dim = 2
        in0 = builder.addInput(input_dim, name="features")
        enc1 = builder.addInner(10, num_islots=1, directives=enc_dirs)
        enc2 = builder.addInner(10, num_islots=1, directives=enc_dirs)
        enc3 = builder.addInner(1,
                                num_islots=2,
                                num_layers=1,
                                activation='linear')
        out0 = builder.addOutput(name="prediction")

        builder.addDirectedLink(in0, enc1)
        builder.addDirectedLink(in0, enc2)
        builder.addDirectedLink(enc1, enc3, islot=0)
        builder.addDirectedLink(enc2, enc3, islot=1)
        builder.addDirectedLink(enc3, out0)

        # Pass the builder object to the Regression Model
        reg = Regression(input_dim=2, output_dim=1, builder=builder)
        reg.build()
        reg.visualize_model_graph("two_encoders.png")
        reg.train(dataset, num_epochs=50)
    def test_train_custom_node2(self):
        """
    Test commit
    """
        print("Test 2: with CustomNode\n")
        input_dim = 2
        x = 10.0 * np.random.randn(100, input_dim)
        y = x[:,
              0:1] + 1.5 * x[:,
                             1:]  # + 3*x[:,1:]**2 + 0.5*np.random.randn(100,1)
        dataset = {'train_features': x, 'train_input_response': y}

        builder = StaticBuilder("MyModel")
        enc_dirs = {
            'num_layers': 2,
            'num_nodes': 128,
            'activation': 'leaky_relu',
            'net_grow_rate': 1.0
        }

        in0 = builder.addInput(input_dim, name='features')

        cust = builder.createCustomNode(1, 1, name="Custom")
        cust_in1 = cust.addInner(3, directives=enc_dirs)
        cust_in2 = cust.addInner(1, directives=enc_dirs)
        cust.addDirectedLink(cust_in1, cust_in2)
        cust.addInput(islot=0, inode_name=cust_in1, inode_islot=0)
        cust.addOutput(oslot=0, inode_name=cust_in2, inode_oslot=0)
        cust.commit()

        out0 = builder.addOutput(name='prediction')

        builder.addDirectedLink(in0, cust)
        builder.addDirectedLink(cust, out0)

        # Pass the builder object to the Regression Model
        reg = Regression(input_dim=2, output_dim=1, builder=builder)
        reg.build()
        reg.visualize_model_graph("two_encoders.png")
        reg.train(dataset, num_epochs=50)
    def test_addOutput(self):
        """
    Test adding basic OutputNode
    """
        print("\nTest 2: Adding OutputNode")
        builder = StaticBuilder()
        builder.addInput(10, name="In")
        builder.addInner(3, name="Det")
        o_name = builder.addOutput(name="Out")

        o1 = builder.nodes[o_name]
        print("\nNode keys in builder:", list(builder.nodes.keys()))
        print("This node's key:", o_name)
        self.assertEqual(o1.label, 2,
                         "The label has not been assigned correctly")
        self.assertEqual(
            builder.num_nodes, 3, "The number of nodes has not been "
            "assigned correctly")
        self.assertEqual(
            o1.num_declared_outputs, 0, "The number of outputs of the "
            "OutputNode has not been assigned correctly")
        self.assertEqual(
            o1.num_declared_inputs, 0, "The number of inputs of the "
            "OutputNode has not been assigned correctly")