Ejemplo n.º 1
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_init(self):
     """
 Create a CustomNode
 """
     tf.reset_default_graph()
     builder = StaticBuilder()
     builder.createCustomNode(1, 1, name="Custom")
Ejemplo n.º 3
0
    def test_train_custom_node2(self):
        """
    Test a custom Regression model with a CustomNode
    """
        print("Test 2: with CustomNode\n")

        builder = StaticBuilder("MyModel")
        enc_dirs = {
            'numlayers': 2,
            'numnodes': 128,
            'activations': 'leaky_relu',
            'netgrowrate': 1.0
        }

        input_dim = 2
        in0 = builder.addInput(input_dim, name='Features')

        cust = builder.createCustomNode(inputs=[in0],
                                        num_outputs=1,
                                        name="Prediction")
        cust_in1 = cust.addTransformInner(3,
                                          main_inputs=[0],
                                          directives=enc_dirs)
        cust_in2 = cust.addTransformInner(1,
                                          main_inputs=cust_in1,
                                          directives=enc_dirs)
        cust.declareOslot(oslot=0,
                          innernode_name=cust_in2,
                          inode_oslot_name='main')

        reg = Regression(builder=builder, input_dim=2, output_dim=1)
        #                      save_on_valid_improvement=True) # ok!
        reg.train(dataset, num_epochs=10)
    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("\nTest 1: Adding InnerNode")
        try:
            builder = StaticBuilder()
            builder.addInput(10, name="In")
            enc_name = builder.addInner(3, name="In")
        except AttributeError:
            print("\nCAUGHT! Trying to assign the same name to two nodes! "
                  "AttributeError exception\n")
            builder = StaticBuilder()
            builder.addInput(10, name="In")
            enc_name = builder.addInner(3, name="Det")

        enc1 = builder.nodes[enc_name]
        print('\nNode keys in builder:', list(builder.nodes.keys()))
        print("This node's key:", enc_name)
        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")
Ejemplo n.º 5
0
 def test_merge_init(self):
   """
   Test Merge Node initialization
   """
   builder = StaticBuilder(scope='Main')
   in1 = builder.addInner([[3]], num_inputs=1, node_class=NormalTriLNode)
   in2 = builder.addInner([[3]], num_inputs=1, node_class=NormalTriLNode)
   builder.addMergeNode(node_list=[in1, in2],
                        merge_class=MergeNormals)
    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")
Ejemplo n.º 7
0
 def test_init(self):
   """
   Create a CustomNode
   """
   print("Test 0: Initialization")
   builder = StaticBuilder(scope='BuildCust')
   i1 = builder.addInput([[3]], name='In1')
   cust = builder.createCustomNode(i1, 3, name="Custom")
   
   print("cust.name", cust.name)
   print("cust.num_expected_inputs", cust.num_expected_inputs)
   print("cust.num_expected_outputs", cust.num_expected_outputs)
Ejemplo n.º 8
0
 def declare_cell_encoder(self):
   """
   """
   if self.builder is None:
     builder = StaticBuilder(scope='Main')
   else:
     builder = self.builder
   
   enc_name = builder.addInner(self.state_dim,
                               num_inputs=2,
                               node_class=NormalTriLNode,
                               name='NormalTrilCell')
   return builder.nodes[enc_name]
Ejemplo n.º 9
0
  def test_add_encoder0(self):
    """
    Test commit
    """
    print("Test 1: Adding nodes to Custom")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    cust = builder.createCustomNode(inputs=i1,
                                     num_outputs=1,
                                     name="Custom")
    cust.addTransformInner(3, main_inputs=[0])
    print("cust.in_builder.inode_to_innernode", cust.in_builder.islot_to_innernode_names)
