forward of OneHot, we borrow this function from onnx Args: indices (CTensor): Scalar specifying the number of classes in one-hot tensor. The values in the 'indices' input tensor are expected to be in the range [-depth, depth-1]. Re
(self, indices)
| 4752 | self.values = values |
| 4753 | |
| 4754 | def forward(self, indices): |
| 4755 | """ |
| 4756 | forward of OneHot, we borrow this function from onnx |
| 4757 | Args: |
| 4758 | indices (CTensor): Scalar specifying the number of classes in |
| 4759 | one-hot tensor. The values in the 'indices' input tensor are |
| 4760 | expected to be in the range [-depth, depth-1]. |
| 4761 | Returns: |
| 4762 | the output CTensor. |
| 4763 | """ |
| 4764 | values = tensor.to_numpy(tensor.from_raw_tensor(indices)) |
| 4765 | rank = len(values.shape) |
| 4766 | depth_range = np.arange(self.depth) |
| 4767 | if self.axis < 0: |
| 4768 | self.axis += (rank + 1) |
| 4769 | ls = values.shape[0:self.axis] |
| 4770 | rs = values.shape[self.axis:rank] |
| 4771 | targets = np.reshape(depth_range, (1,) * len(ls) + depth_range.shape + |
| 4772 | (1,) * len(rs)) |
| 4773 | values = np.reshape(np.mod(values, self.depth), ls + (1,) + rs) |
| 4774 | np_tensor = np.asarray(targets == values, dtype=np.float32) |
| 4775 | np_tensor = np_tensor * (self.values[1] - |
| 4776 | self.values[0]) + self.values[0] |
| 4777 | tmp_tensor = tensor.from_numpy(np_tensor) |
| 4778 | tmp_tensor.to_device(indices.device()) |
| 4779 | return tmp_tensor.data |
| 4780 | |
| 4781 | def backward(self, dy): |
| 4782 | """ |