| 5051 | self.shape = shape |
| 5052 | |
| 5053 | def forward(self, x): |
| 5054 | if isinstance(self.shape, np.ndarray): |
| 5055 | self.shape = self.shape.tolist() |
| 5056 | else: |
| 5057 | self.shape = list(self.shape) |
| 5058 | self.x_shape = list(x.shape()) |
| 5059 | x_shape = self.x_shape.copy() |
| 5060 | self.dim_changed = True if len(self.shape) != len(x_shape) else False |
| 5061 | if self.dim_changed: |
| 5062 | tmp_tensor = singa.Tensor(self.shape, x.device()) |
| 5063 | tmp_tensor.SetFloatValue(1.) |
| 5064 | x = singa.__mul__(x, tmp_tensor) |
| 5065 | else: |
| 5066 | for axis, s_1, s_2 in zip(range(len(self.shape)), self.shape, |
| 5067 | x_shape): |
| 5068 | if s_1 == s_2: |
| 5069 | continue |
| 5070 | xs = [x] * (s_1 // s_2) |
| 5071 | x = singa.VecTensor(xs) |
| 5072 | x = singa.ConcatOn(x, axis) |
| 5073 | return x |
| 5074 | |
| 5075 | def backward(self, dy): |
| 5076 | x_shape = self.x_shape |