Esempio n. 1
0
 def test_add_unresolved_components_distribution(self):
     v = mfc.Variable()
     m = mfc.Variable()
     f = Normal.define_variable(mean=m, variance=v)
     component_set = set((v, m, f))
     self.fg.f = f
     self.assertTrue(component_set <= set(self.fg.components_graph.nodes().keys()),
                     "Variables are all added to _components_graph {} {}".format(component_set, self.fg.components_graph.nodes().keys()))
     self.assertTrue(set((v.uuid, m.uuid, f.uuid)) <= self.fg.components.keys(), "Variable is added to _components dict. {} {}".format(v.uuid, self.fg.components))
Esempio n. 2
0
    def test_replicate_variable_with_variable_shape(self):
        m = mf.models.Model(verbose=False)
        y = mfc.Variable()
        m.x = mfc.Variable(shape=(y, 1))
        var_map = {}

        def func(component):
            return 'recursive', 'recursive'

        x2 = m.x.replicate(var_map=var_map, replication_function=func)
        self.assertTrue(x2.uuid == m.x.uuid)
        self.assertTrue(x2.shape == m.x.shape, (x2.shape, m.x.shape))
        self.assertTrue(y in m)
Esempio n. 3
0
 def make_bnn_model(self, net):
     component_set = set()
     m = mf.models.Model(verbose=False)
     m.N = mfc.Variable()
     m.f = MXFusionGluonFunction(net, num_outputs=1)
     m.x = mfc.Variable(shape=(m.N,))
     m.r = m.f(m.x)
     for k, v in m.r.factor.parameters.items():
         if k.endswith('_weight') or k.endswith('_bias'):
             d = mf.components.distributions.Normal(mean=mx.nd.array([0]), variance=mx.nd.array([1e6]))
             v.set_prior(d)
             component_set.union(set([v,d]))
     m.y = mf.components.distributions.Categorical.define_variable(log_prob=m.r, num_classes=2, normalization=True, one_hot_encoding=False)
     component_set.union(set([m.N, m.f, m.x, m.r, m.y]))
     return m, component_set
Esempio n. 4
0
 def test_assignment_with_variable_success(self):
     v = mfc.Variable()
     self.fg.v = v
     self.assertTrue(v in self.fg.components_graph.nodes(),
                     "Variable is added to _components_graph.")
     self.assertTrue(v.uuid in self.fg.components,
                     "Variable is added to _components dict.")
Esempio n. 5
0
 def test_replicate_simple_model(self):
     m = mf.models.Model(verbose=False)
     m.x = mfc.Variable(shape=(2,))
     m.x_mean = mfc.Variable(value=mx.nd.array([0, 1]), shape=(2,))
     m.x_var = mfc.Variable(value=mx.nd.array([1e6]))
     d = mf.components.distributions.Normal(mean=m.x_mean, variance=m.x_var)
     m.x.set_prior(d)
     m2 = m.clone()
     # compare m and m2 components and such for exactness.
     self.assertTrue(set([v.uuid for v in m.components.values()]) ==
                     set([v.uuid for v in m2.components.values()]))
     self.assertTrue(all([v in m.components for v in m2.components]), (set(m2.components) - set(m.components)))
     self.assertTrue(all([v in m2.components for v in m.components]), (set(m.components) - set(m2.components)))
     self.assertTrue(all([m.x.shape == m2.x.shape,
                          m.x_mean.shape == m2.x_mean.shape,
                          m.x_var.shape == m2.x_var.shape]))
Esempio n. 6
0
 def test_remove_variable_success(self):
     v = mfc.Variable()
     self.fg.v = v
     self.assertTrue(v in self.fg.components_graph.nodes(), "Variable is added to _components_graph.")
     self.assertTrue(v.uuid in self.fg.components, "Variable is added to _components dict.")
     self.fg.remove_component(v)
     self.assertFalse(v in self.fg.components_graph.nodes(), "Variable is removed from _components_graph.")
     self.assertFalse(v.uuid in self.fg.components, "Variable is removed from _components dict.")
Esempio n. 7
0
    def test_same_variable_as_multiple_inputs_to_factor_not_in_graph(self):

        x = mfc.Variable()
        y = mf.components.distributions.Normal.define_variable(mean=x, variance=x)

        self.assertTrue(set([v for _, v in y.factor.predecessors]) == set([x]))
        self.assertTrue(set([v for _, v in x.successors]) == set([y.factor]))
        self.assertTrue(len(y.factor.predecessors) == 2)
        self.assertTrue(len(x.successors) == 2)
Esempio n. 8
0
    def test_replicate_variable(self):
        m = mf.models.Model(verbose=False)
        m.x = mfc.Variable()

        def func(component):
            return 'recursive', 'recursive'

        x2 = m.x.replicate(replication_function=func)
        self.assertTrue(x2.uuid == m.x.uuid)
Esempio n. 9
0
 def test_replicate_factor_only_self(self):
     m = mf.models.Model(verbose=False)
     m.x = mfc.Variable()
     d = mf.components.distributions.Normal(mean=mx.nd.array([0]), variance=mx.nd.array([1e6]))
     m.x.set_prior(d)
     def func(component):
         return 'recursive', 'recursive'
     x2 = m.x.replicate(replication_function=func)
     self.assertTrue(x2.factor is not None)
Esempio n. 10
0
    def test_same_variable_as_multiple_inputs_to_factor_in_graph(self):
        fg = mf.models.Model()

        fg.x = mfc.Variable()
        fg.y = mf.components.distributions.Normal.define_variable(mean=fg.x, variance=fg.x)

        self.assertTrue(set([v for _, v in fg.y.factor.predecessors]) == set([fg.x]))
        self.assertTrue(set([v for _, v in fg.x.successors]) == set([fg.y.factor]))
        self.assertTrue(len(fg.y.factor.predecessors) == 2)
        self.assertTrue(len(fg.x.successors) == 2)
Esempio n. 11
0
    def test_set_prior_after_factor_attach(self):
        fg = mf.models.Model()
        d = mf.components.distributions.Normal(mean=mx.nd.array([1e2]), variance=mx.nd.array([1e6]))
        fg.d = d

        x = mfc.Variable()
        x.set_prior(d)
        self.assertTrue(set([v for _, v in d.successors]) == set([x]))
        self.assertTrue(set([v for _, v in x.predecessors]) == set([d]))
        self.assertTrue(x.graph == d.graph and d.graph == fg.components_graph)
Esempio n. 12
0
 def test_add_unresolved_components_function(self):
     f = MXFusionGluonFunction(self.basic_net, num_outputs=1)
     x = mfc.Variable()
     y = f(x)
     component_set = set((x, y))
     self.fg.y = y
     self.assertTrue(component_set <= set(self.fg.components_graph.nodes().keys()),
                     "Variables are all added to _components_graph {} {}".format(component_set, self.fg.components_graph.nodes().keys()))
     self.assertTrue(set(map(lambda x: x.uuid, component_set)) <= self.fg.components.keys(), "Variable is added to _components dict. {} {}".format(set(map(lambda x: x.uuid, component_set)), self.fg.components.keys()))
     self.assertTrue(len(self.fg.components_graph.nodes().keys()) == 5, "There should be variables for the block parameters and a function evaluation totally 5 nodes in the graph but there were only {} in {}".format(len(self.fg.components_graph.nodes().keys()), self.fg.components_graph.nodes().keys()))
Esempio n. 13
0
    def test_replicate_function_only_self(self):

        self.D = 10
        self.net = nn.HybridSequential()
        with self.net.name_scope():
            self.net.add(nn.Dense(self.D, activation="relu"))

        m = mf.models.Model(verbose=False)
        f = MXFusionGluonFunction(self.net, num_outputs=1)
        m.x = mfc.Variable()
        m.y = f(m.x)

        def func(component):
            return 'recursive', 'recursive'
        y2 = m.y.replicate(replication_function=func)
        self.assertTrue(y2.factor is not None)
Esempio n. 14
0
    def test_set_prior(self):
        m = mf.models.Model(verbose=False)
        m.x = mfc.Variable()
        component_set = set([m.x])
        self.assertTrue(
            component_set <= set(m._components_graph.nodes().keys()),
            "Variables are all added to components_graph {} {}".format(
                component_set,
                m.components_graph.nodes().keys()))

        d = mf.components.distributions.Normal(mean=mx.nd.array([0]),
                                               variance=mx.nd.array([1e6]))
        m.x.set_prior(d)
        component_set.union(set([d] + [v for _, v in d.inputs]))
        self.assertTrue(
            component_set <= set(m._components_graph.nodes().keys()),
            "Variables are all added to components_graph {} {}".format(
                component_set,
                m.components_graph.nodes().keys()))
Esempio n. 15
0
 def test_replicate_simple_model(self):
     m = mf.models.Model(verbose=False)
     m.x = mfc.Variable()
     d = mf.components.distributions.Normal(mean=mx.nd.array([0]),
                                            variance=mx.nd.array([1e6]))
     m.x.set_prior(d)
     m2, var_map = m.clone()
     # compare m and m2 components and such for exactness.
     self.assertTrue(all([k.uuid == v.uuid for k, v in var_map.items()]))
     self.assertTrue(
         all([
             k.uuid == v.uuid
             for k, v in zip(m.components.values(), m2.components.values())
         ]))
     self.assertTrue(all([v in m.components for v in m2.components]),
                     (set(m2.components) - set(m.components)))
     self.assertTrue(all([v in m2.components for v in m.components]),
                     (set(m.components) - set(m2.components)))
     self.assertTrue(
         all([
             self.shape_match(old.shape, new.shape, var_map) for old, new in
             zip(m.variables.values(), m2.variables.values())
         ]))
Esempio n. 16
0
 def test_remove_nonexistant_variable_failure(self):
     v = mfc.Variable()
     with self.assertRaises(ModelSpecificationError):
         self.fg.remove_component(v)