Exemple #1
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, "")
    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_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)
  def test_build0(self):
    """
    Test commit
    """
    print("Test 4: Basic Custom build")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    cust = builder.createCustomNode(inputs=i1,
                                     num_outputs=1,
                                     name="Custom")
    ctin = cust.addTransformInner(3, main_inputs=[0])
    cust.declareOslot(0, ctin, 'main')
    
    builder.build()
    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")
    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("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)
  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()
Exemple #10
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 test_build_outputs(self):
    """
    Test commit
    """
    print("Test 8: Building outputs")
    builder = StaticBuilder("MyModel")

    i1 = builder.addInput(10)
    cust = builder.createCustomNode(inputs=i1,
                                     num_outputs=1,
                                     name="Custom")
    ctin = cust.addTransformInner(3, main_inputs=[0])
    cust.declareOslot(0, ctin, 'main')
    
    builder.build()
    
    X = tf.placeholder(tf.float64, [None, 10]) 
    cust.build_outputs(imain0=X)
Exemple #12
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)
Exemple #13
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)
    def test_add_encoder1(self):
        """
    Test build
    """
        tf.reset_default_graph()
        builder = StaticBuilder("MyModel")

        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()

        in1 = builder.addInput(10)
        o1 = builder.addOutput()
        builder.addDirectedLink(in1, cust)
        builder.addDirectedLink(cust, o1)

        builder.build()
Exemple #15
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)
    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()
Exemple #17
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, "")
  def test_build_outputs1(self):
    """
    Test build
    """
    print("Test 9: Building outputs")
    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, [0, ctin1, 1])
    cust.declareOslot(oslot=0, innernode_name=ctin2, inode_oslot_name='main')
    
    builder.build()
    
    X = tf.placeholder(tf.float64, [None, 10]) 
    Y = tf.placeholder(tf.float64, [None, 5]) 
    cust.build_outputs(imain0=X, imain1=Y)
Exemple #19
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)