Ejemplo n.º 10
0
    def test_CallCustom(self):
        """
    Test, calling a CustomNode
    """
        print("\nTest 1: Build")
        builder = StaticBuilder("MyModel")

        cust = builder.createCustomNode(num_inputs=1,
                                        num_outputs=1,
                                        name="Custom")
        cust_in1 = cust.addInner(3)
        cust_in2 = cust.addInner(4)
        cust.addDirectedLink(cust_in1, cust_in2, islot=0)

        cust.declareIslot(islot=0, innernode_name=cust_in1, inode_islot=0)
        cust.declareOslot(oslot='main',
                          innernode_name=cust_in2,
                          inode_oslot='main')

        in1 = builder.addInput(10)
        builder.addDirectedLink(in1, cust, islot=0)
        builder.build()

        X = tf.placeholder(tf.float32, [1, 10], 'X')
        Y = cust(X)  # pylint: disable=unpacking-non-sequence
        print('Y', Y)
        self.assertEqual(Y[0].shape[-1], 4, "")
Ejemplo n.º 11
0
  def test_add_encoder1(self):
    """
    Test commit
    """
    print("Test 2: Adding more nodes to Custom")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    cust = builder.createCustomNode(inputs=i1,
                                     num_outputs=1,
                                     name="Custom")
    ctin1 = cust.addTransformInner(3, [0])
    cust.addTransformInner(4, [ctin1])
    print("cust.in_builder._inode_to_innernode", cust.in_builder.islot_to_innernode_names)
    print("cust.in_builder.input_nodes", cust.in_builder.input_nodes)
Ejemplo n.º 12
0
  def test_add_encoder2(self):
    """
    Test commit
    """
    print("Test 3: Declaring multiple inputs to the Custom Node")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    i2 = builder.addInput(5)
    cust = builder.createCustomNode(inputs=[i1,i2],
                                     num_outputs=1,
                                     name="Custom")
    ctin1 = cust.addTransformInner(3, [0])
    cust.addTransformInner(3, [1, ctin1])
    print("cust.in_builder._inode_to_innernode", cust.in_builder.islot_to_innernode_names)
    print("cust.in_builder.input_nodes", cust.in_builder.input_nodes)
Ejemplo n.º 13
0
    def test_train_custom_node2(self):
        """
    Test commit
    """
        print("Test 1: with CustomNode\n")

        builder = StaticBuilder("MyModel")
        enc_dirs = {
            'loc_numlayers': 2,
            'loc_numnodes': 64,
            'loc_activations': 'softplus',
            'loc_netgrowrate': 1.0
        }

        input_dim = 10
        in0 = builder.addInput(input_dim, name='Observation')

        # Define Custom Recognition Model
        cust_rec = builder.createCustomNode(inputs=[in0],
                                            num_outputs=1,
                                            name="Recognition")
        cust_in2 = cust_rec.addTransformInner(3,
                                              main_inputs=[0],
                                              node_class=NormalTriLNode,
                                              **enc_dirs)
        cust_rec.declareOslot(oslot=0,
                              innernode_name=cust_in2,
                              inode_oslot_name='main')

        # Define Custom Generative Model
        cust_gen = builder.createCustomNode(inputs=cust_rec.name,
                                            num_outputs=1,
                                            name="Generative")
        cust_ginn1 = cust_gen.addTransformInner(16, main_inputs=[0])
        cust_ginn2 = cust_gen.addTransformInner(10,
                                                main_inputs=cust_ginn1,
                                                node_class=NormalTriLNode,
                                                **enc_dirs)
        cust_gen.declareOslot(oslot=0,
                              innernode_name=cust_ginn2,
                              inode_oslot_name='main')

        # Define VAE and train
        vae = VariationalAutoEncoder(builder=builder)

        vae.train(dataset, num_epochs=10)
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    def test_init(self):
        """
    Create a Custom Cell
    """
        print("Test 0: Custom cell init")
        builder = StaticBuilder(scope='BuildCell')

        cell = BasicEncoderCell(builder, state_sizes=[[5]])
        print("cell.encoder", cell.encoder)
