def test_DeclareModel3(self):
   """
   """
   builder = SequentialBuilder(max_steps=10, scope="Basic")
   
   i1 = builder.addInput(2, iclass=NormalInputNode)
   in1 = builder.addInputSequence(10, 10)
   enc1 = builder.addEvolutionSequence(2, init_states=[i1], num_islots=2)
   o1 = builder.addOutputSequence()
   
   builder.addDirectedLink(in1, enc1, islot=1)
   builder.addDirectedLink(enc1, o1)
 def test_DeclareModel2(self):
   """
   """
   builder = SequentialBuilder(max_steps=10, scope="Basic")
   
   i1 = builder.addInput(2, iclass=NormalInputNode)
   builder.addInputSequence(10, 10)
   builder.addEvolutionSequence(2, init_states=[i1], num_islots=2)
   builder.addOutputSequence()
Esempio n. 3
0
    def test_LDSEvolution(self):
        """
    Test LDSNode build
    """
        builder = SequentialBuilder(scope='Main', max_steps=2)
        i0 = builder.addInput([[3]], NormalInputNode, name='Prior')
        i1 = builder.addInputSequence([[3]], name='InSeq')
        in1 = builder.addEvolutionwPriors([[3]],
                                          main_inputs=i1,
                                          prior_inputs=i0,
                                          node_class=LDSEvolution,
                                          name='LDS')

        builder.build()

        in1, i1 = builder.nodes[in1], builder.nodes[i1]
        print("{}._oslot_to_otensor:".format(i1.name), i1._oslot_to_otensor)
        print("{}._oslot_to_otensor:".format(in1.name), in1._oslot_to_otensor)
        print("{}._islot_to_itensor:".format(in1.name), in1._islot_to_itensor)

        Z = tf.placeholder(tf.float64, [None, 2, 3])
        in1.build_loc(imain0=Z)
        lp = in1.logprob(Z)
        print("logprob,", lp)

        sess = tf.Session(graph=tf.get_default_graph())
        sess.run(tf.global_variables_initializer())

        z = np.array([[[1.0, 2.0, 3.0], [1.0, 1.0, 1.0]]])
        opt = builder.eval_node_oslot(sess,
                                      'LDS',
                                      feed_dict={'Main/InSeq_main:0': z})
        print("output", opt)
Esempio n. 4
0
    def test_LLDSEvolution_build(self):
        """
    Test LLDSNode initialization
    """
        builder = SequentialBuilder(scope='Main', max_steps=2)
        i0 = builder.addInput([[3]], NormalInputNode, name='Prior')
        i1 = builder.addInputSequence([[3]], name='InSeq')
        in1 = builder.addEvolutionwPriors([[3]],
                                          main_inputs=i1,
                                          prior_inputs=i0,
                                          node_class=LLDSEvolution,
                                          name='LLDS')

        builder.build()

        in1, i1 = builder.nodes[in1], builder.nodes[i1]
        print("{}._oslot_to_otensor:".format(i1.name), i1._oslot_to_otensor)
        print("{}._oslot_to_otensor:".format(in1.name), in1._oslot_to_otensor)

        sess = tf.Session(graph=tf.get_default_graph())
        sess.run(tf.global_variables_initializer())

        z = np.array([[[1.0, 2.0, 3.0], [1.0, 0.0, 0.0]]])
        opt = builder.eval_node_oslot(sess,
                                      'LLDS',
                                      oslot='A',
                                      feed_dict={'Main/InSeq_main:0': z})
        print("output", opt)
