def test_train_custom_builder2(self):
        """
    Build a custom Regression model whose Model graph has the rhombic design
    """
        print("Test 1: Rhombic Design\n")

        # Define the builder
        builder = StaticBuilder('CustBuild')
        enc_dirs = {
            'numlayers': 2,
            'numnodes': 128,
            'activations': 'leaky_relu',
            'netgrowrate': 1.0
        }
        input_dim = 2
        in0 = builder.addInput(input_dim, name="Features")
        enc1 = builder.addTransformInner(10,
                                         main_inputs=in0,
                                         directives=enc_dirs)
        enc2 = builder.addTransformInner(10,
                                         main_inputs=in0,
                                         directives=enc_dirs)
        builder.addTransformInner(1,
                                  main_inputs=[enc1, enc2],
                                  numlayers=1,
                                  activations='linear',
                                  name='Prediction')

        reg = Regression(input_dim=2, output_dim=1, builder=builder)
        #                      save_on_valid_improvement=True)  # OK!
        reg.train(dataset, num_epochs=10)
    def test_train_custom_builder(self):
        """
    """
        print("Test 0: Chain of Encoders\n")

        # Define a builder. Get more control on the directives of each node
        enc_dirs = {
            'numlayers': 2,
            'numnodes': 128,
            'activations': 'leaky_relu',
            'netgrowrate': 1.0
        }
        input_dim, output_dim = 2, 1

        builder = StaticBuilder('CustBuild')
        in0 = builder.addInput(input_dim, name="Features")
        enc1 = builder.addTransformInner(5, main_inputs=in0, **enc_dirs)
        builder.addTransformInner(state_size=1,
                                  main_inputs=enc1,
                                  directives=enc_dirs,
                                  name='Prediction')

        # Pass the builder object to the Regression Model
        reg = Regression(builder=builder, output_dim=output_dim)
        #                      save_on_valid_improvement=True) # ok!

        reg.train(dataset, num_epochs=10)
示例#3
0
    def test_BuildModel3(self):
        """
    Try to break it, the algorithm... !! Guess not mdrfkr.
    """
        print("Test 7: Building a more complicated Model")
        builder = StaticBuilder("BreakIt")
        in1 = builder.addInput(10)
        in2 = builder.addInput(20)
        enc1 = builder.addTransformInner(3, main_inputs=in1)
        enc2 = builder.addTransformInner(5, main_inputs=[in2, enc1])

        builder.build()
        enc1, enc2 = builder.nodes[enc1], builder.nodes[enc2]
        print("enc1._islot_to_itensor", enc1._islot_to_itensor)
        print("enc1._islot_to_itensor", enc2._islot_to_itensor)
        print("enc1._oslot_to_otensor", enc1._oslot_to_otensor)
        print("enc1._oslot_to_otensor", enc2._oslot_to_otensor)
示例#4
0
    def test_BuildModel5(self):
        """
    Try to break it, the algorithm... !! Guess not mdrfkr.
    """
        print("\nTest 2: Building a Model with 2 inputs")
        builder = StaticBuilder("BreakIt")
        in1 = builder.addInput(10)
        in2 = builder.addInput(20)
        enc1 = builder.addTransformInner(3,
                                         main_inputs=[in1, in2],
                                         node_class=NormalTriLNode)
        enc2 = builder.addTransformInner(5, main_inputs=enc1)
        builder.build()

        enc1, enc2 = builder.nodes[enc1], builder.nodes[enc2]
        print("enc1._islot_to_itensor", enc1._islot_to_itensor)
        print("enc1._oslot_to_otensor", enc1._oslot_to_otensor)
        print("enc2._islot_to_itensor", enc2._islot_to_itensor)
        print("enc2._oslot_to_otensor", enc2._oslot_to_otensor)
