Element-wise sum of each of the input tensors
| 1108 | |
| 1109 | |
| 1110 | class Sum(Operator): |
| 1111 | """ |
| 1112 | Element-wise sum of each of the input tensors |
| 1113 | """ |
| 1114 | |
| 1115 | def __init__(self): |
| 1116 | super(Sum, self).__init__() |
| 1117 | |
| 1118 | def forward(self, *l): |
| 1119 | """ |
| 1120 | Args: |
| 1121 | l (a list of CTensor): element-wise sum operator |
| 1122 | Returns: |
| 1123 | a CTensor for the result |
| 1124 | """ |
| 1125 | if training: |
| 1126 | self.l = len(l) |
| 1127 | assert (len(l) > 0) |
| 1128 | x = singa.Tensor(list(l[0].shape()), l[0].device()) |
| 1129 | x.SetFloatValue(0.0) |
| 1130 | for i in range(len(l)): |
| 1131 | x += l[i] |
| 1132 | return x |
| 1133 | |
| 1134 | def backward(self, dy): |
| 1135 | """ |
| 1136 | Args: |
| 1137 | dy (CTensor): dL / dy |
| 1138 | Returns: |
| 1139 | dx (CTensor): dL / dx |
| 1140 | """ |
| 1141 | return [dy] * self.l |
| 1142 | |
| 1143 | |
| 1144 | def sum(*l): |
no outgoing calls