Esempio n. 5
0
    def build(self):
        """
    Builds the RNNClassifier
    """
        builder = self.builder
        dirs = self.directives
        if builder is None:
            self.builder = builder = SequentialBuilder(
                scope=self.main_scope,
                max_steps=self.max_steps,
                batch_size=self.batch_size)
            #       init_states = get_cell_init_states(builder,
            #                                          self.cell_class,
            #                                          dirs['hidden_dims'])
            #       num_init_states = len(init_states)
            #       evseq_num_inputs = self.num_inputs + num_init_states
            #       iseq_islot = num_init_states

            #       else:
            #         cell = self.cell_class(state_size=self.latent_dim,
            #                                builder=builder,
            #                                **dirs)
            #         init_states = cell.get_istates()
            #         evseq_num_inputs = cell.num_expected_inputs
            #         iseq_islot = evseq_num_inputs - 1
            is1 = builder.addInputSequence(self.input_dim, name='iseq')
            evs1 = builder.addEvolutionSequence(
                num_features=self.latent_dim,
                #                                           num_inputs=evseq_num_inputs,
                num_inputs=2,
                #                                           init_states=init_states,
                ev_seq_class=self.seq_class,
                cell_class=self.cell_class,
                name='ev_seq',
                **dirs)
            inn1 = builder.addInnerSequence(self.num_labels)
            os1 = builder.addOutputSequence(name='prediction')

            #       builder.addDirectedLink(is1, evs1, islot=iseq_islot)
            builder.addDirectedLink(is1, evs1, islot=1)
            builder.addDirectedLink(evs1, inn1)
            builder.addDirectedLink(inn1, os1)

            self._adj_list = builder.adj_list
        else:
            self._check_custom_build()
            builder.scope = self.main_scope
        is2 = builder.addInputSequence(1, name='i_labels', data_type='int32')
        os2 = builder.addOutputSequence(name='labels')
        builder.addDirectedLink(is2, os2)

        builder.build()

        self.nodes = self.builder.nodes
        self.cost = dirs['loss_func'](self.nodes)  #pylint: disable=not-callable
        self.trainer = GDTrainer(self.cost, **dirs)

        self._is_built = True
 def test_build_custom0(self):
   """
   Test build Custom Cell
   """
   print("Test 0:")
   state_dim = 10
   builder = SequentialBuilder(max_steps=25,
                               scope="Main")
   is1 = builder.addInputSequence([[1]], name='Features')
   prior = builder.addInput(state_dim,
                            iclass=NormalInputNode,
                            name='Prior')
   rnn1 = builder.addRNN(main_inputs=is1,
                         state_inputs=prior,
                         cell_class=BasicEncoderCell,
                         num_inputs=2,
                         name='RNN')
   
   builder.addInnerSequence(state_sizes=[[1]],
                            main_inputs=rnn1,
                            name='Prediction')    
   
   rnn = PredictorRNN(builder=builder)
   
   rnn.train(dataset, num_epochs=10)
Esempio n. 7
0
    def test_evalLogProb(self):
        """
    """
        max_steps = 25
        dataset = generate_echo_data(1000, 3, max_steps)
        Y = dataset['train_outputSeq']

        idims = [[1]]
        builder = SequentialBuilder(scope='Test', max_steps=max_steps)

        is1 = builder.addInputSequence(idims, name='inputSeq')
        evs1 = builder.addEvolutionSequence(
            [[2]],
            ev_seq_class=NonlinearDynamicswGaussianNoise,
            num_inputs=2,
            name='EvSeq')
        builder.addDirectedLink(is1, evs1, islot=1)
        builder.build()
Esempio n. 8
0
    def test_train0(self):
        """
    Test train RNNPredictor with custom build : continuous data
    """
        print("Test 0:")
        max_steps = dataset['train_Observation'].shape[1]

        # Define a MG with 2 RNNs
        input_dims = [[1]]
        state_dim1 = [[5]]
        state_dim2 = [[10]]
        output_dims = [[1]]

        builder = SequentialBuilder(max_steps=max_steps, scope="Main")

        is1 = builder.addInputSequence(input_dims, name='Features')
        prior1 = builder.addInput(state_dim1,
                                  iclass=NormalInputNode,
                                  name='Prior1')
        rnn1 = builder.addRNN(main_inputs=is1,
                              state_inputs=prior1,
                              name='RNN1')

        prior2 = builder.addInput(state_dim2,
                                  iclass=NormalInputNode,
                                  name='Prior2')
        rnn2 = builder.addRNN(main_inputs=rnn1,
                              state_inputs=prior2,
                              name='RNN2')

        builder.addInnerSequence(state_sizes=output_dims,
                                 main_inputs=rnn2,
                                 name='Prediction')

        # Pass the builder to the PredictorRNN model
        model = PredictorRNN(builder=builder)

        model.train(dataset, num_epochs=10)
