Source code for mlprodict.grammar.gactions_tensor

"""
Action definition.


:githublink:`%|py|5`
"""
import numpy
from .gactions import MLActionFunctionCall


[docs]class MLActionTensorDot(MLActionFunctionCall): """ Scalar product. :githublink:`%|py|12` """
[docs] def __init__(self, act1, act2): """ :param act1: first tensor :param act2: second tensor :githublink:`%|py|18` """ MLActionFunctionCall.__init__(self, "adot", act1.output, act1, act2) # dot product takes two vectors and returns a float self.output = act1.output.element_type
[docs] def _optional_parameters(self): return str(self.inputs[0].dim[0])
[docs] def execute(self, **kwargs): """ Addition :githublink:`%|py|29` """ MLActionFunctionCall.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(self.output.softcast(numpy.dot(res[0], res[1])))
[docs]class MLActionTensorTake(MLActionFunctionCall): """ Extracts an element of the tensor. :githublink:`%|py|38` """
[docs] def __init__(self, tens, ind): """ :param tens: tensor :param ind: index :githublink:`%|py|44` """ MLActionFunctionCall.__init__(self, "atake", tens.output, tens, ind) self.output = tens.output.element_type
[docs] def _optional_parameters(self): return str(self.inputs[0].dim[0])
[docs] def execute(self, **kwargs): """ Addition :githublink:`%|py|54` """ MLActionFunctionCall.execute(self, **kwargs) res = self.ChildrenResults if res[1] < 0: raise ValueError("Cannot take element {0}".format(res[1])) if res[1] >= len(res[0]): raise ValueError( "Cannot take element {0} >= size={1}".format(res[1], len(res[0]))) return self.output.validate(self.output.softcast(res[0][res[1]]))
[docs]class MLActionTensorVector(MLActionFunctionCall): """ Tensor operation. :githublink:`%|py|68` """
[docs] def __init__(self, act1, act2, name, fct): """ :param act1: first tensor :param act2: second tensor :param name: operator name :param fct: function :githublink:`%|py|76` """ MLActionFunctionCall.__init__(self, name, act1.output, act1, act2) self.output = act1.output self.fct = fct
[docs] def _optional_parameters(self): return str(self.inputs[0].dim[0])
[docs] def execute(self, **kwargs): """ Addition :githublink:`%|py|87` """ MLActionFunctionCall.execute(self, **kwargs) res = self.ChildrenResults return self.output.validate(self.fct(res[0], res[1]))
[docs]class MLActionTensorSub(MLActionTensorVector): """ Tensor soustraction. :githublink:`%|py|96` """
[docs] def __init__(self, act1, act2): """ :param act1: first tensor :param act2: second tensor :githublink:`%|py|102` """ MLActionTensorVector.__init__( self, act1, act2, "asub", lambda v1, v2: v1 - v2)
[docs]class MLActionTensorMul(MLActionTensorVector): """ Tensor multiplication. :githublink:`%|py|110` """
[docs] def __init__(self, act1, act2): """ :param act1: first tensor :param act2: second tensor :githublink:`%|py|116` """ MLActionTensorVector.__init__( self, act1, act2, "amul", lambda v1, v2: numpy.multiply(v1, v2))
[docs]class MLActionTensorDiv(MLActionTensorVector): """ Tensor division. :githublink:`%|py|124` """
[docs] def __init__(self, act1, act2): """ :param act1: first tensor :param act2: second tensor :githublink:`%|py|130` """ MLActionTensorVector.__init__( self, act1, act2, "adiv", lambda v1, v2: numpy.divide(v1, v2))
[docs]class MLActionTensorAdd(MLActionTensorVector): """ Tensor addition. :githublink:`%|py|138` """
[docs] def __init__(self, act1, act2): """ :param act1: first tensor :param act2: second tensor :githublink:`%|py|144` """ MLActionTensorVector.__init__( self, act1, act2, "aadd", lambda v1, v2: v1 + v2)