Ejemplo n.º 1
0
class FnApprox(params.ParameterizedObject):

    num_inputs = params.Integer(default=1, bounds=(1, None))
    num_outputs = params.Integer(default=1, bounds=(1, None))
    batch_epochs = params.Integer(default=1, bounds=(1, None))

    range = params.Parameter(default=(0, 1))

    def __init__(self, **params):
        super(FnApprox, self).__init__(**params)
        self.MSE = 0

    def __call__(self, X, error=False):
        """
        Apply self to X.
        """
        raise base.NYI

    def learn_step(self, X, Y):
        """
        Learn step for self(X)=Y
        """
        raise base.NYI

    def learn_batch(self, data):
        """
        Learns on a batch of data, given as a sequence.  The batch is shuffled,
        then presented sequentially to self.learn_step()
        """
        import plastk.rand
        self.verbose("Training on", len(data), "examples.")
        for i in xrange(self.batch_epochs):
            data = rand.shuffle(data)
            for X, Y in data:
                self.learn_step(X, Y)

    def scale_input(self, X):
        rmin, rmax = self.range
        return (X - rmin) / (rmax - rmin)

    def scale_output(self, X):
        rmin, rmax = self.range
        return X * (rmax - rmin) + rmin
Ejemplo n.º 2
0
 def test_missing_value(self):
     """Ensure an error is raised on missing parameterError"""
     param = params.Parameter("test")
     with self.assertRaises(params.MissingParameterError):
         param.parse({"test2": "value"})
Ejemplo n.º 3
0
 def test_parse_multi_parameters(self):
     """Ensure that other parameters are not lost"""
     param = params.Parameter("test")
     parsed_kwargs = param.parse({"test": "value", "doNotDelete": True})
     self.assertEqual(parsed_kwargs["doNotDelete"], True)
Ejemplo n.º 4
0
 def test_parse_str(self):
     """Ensure that we can do a simple parse via a parameter"""
     param = params.Parameter("test")
     parsed_kwargs = param.parse({"test": "value"})
     self.assertIn("test", parsed_kwargs)
     self.assertEqual(parsed_kwargs["test"], "value")
Ejemplo n.º 5
0
 def test_replace_fn(self):
     "Ensures that the decorator works with functions"
     fn = params.Parameter("value", default=2)(temp_fn)
     self.assertEqual(fn(), 2)
Ejemplo n.º 6
0
 def test_replace_sub_fn(self):
     "Ensures that the decorator works with class members"
     self.cls.sub_fn = params.Parameter("value", default=2)(self.cls.sub_fn)
     instance = self.cls()
     self.assertEqual(instance.sub_fn(), 2)
Ejemplo n.º 7
0
 def test_replace_init_exception(self):
     """Ensure that the replace_init raises missing parameter error"""
     self.cls = params.Parameter("value")(self.cls)
     with self.assertRaises(params.MissingParameterError):
         instance = self.cls()
Ejemplo n.º 8
0
    def test_replace_init(self):
        """Ensure that the replace_init function doesn't overshadow"""

        self.cls = params.Parameter("value")(self.cls)
        instance = self.cls(**{"value": "abc"})
        self.assertEqual(instance.value, "abc")
Ejemplo n.º 9
0
 def test_default_none(self):
     """Ensure that explicit default none get assigned"""
     param = params.Parameter("test", default=None)
     parsed_kwargs = param.parse({})
     self.assertEqual(parsed_kwargs["test"], None)
Ejemplo n.º 10
0
 def test_default_parse(self):
     """Ensure that the default is loaded when missing parameter"""
     param = params.Parameter("test", default="default_value")
     parsed_kwargs = param.parse({})
     self.assertEqual(parsed_kwargs["test"], "default_value")