Add Bias to each row / column of the Tensor, depending on the axis arg.
| 666 | |
| 667 | |
| 668 | class AddBias(Operator): |
| 669 | """ |
| 670 | Add Bias to each row / column of the Tensor, depending on the axis arg. |
| 671 | """ |
| 672 | |
| 673 | def __init__(self, axis=0): |
| 674 | """ |
| 675 | To indicate the calculation axis, 0 for row, 1 for column. |
| 676 | Args: |
| 677 | axis (int): 0 or 1, default is 0. |
| 678 | """ |
| 679 | super(AddBias, self).__init__() |
| 680 | self.axis = axis |
| 681 | |
| 682 | def forward(self, x, b): |
| 683 | """ |
| 684 | Args: |
| 685 | x (CTensor): matrix. |
| 686 | b (CTensor): bias to be added. |
| 687 | Return: |
| 688 | the result Tensor |
| 689 | """ |
| 690 | if self.axis == 0: |
| 691 | singa.AddRow(b, x) |
| 692 | elif self.axis == 1: |
| 693 | singa.AddColumn(b, x) |
| 694 | return x |
| 695 | |
| 696 | def backward(self, dy): |
| 697 | """ |
| 698 | Args: |
| 699 | dy (CTensor): data for the dL / dy, L is the loss. |
| 700 | Return: |
| 701 | a tuple for (db, dx), db is data for dL / db, dx is data |
| 702 | for dL / dx. |
| 703 | """ |
| 704 | dtype = dy.data_type() |
| 705 | _dy = dy.AsType(tensor.float32) |
| 706 | if self.axis == 0: |
| 707 | return dy, singa.Sum(_dy, 0).AsType(dtype) |
| 708 | elif self.axis == 1: |
| 709 | return dy, singa.Sum(_dy, 0).AsType(dtype) |
| 710 | |
| 711 | |
| 712 | def add_bias(x, b, axis=0): |