Ejemplo n.º 16
0
 def test_merge_build1(self):
   """
   Test Merge Node build
   """
   builder = StaticBuilder(scope='Main')
   i1 = builder.addInput([[1]])
   in1 = builder.addInner([[3]], node_class=NormalTriLNode)
   in2 = builder.addInner([[3]], node_class=NormalTriLNode)
   builder.addDirectedLink(i1, in1, islot=0)
   builder.addDirectedLink(i1, in2, islot=0)
   m1 = builder.addMergeNode([in1, in2],
                             merge_class=MergeNormals)
   builder.build()
   m1 = builder.nodes[m1]
   self.assertIn('loc', m1._oslot_to_otensor)
   self.assertIn('main', m1._oslot_to_otensor)
   self.assertIn('cov', m1._oslot_to_otensor)
    def test0_init(self):
        """
    Test adding basic InputNode
    """
        print("\nTest 0: Initialization")
        builder = StaticBuilder()
        in1_name = builder.addInput(10)
        in1 = builder.input_nodes[in1_name]

        print('Node keys in builder:', list(builder.input_nodes.keys()))
        self.assertEqual(in1.label, 0,
                         "The label has not been assigned correctly")
        self.assertEqual(
            builder.num_nodes, 1, "The number of nodes has not been "
            "assigned correctly")
        self.assertEqual(
            in1.num_declared_outputs, 0, "The number of outputs of "
            "the InputNode has not been assigned correctly")
        self.assertEqual(
            in1.num_declared_inputs, 0, "The number of outputs of "
            "the InputNode has not been assigned correctly")
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    def build(self):
        """
    Builds the Regression.
    
    => E =>
    """
        if self._is_built:
            raise ValueError("Node is already built")
        builder = self.builder

        ftrs_dirs = self.directives['ftrs']
        enc_dirs = self.directives['enc']
        tr_dirs = self.directives['tr']
        if builder is None:
            self.builder = builder = StaticBuilder(scope=self._main_scope)

            in0 = builder.addInput(self.input_dim,
                                   name="Features",
                                   **ftrs_dirs)
            builder.addTransformInner(state_size=1,
                                      main_inputs=in0,
                                      name='Prediction',
                                      **enc_dirs)
        else:
            self._check_custom_build()
            builder.scope = self._main_scope
        builder.addInput(self.output_dim, name="Observation")

        # build the tensorflow graph
        builder.build()
        self.nodes = builder.nodes
        self.otensor_names = builder.otensor_names
        self.dummies = self.builder.dummies

        # Define cost and trainer attribute
        cost_declare = ('mse', ('Prediction', 'Observation'))
        cost_func = self.summaries_dict[cost_declare[0]]
        self.cost = cost_func(self.nodes, cost_declare[1])
        self.otensor_names['cost'] = self.cost.name

        # trainer object
        self.trainer = GDTrainer(model=self,
                                 keep_logs=self.keep_logs,
                                 **tr_dirs)
        if self.save:  # save
            self.save_otensor_names()

        print("\nThe following names are available for evaluation:")
        for name in sorted(self.otensor_names.keys()):
            print('\t', name)

        self._is_built = True
Ejemplo n.º 20
0
    def test_call(self):
        """
    Call a Custom Cell Node
    """
        print("Test 1: Custom cell call")
        builder = StaticBuilder(scope='BuildCell')

        cell = BasicEncoderCell(builder, state_sizes=[[5]])
        print("cell.encoder", cell.encoder)

        X = tf.placeholder(tf.float64, [None, 5])
        Y = tf.placeholder(tf.float64, [None, 10])
        Z = cell(X, Y)
        print("Z", Z)
Ejemplo n.º 21
0
    def test_NormalTriLCell(self):
        """
    Test the NormalTriLCell Node
    """
        print("Test 2: ")
        builder = StaticBuilder(scope='BuildCell')

        cell = NormalTriLCell(builder, state_sizes=[[5]])
        print("cell.encoder", cell.encoder)

        X = tf.placeholder(tf.float64, [None, 5])
        Y = tf.placeholder(tf.float64, [None, 10])
        Z = cell(X, Y)
        print("Z", Z)