Esempio n. 9
0
    def test_BuildModel2(self):
        """
    """
        print("Test 1:")
        builder = SequentialBuilder(max_steps=10, scope="Basic")

        in1 = builder.addInputSequence(10)
        prior1 = builder.addInput([[3]], NormalInputNode, name='Prior1')
        prior2 = builder.addInput([[3]], NormalInputNode, name='Prior2')
        enc1 = builder.addRNN(main_inputs=in1, state_inputs=prior1)
        enc2 = builder.addRNN(main_inputs=enc1, state_inputs=prior2)

        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)
Esempio n. 10
0
    def test_DeclareModel3(self):
        """
    """
        print("Test 0:")
        builder = SequentialBuilder(max_steps=10, scope="Basic")

        in1 = builder.addInputSequence(10)
        prior = builder.addInput([[3]], NormalInputNode, name='Prior')
        rnn_name = builder.addRNN(main_inputs=in1, state_inputs=prior)
        builder.build()

        rnn = builder.nodes[rnn_name]
        rnn_cell = rnn.cell
        X = tf.placeholder(tf.float64, [None, 3])
        Y = tf.placeholder(tf.float64, [None, 10])
        Z = rnn_cell(X, Y)
        print("Z", Z)
Esempio n. 11
0
 def test_NormalPrecisionNode_init(self):
     """
 Test 3: NormalPrecisionNode build
 """
     print("\nTest 3: NormalPrecisionNode build")
     builder = SequentialBuilder(max_steps=30, scope='Main')
     i1 = builder.addInputSequence([[3]])
     es1 = builder.addInnerSequence([[3]],
                                    i1,
                                    node_class=NormalPrecisionNode)
     builder.build()
     enc1 = builder.nodes[es1]
     print("enc1._islot_to_itensor", enc1._islot_to_itensor)
     print("enc1._oslot_to_otensor", enc1._oslot_to_otensor)
Esempio n. 12
0
 def test_NormalTriLRNN(self):
   """
   """
   print("Test 0:")
   builder = SequentialBuilder(max_steps=10, scope="Basic")
   
   in1 = builder.addInputSequence(10)
   prior = builder.addInput([[3]],
                            NormalInputNode,
                            name='Prior')
   rnn_name = builder.addRNN(main_inputs=in1,
                             state_inputs=prior,
                             cell_class=NormalTriLCell)
   builder.build()
   
   rnn = builder.nodes[rnn_name]
   print("rnn._islot_to_itensor", rnn._islot_to_itensor)
   print("rnn._oslot_to_otensor", rnn._oslot_to_otensor)
Esempio n. 13
0
    def test_BuildModel3(self):
        """
    """
        print("Test 2:")
        builder = SequentialBuilder(max_steps=10, scope="Basic")

        in1 = builder.addInputSequence(6)
        prior1 = builder.addInput([[3]], NormalInputNode, name='Prior1')
        prior2 = builder.addInput([[3]], NormalInputNode, name='Prior2')
        enc1 = builder.addRNN(main_inputs=in1,
                              state_inputs=[prior1, prior2],
                              cell_class='lstm')
        enc2 = builder.addInnerSequence([[4]], main_inputs=enc1)

        builder.build()

        e1 = builder.nodes[enc1]
        e2 = builder.nodes[enc2]
        print("e1._islot_to_itensor", e1._islot_to_itensor)
        print("e1._oslot_to_otensor", e1._oslot_to_otensor)
        print("e2._islot_to_itensor", e2._islot_to_itensor)
        print("e2._oslot_to_otensor", e2._oslot_to_otensor)
Esempio n. 14
0
    def test_LLDSNode_build2(self):
        """
    Test LLDSNode initialization
    """
        builder = SequentialBuilder(max_steps=2, scope='Main')
        i1 = builder.addInput([[3]], NormalInputNode, name='In1')
        is2 = builder.addInputSequence([[3]], name='In2')
        is3 = builder.addInputSequence([[4]], name='In3')
        in1 = builder.addEvolutionwPriors([[3]],
                                          main_inputs=is2,
                                          prior_inputs=i1,
                                          sec_inputs=is3,
                                          node_class=LLDSEvolution,
                                          name='LLDS')

        builder.build()

        in1, i1 = builder.nodes[in1], builder.nodes[i1]
        print("{}._oslot_to_otensor:".format(i1.name), i1._oslot_to_otensor)
        print("{}._oslot_to_otensor:".format(in1.name), in1._oslot_to_otensor)
    def build(self):
        """
    Builds the RNNClassifier
    """
        builder = self.builder
        dirs = self.directives
        if builder is None:
            self.builder = builder = SequentialBuilder(
                scope=self.main_scope,
                max_steps=self.max_steps,
                batch_size=self.batch_size)
            i1 = builder.addInput(self.hidden_dim, iclass=NormalInputNode)
            is1 = builder.addInputSequence(self.input_dim, name='iseq')
            enc1 = builder.addEvolutionSequence(self.hidden_dim,
                                                init_states=[i1],
                                                num_islots=2,
                                                node_class=self.node_class)
            inn1 = builder.addInner(self.num_labels)
            os1 = builder.addOutputSequence(name='prediction')

            builder.addDirectedLink(is1, enc1, islot=1)
            builder.addDirectedLink(enc1, inn1)
            builder.addDirectedLink(inn1, os1)

            self._adj_list = builder.adj_list
        else:
            self._check_custom_build()
            builder.scope = self.main_scope
        is2 = builder.addInputSequence(1, name='i_labels', data_type='int32')
        os2 = builder.addOutputSequence(name='labels')
        builder.addDirectedLink(is2, os2)

        builder.build()

        self.nodes = self.builder.nodes
        self.cost = dirs['loss_func'](self.nodes)  #pylint: disable=not-callable
        self.trainer = GDTrainer(self.cost, **dirs)

        self._is_built = True
Esempio n. 16
0
 def test_BuildModel3(self):
   """
   """
   builder = SequentialBuilder(max_steps=10, scope="Basic")
   
   i1 = builder.addInput(2, iclass=NormalInputNode)
   i2 = builder.addInput(2, iclass=NormalInputNode)
   
   in1 = builder.addInputSequence(10, 10)
   enc1 = builder.addEvolutionSequence(2,
                                       init_states=[i1,i2],
                                       num_islots=3,
                                       node_class='lstm')
   enc2 = builder.addInnerSequence(4, num_islots=2)
   o1 = builder.addOutputSequence()
   
   builder.addDirectedLink(in1, enc1, islot=2)
   builder.addDirectedLink(in1, enc2)
   builder.addDirectedLink(enc1, enc2, islot=1)
   builder.addDirectedLink(enc2, o1)
   
   builder.build()
   
   e2 = builder.nodes[enc2]
   self.assertEqual(e2._oslot_to_otensor[0].shape.as_list()[-1],
                    4, "Error")
   print("e2._islot_to_itensor", e2._islot_to_itensor)
