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): 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|25` """ 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|34` """
[docs] def __init__(self, tens, ind): 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|46` """ 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|60` """
[docs] def __init__(self, act1, act2, name, fct): 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|73` """ 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|82` """
[docs] def __init__(self, act1, act2): MLActionTensorVector.__init__( self, act1, act2, "asub", lambda v1, v2: v1 - v2)
[docs]class MLActionTensorMul(MLActionTensorVector): """ Tensor multiplication. :githublink:`%|py|92` """
[docs] def __init__(self, act1, act2): MLActionTensorVector.__init__( self, act1, act2, "amul", lambda v1, v2: numpy.multiply(v1, v2))
[docs]class MLActionTensorDiv(MLActionTensorVector): """ Tensor division. :githublink:`%|py|102` """
[docs] def __init__(self, act1, act2): MLActionTensorVector.__init__( self, act1, act2, "adiv", lambda v1, v2: numpy.divide(v1, v2))
[docs]class MLActionTensorAdd(MLActionTensorVector): """ Tensor addition. :githublink:`%|py|112` """
[docs] def __init__(self, act1, act2): MLActionTensorVector.__init__( self, act1, act2, "aadd", lambda v1, v2: v1 + v2)