Ejemplo n.º 22
0
    def test_init1(self):
        """
    Test adding basic InputNode
    """
        print("Test 1: Simple build")
        builder = StaticBuilder(scope='Build')
        in1_name = builder.addInput(state_size=10, iclass=NormalInputNode)
        in1 = builder.input_nodes[in1_name]

        print('Node keys in builder:', list(builder.input_nodes.keys()))
        self.assertEqual(in1.label, 0,
                         "The label has not been assigned correctly")
        self.assertEqual(
            builder.num_nodes, 1, "The number of nodes has not been "
            "assigned correctly")
        self.assertEqual(
            in1.num_declared_outputs, 0, "The number of outputs of "
            "the InputNode has not been assigned correctly")
        self.assertEqual(
            in1.num_declared_inputs, 0, "The number of outputs of "
            "the InputNode has not been assigned correctly")
        builder.build()

        print('in1._oslot_to_otensor', in1._oslot_to_otensor)
    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_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()
Ejemplo n.º 25
0
    def addInputSequence(self,
                         state_size,
                         name=None,
                         node_class=PlaceholderInputNode,
                         **dirs):
        """
    Add an InputSequence
    """
        node_name = StaticBuilder.addInput(self,
                                           state_size=state_size,
                                           iclass=node_class,
                                           name=name,
                                           is_sequence=True,
                                           **dirs)
        self.input_sequences[node_name] = self.input_nodes[node_name]

        return node_name
Ejemplo n.º 26
0
 def addInputSequence(self,
                      num_features,
                      name=None,
                      node_class=PlaceholderInputNode,
                      **dirs):
   """
   Add an InputSequence
   """
   node_name = StaticBuilder.addInput(self,
                                      num_features=num_features,
                                      name=name,
                                      iclass=node_class,
                                      max_steps=self.max_steps,
                                      is_sequence=True,
                                      **dirs)
   self.input_sequences[node_name] = self.input_nodes[node_name]
   
   return node_name
Ejemplo n.º 27
0
    def test_CallDeterministic(self):
        """
    Test Calling a DeterministicNode
    """
        print("\nTest 0: Calling a Deterministic Node")
        builder = StaticBuilder(scope="Basic")
        in_name = builder.addInput(10)
        enc_name = builder.addInner(3)
        builder.addDirectedLink(in_name, enc_name, islot=0)

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

        X = tf.placeholder(tf.float32, [1, 10], 'X')
        enc = builder.nodes[enc_name]
        Y = enc((X))
        print('Y', Y)
        self.assertEqual(Y.shape[-1], 3, "")
Ejemplo n.º 28
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)
Ejemplo n.º 29
0
  def test_build3(self):
    """
    Test build
    """
    print("Test 7: Slightly more complicated Custom build")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    i2 = builder.addInput(5)
    cust = builder.createCustomNode(inputs=[i1,i2],
                                     num_outputs=1,
                                     name="Custom")
    ctin1 = cust.addTransformInner(3, [0])
    ctin2 = cust.addTransformInner(3, [1, ctin1])
    cust.declareOslot(oslot=0, innernode_name=ctin2, inode_oslot_name='main')
    
    builder.build()
Ejemplo n.º 30
0
 def test_merge_build2(self):
   """
   Test Merge Node build
   """
   builder = StaticBuilder(scope='Main')
   i1 = builder.addInput([[3]], iclass=NormalInputNode, name='N1')
   i2 = builder.addInput([[3]], iclass=NormalInputNode, name='N2')
   m1 = builder.addMergeNode(node_list=[i1, i2],
                             merge_class=MergeNormals)
   builder.build()
   
   sess = tf.Session(graph=tf.get_default_graph())
   sess.run(tf.global_variables_initializer())
   
   s3 = builder.eval_node_oslot(sess, m1,
                                oslot='cov')
   print("merge output", s3)
   print("merge output shape", s3.shape)