Esempio n. 17
0
    def test_merge_build(self):
        """
    Test Merge Node Build
    """
        ydim = 10
        xdim = 2

        builder = SequentialBuilder(scope='Main', max_steps=25)
        is1 = builder.addInputSequence([[ydim]], name='Observation')
        is2 = builder.addInputSequence([[xdim]], name='State')
        n1 = builder.addInput([[xdim]], NormalInputNode, name='Prior')
        ins1 = builder.addInnerSequence([[xdim]],
                                        main_inputs=is1,
                                        node_class=NormalPrecisionNode,
                                        name='InnSeq')
        evs1 = builder.addEvolutionwPriors([[xdim]],
                                           main_inputs=is2,
                                           prior_inputs=n1,
                                           node_class=LLDSEvolution,
                                           name='LLDS')
        m1 = builder.addMergeSeqwDS(seq_inputs=ins1,
                                    ds_inputs=evs1,
                                    prior_inputs=n1,
                                    merge_class=MergeSeqsNormalwNormalEv,
                                    name='Recognition')
        builder.addInnerSequence([[ydim]],
                                 main_inputs=m1,
                                 node_class=NormalPrecisionNode,
                                 name='Generative')

        builder.build()
Esempio n. 18
0
  def build(self):
    """
    Builds the PredictorRNN
    """
    if self._is_built:
      raise ValueError("Node is already built")
    builder = self.builder

    obs_dirs = self.directives['obs']
    ftrs_dirs = self.directives['ftrs']
    rnn_dirs = self.directives['rnn']
    pred_dirs = self.directives['model']
    if builder is None:
      self.builder = builder = SequentialBuilder(scope=self._main_scope,
                                                 max_steps=self.max_steps)
      
      is1 = builder.addInputSequence(self.input_dims,
                                     name='Features',
                                     **ftrs_dirs)
      rnn_priors = []
      for i, state_dim in enumerate(self.state_dims):
        prior = builder.addInput(state_dim[0],
                                 iclass=NormalInputNode,
                                 name='Prior'+str(i))
        rnn_priors.append(prior)
         
      print("PredictorRNN; rnn_priors", rnn_priors)
      rnn = builder.addRNN(main_inputs=is1,
                           state_inputs=rnn_priors,
                           cell_class=self.cell_class,
                           name='RNN',
                           **rnn_dirs)

      if self.is_categorical:
        builder.addInnerSequence(self.num_labels,
                                 main_inputs=rnn,
                                 name='Prediction',
                                 **pred_dirs)
      else:
        builder.addInnerSequence(self.output_dims,
                                 main_inputs=rnn,
                                 name='Prediction',
                                 **pred_dirs)            
    else:
      builder.scope = self._main_scope
    data_type = 'int32' if self.is_categorical else 'float64'
    builder.addInputSequence(self.input_dims,
                             name='Observation',
                             dtype=data_type,
                             **obs_dirs)
    
    # 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
    if self.is_categorical:
      cost_declare = ('cross_entropy_wlogits', ('Observation', 'Prediction'))
    else:
      cost_declare = ('mse', ('Observation', 'Prediction'))
    cost_func = self.summaries_dict[cost_declare[0]]
    self.cost = cost_func(self.nodes, cost_declare[1])
    builder.add_to_output_names('cost', self.cost)

    # trainer
    tr_dirs = self.directives['tr']
    self.trainer = GDTrainer(self,
                             root_rslts_dir=self.root_rslts_dir,
                             **tr_dirs)
    if self.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
