Commit f4164d98 authored by Christoph Lienhard's avatar Christoph Lienhard
Browse files

added docu for make functions

also changed op -> model
parent 5d0519d3
...@@ -21,9 +21,9 @@ from ..sugar import makeOp ...@@ -21,9 +21,9 @@ from ..sugar import makeOp
from .model import Model from .model import Model
def _joint_position(op1, op2): def _joint_position(model1, model2):
a = op1.position._val a = model1.position._val
b = op2.position._val b = model2.position._val
# Note: In python >3.5 one could do {**a, **b} # Note: In python >3.5 one could do {**a, **b}
ab = a.copy() ab = a.copy()
ab.update(b) ab.update(b)
...@@ -32,59 +32,77 @@ def _joint_position(op1, op2): ...@@ -32,59 +32,77 @@ def _joint_position(op1, op2):
class ScalarMul(Model): class ScalarMul(Model):
"""Class representing a model multiplied by a scalar factor.""" """Class representing a model multiplied by a scalar factor."""
def __init__(self, factor, op): def __init__(self, factor, model):
# TODO op -> model super(ScalarMul, self).__init__(model.position)
super(ScalarMul, self).__init__(op.position)
# TODO -> floating # TODO -> floating
if not isinstance(factor, (float, int)): if not isinstance(factor, (float, int)):
raise TypeError raise TypeError
self._op = op self._model = model
self._factor = factor self._factor = factor
self._value = self._factor * self._op.value self._value = self._factor * self._model.value
self._gradient = self._factor * self._op.gradient self._gradient = self._factor * self._model.gradient
def at(self, position): def at(self, position):
return self.__class__(self._factor, self._op.at(position)) return self.__class__(self._factor, self._model.at(position))
class Add(Model): class Add(Model):
"""Class representing the sum of two models.""" """Class representing the sum of two models."""
def __init__(self, position, op1, op2): def __init__(self, position, model1, model2):
super(Add, self).__init__(position) super(Add, self).__init__(position)
self._op1 = op1.at(position) self._model1 = model1.at(position)
self._op2 = op2.at(position) self._model2 = model2.at(position)
self._value = self._op1.value + self._op2.value self._value = self._model1.value + self._model2.value
self._gradient = self._op1.gradient + self._op2.gradient self._gradient = self._model1.gradient + self._model2.gradient
@staticmethod @staticmethod
def make(op1, op2): def make(model1, model2):
position = _joint_position(op1, op2) """Build the sum of two models.
return Add(position, op1, op2)
Parameters
----------
model1: Model
First model.
model2: Model
Second model
"""
position = _joint_position(model1, model2)
return Add(position, model1, model2)
def at(self, position): def at(self, position):
return self.__class__(position, self._op1, self._op2) return self.__class__(position, self._model1, self._model2)
class Mul(Model): class Mul(Model):
"""Class representing the pointwise product of two models.""" """Class representing the pointwise product of two models."""
def __init__(self, position, op1, op2): def __init__(self, position, model1, model2):
super(Mul, self).__init__(position) super(Mul, self).__init__(position)
self._op1 = op1.at(position) self._model1 = model1.at(position)
self._op2 = op2.at(position) self._model2 = model2.at(position)
self._value = self._op1.value * self._op2.value self._value = self._model1.value * self._model2.value
self._gradient = (makeOp(self._op1.value) * self._op2.gradient + self._gradient = (makeOp(self._model1.value) * self._model2.gradient +
makeOp(self._op2.value) * self._op1.gradient) makeOp(self._model2.value) * self._model1.gradient)
@staticmethod @staticmethod
def make(op1, op2): def make(model1, model2):
position = _joint_position(op1, op2) """Build the pointwise product of two models.
return Mul(position, op1, op2)
Parameters
----------
model1: Model
First model.
model2: Model
Second model
"""
position = _joint_position(model1, model2)
return Mul(position, model1, model2)
def at(self, position): def at(self, position):
return self.__class__(position, self._op1, self._op2) return self.__class__(position, self._model1, self._model2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment