Coverage for mlprodict/grammar/grammar_sklearn/grammar/gactions_tensor.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Action definition.
4"""
5import numpy
6from .gactions import MLActionFunctionCall
9class MLActionTensorDot(MLActionFunctionCall):
10 """
11 Scalar product.
12 """
14 def __init__(self, act1, act2):
15 """
16 @param act1 first tensor
17 @param act2 second tensor
18 """
19 MLActionFunctionCall.__init__(self, "adot", act1.output, act1, act2)
20 # dot product takes two vectors and returns a float
21 self.output = act1.output.element_type
23 def _optional_parameters(self):
24 return str(self.inputs[0].dim[0])
26 def execute(self, **kwargs):
27 """
28 Addition
29 """
30 MLActionFunctionCall.execute(self, **kwargs)
31 res = self.ChildrenResults
32 return self.output.validate(self.output.softcast(numpy.dot(res[0], res[1])))
35class MLActionTensorTake(MLActionFunctionCall):
36 """
37 Extracts an element of the tensor.
38 """
40 def __init__(self, tens, ind):
41 """
42 @param tens tensor
43 @param ind index
44 """
45 MLActionFunctionCall.__init__(self, "atake", tens.output, tens, ind)
46 self.output = tens.output.element_type
48 def _optional_parameters(self):
49 return str(self.inputs[0].dim[0])
51 def execute(self, **kwargs):
52 """
53 Addition
54 """
55 MLActionFunctionCall.execute(self, **kwargs)
56 res = self.ChildrenResults
57 if res[1] < 0:
58 raise ValueError( # pragma: no cover
59 "Cannot take element {0}".format(res[1]))
60 if res[1] >= len(res[0]):
61 raise ValueError( # pragma: no cover
62 "Cannot take element {0} >= size={1}".format(res[1], len(res[0])))
63 return self.output.validate(self.output.softcast(res[0][res[1]]))
66class MLActionTensorVector(MLActionFunctionCall):
67 """
68 Tensor operation.
69 """
71 def __init__(self, act1, act2, name, fct):
72 """
73 @param act1 first tensor
74 @param act2 second tensor
75 @param name operator name
76 @param fct function
77 """
78 MLActionFunctionCall.__init__(self, name, act1.output, act1, act2)
79 self.output = act1.output
80 self.fct = fct
82 def _optional_parameters(self):
83 return str(self.inputs[0].dim[0])
85 def execute(self, **kwargs):
86 """
87 Addition
88 """
89 MLActionFunctionCall.execute(self, **kwargs)
90 res = self.ChildrenResults
91 return self.output.validate(self.fct(res[0], res[1]))
94class MLActionTensorSub(MLActionTensorVector):
95 """
96 Tensor soustraction.
97 """
99 def __init__(self, act1, act2):
100 """
101 @param act1 first tensor
102 @param act2 second tensor
103 """
104 MLActionTensorVector.__init__(
105 self, act1, act2, "asub", lambda v1, v2: v1 - v2)
108class MLActionTensorMul(MLActionTensorVector):
109 """
110 Tensor multiplication.
111 """
113 def __init__(self, act1, act2):
114 """
115 @param act1 first tensor
116 @param act2 second tensor
117 """
118 MLActionTensorVector.__init__( # pragma: no cover
119 self, act1, act2, "amul", lambda v1, v2: numpy.multiply(v1, v2))
122class MLActionTensorDiv(MLActionTensorVector):
123 """
124 Tensor division.
125 """
127 def __init__(self, act1, act2):
128 """
129 @param act1 first tensor
130 @param act2 second tensor
131 """
132 MLActionTensorVector.__init__(
133 self, act1, act2, "adiv", lambda v1, v2: numpy.divide(v1, v2))
136class MLActionTensorAdd(MLActionTensorVector):
137 """
138 Tensor addition.
139 """
141 def __init__(self, act1, act2):
142 """
143 @param act1 first tensor
144 @param act2 second tensor
145 """
146 MLActionTensorVector.__init__( # pragma: no cover
147 self, act1, act2, "aadd", lambda v1, v2: v1 + v2)