示例#5
0
    def test_addInner(self):
        """
    Test adding basic Deterministic InnerNode. 
    
    NOTE: The DeterministicNode belongs to the class of nodes that starts with a
    known final number of outputs.
    """
        print("Test 2: Adding InnerNode")
        try:
            builder = StaticBuilder(scope='Build0')
            i1 = builder.addInput(state_size=10, name="In")
            enc_name = builder.addTransformInner(state_sizes=3,
                                                 main_inputs=i1,
                                                 name="In")
        except AttributeError:
            print("\nCAUGHT! (AttributeError exception) \n"
                  "Trying to assign the same name to two nodes!")
            builder = StaticBuilder(scope='Build0')
            i1 = builder.addInput(state_size=10, name="In")
            enc_name = builder.addTransformInner(state_size=3, main_inputs=i1)

        enc1 = builder.nodes[enc_name]
        print('\nNode keys in builder:', list(builder.nodes.keys()))
        print("This node's key:", enc_name)
        print("Builder adjacency matrix", builder.adj_matrix)
        self.assertEqual(enc1.label, 1,
                         "The label has not been assigned correctly")
        self.assertEqual(
            builder.num_nodes, 2, "The number of nodes has not been "
            "assigned correctly")
        self.assertEqual(
            enc1.num_declared_outputs, 0, "The number of outputs of the "
            "DeterministicNode has not been assigned correctly")
        self.assertEqual(
            enc1.num_declared_inputs, 0, "The number of inputs of the "
            "DeterministicNode has not been assigned correctly")
示例#6
0
    def test_BuildModel2(self):
        """
    Builds a model with 2 inputs. Test concatenation
    """
        print("Test 4: Building a Model with concat")
        builder = StaticBuilder("Concat")
        in1 = builder.addInput(10)
        in2 = builder.addInput(20)
        enc1 = builder.addTransformInner(3, main_inputs=[in1, in2])

        in1, in2, enc1 = builder.nodes[in1], builder.nodes[in2], builder.nodes[
            enc1]
        builder.build()
        print("enc1._islot_to_itensor", enc1._islot_to_itensor)
        print("enc1._oslot_to_otensor", enc1._oslot_to_otensor)
示例#7
0
    def test_BuildModel0(self):
        """
    Test building the simplest model possible.
    """
        print("Test 3: Building a Basic Model")
        builder = StaticBuilder(scope="Basic")
        in_name = builder.addInput(10)
        enc_name = builder.addTransformInner(3, main_inputs=in_name)

        inn, enc = builder.nodes[in_name], builder.nodes[enc_name]
        builder.build()
        print("enc._islot_to_itensor", enc._islot_to_itensor)
        self.assertEqual(inn._oslot_to_otensor['main'].shape.as_list()[-1],
                         enc._islot_to_itensor[0]['main'].shape.as_list()[-1],
                         "The input tensors have not been assigned correctly")
示例#8
0
    def test_train1(self):
        """
    """
        print("Test 0:")
        dirs = {}
        builder = StaticBuilder(scope='vae')

        odim = 10
        idim = 3
        hdim = 16
        i1 = builder.addInput(odim, name='Observation', **dirs)
        enc0 = builder.addTransformInner(hdim, main_inputs=i1, name='Inner')
        enc1 = builder.addTransformInner(idim,
                                         main_inputs=enc0,
                                         name='Recognition',
                                         node_class=NormalTriLNode)
        builder.addTransformInner(odim,
                                  main_inputs=enc1,
                                  name='Generative',
                                  node_class=NormalTriLNode)

        vae = VariationalAutoEncoder(builder=builder)
        #                                  save_on_valid_improvement=True) # OK!
        vae.train(dataset, num_epochs=20)
示例#9
0
    def test_BuildModel1(self):
        """
    Test building the simplest stochastic model possible.
    """
        print("\nTest 1: Building a Model with a Stochastic Node")
        builder = StaticBuilder(scope="BasicNormal")
        i1 = builder.addInput(10)
        in1 = builder.addTransformInner(3,
                                        main_inputs=i1,
                                        node_class=NormalTriLNode)

        builder.build()
        inn, enc = builder.nodes[i1], builder.nodes[in1]
        self.assertEqual(inn._oslot_to_otensor['main'].shape.as_list()[-1],
                         enc._islot_to_itensor[0]['main'].shape.as_list()[-1],
                         "The input tensors have not been assigned correctly")
        print("enc._oslot_to_otensor", enc._oslot_to_otensor)
        print("enc._islot_to_itensor", enc._islot_to_itensor)