Esempio n. 19
0
    def build(self):
        """
    Build VIND
    """
        builder = self.builder
        ydim = self.ydim
        xdim = self.xdim

        rec_dirs = self.directives['rec']
        post_dirs = self.directives['post']
        gen_dirs = self.directives['gen']
        llds_dirs = self.directives['llds']
        prior_dirs = self.directives['prior']
        if builder is None:
            sec_iseqs = []
            self.builder = builder = SequentialBuilder(
                scope=self._main_scope, max_steps=self.max_steps)
            obs = builder.addInputSequence([[ydim]], name='Observation')
            state = builder.addInputSequence([[xdim]], name='StateSeq')
            prior = builder.addInput([[xdim]],
                                     NormalInputNode,
                                     name='Prior',
                                     **prior_dirs)
            for i, dim in enumerate(self.sec_input_dims):
                sec_iseqs.append(
                    builder.addInputSequence([dim], name='SecInput' + str(i)))
            ins1 = builder.addInnerSequence(xdim,
                                            main_inputs=obs,
                                            node_class=NormalPrecisionNode,
                                            name='Recognition',
                                            **rec_dirs)
            evs1 = builder.addEvolutionwPriors([[xdim]],
                                               main_inputs=state,
                                               prior_inputs=prior,
                                               sec_inputs=sec_iseqs,
                                               node_class=LLDSEvolution,
                                               name='LLDS',
                                               **llds_dirs)
            m1 = builder.addMergeSeqwDS(
                seq_inputs=ins1,
                ds_inputs=evs1,
                prior_inputs=prior,  #node_list=[ins1, evs1, prior],
                merge_class=MergeSeqsNormalwNormalEv,
                name='Posterior',
                **post_dirs)
            builder.addInnerSequence([[ydim]],
                                     main_inputs=m1,
                                     node_class=NormalPrecisionNode,
                                     name='Generative',
                                     **gen_dirs)

        else:
            self._check_custom_build()
            builder.scope = self._main_scope

        # build the tensorflow graph
        builder.build()
        self._build_model_outputs()

        # pull in builder attributes
        self.nodes = builder.nodes
        self.otensor_names = builder.otensor_names
        self.dummies = builder.dummies

        cost_declare = ('elbo_vind', ('Generative', 'Posterior', 'LLDS',
                                      'Observation'))
        cost_func = self.summaries_dict[cost_declare[0]]
        self.cost = cost_func(self.nodes, cost_declare[1])
        builder.add_to_output_names('cost', self.cost)

        # trainer
        tr_dirs = self.directives['tr']
        self.trainer = VINDTrainer(self, **tr_dirs)

        if self.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
Esempio n. 20
0
    def build(self):
        """
    Build the DeepKalmanFilter
    """
        builder = self.builder

        rnn_dirs = self.directives['rnn']
        gen_dirs = self.directives['gen']
        rec_dirs = self.directives['rec']
        if builder is None:
            self.builder = builder = SequentialBuilder(
                scope=self._main_scope, max_steps=self.max_steps)
            for j, idim in enumerate(self.input_dims):
                is1 = builder.addInputSequence([idim],
                                               name='Observation_' +
                                               str(j))  # FIX!

            rnn_priors = []
            for i, rnn_dim in enumerate(self.rnn_state_dims):
                prior = builder.addInput(rnn_dim[0],
                                         iclass=NormalInputNode,
                                         name='RNNPrior' + str(i))
                rnn_priors.append(prior)
            rnn = builder.addRNN(main_inputs=is1,
                                 state_inputs=rnn_priors,
                                 cell_class=self.rnn_cell_class,
                                 name='RNN',
                                 **rnn_dirs)

            prior_evs = builder.addInput(self.ds_state_dim,
                                         iclass=NormalInputNode,
                                         name='PriorEvs')
            evs = builder.addNormalRNN(main_inputs=rnn,
                                       state_inputs=prior_evs,
                                       cell_class=NormalTriLCell,
                                       name='Recognition',
                                       **rec_dirs)
            builder.addInnerSequence(self.input_dims[:1],
                                     main_inputs=evs,
                                     node_class=NormalTriLNode,
                                     name='Generative',
                                     **gen_dirs)

        else:
            builder.scope = self._main_scope

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

        cost_declare = ('elbo', ('Generative', 'Recognition', 'Observation_0'))
        cost_func = self.summaries_dict[cost_declare[0]]
        self.cost = cost_func(self.nodes, cost_declare[1])
        builder.add_to_output_names('cost', self.cost)

        # trainer
        tr_dirs = self.directives['tr']
        self.trainer = GDTrainer(self, **tr_dirs)

        if self.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
Esempio n. 21
0
 def test_DeclareModel0(self):
   """
   """
   builder = SequentialBuilder(max_steps=10, scope="Basic")
   builder.addInputSequence(10, 10)