(self)
| 122 | self.assertListEqual([2, 1, 3, 3], list(dx.shape())) |
| 123 | |
| 124 | def test_batch_norm(self): |
| 125 | x_shape = [2, 2] |
| 126 | x = singa_wrap.Tensor(x_shape) |
| 127 | x.CopyFloatDataFromHostPtr([1, 2, 3, 4]) |
| 128 | |
| 129 | dy_shape = [2, 2] |
| 130 | dy = singa_wrap.Tensor(dy_shape) |
| 131 | dy.CopyFloatDataFromHostPtr([4, 3, 2, 1]) |
| 132 | |
| 133 | scale_shape = [2] |
| 134 | scale = singa_wrap.Tensor(scale_shape) |
| 135 | scale.CopyFloatDataFromHostPtr([1, 1]) |
| 136 | |
| 137 | bias_shape = [2] |
| 138 | bias = singa_wrap.Tensor(bias_shape) |
| 139 | bias.CopyFloatDataFromHostPtr([0, 0]) |
| 140 | |
| 141 | mean_shape = [2] |
| 142 | mean = singa_wrap.Tensor(mean_shape) |
| 143 | mean.CopyFloatDataFromHostPtr([1, 2]) |
| 144 | var = singa_wrap.Tensor(mean_shape) |
| 145 | var.CopyFloatDataFromHostPtr([1, 2]) |
| 146 | |
| 147 | handle = singa_wrap.BatchNormHandle(0.9, x) |
| 148 | |
| 149 | # 2D Forward Inference |
| 150 | y = singa_wrap.CpuBatchNormForwardInference(handle, x, scale, bias, |
| 151 | mean, var) |
| 152 | self.assertListEqual([2, 2], list(y.shape())) |
| 153 | |
| 154 | # 2D Forward Training |
| 155 | (y, mean_updated, var_updated) = singa_wrap.CpuBatchNormForwardTraining( |
| 156 | handle, x, scale, bias, mean, var) |
| 157 | self.assertListEqual([2, 2], list(y.shape())) |
| 158 | self.assertListEqual([2], list(mean_updated.shape())) |
| 159 | self.assertListEqual([2], list(var_updated.shape())) |
| 160 | |
| 161 | # 2D Backward dx |
| 162 | (dx, dscale, |
| 163 | dbias) = singa_wrap.CpuBatchNormBackwardx(handle, y, dy, x, scale, |
| 164 | bias, mean_updated, |
| 165 | var_updated) |
| 166 | self.assertListEqual([2, 2], list(dx.shape())) |
| 167 | self.assertListEqual([2], list(dscale.shape())) |
| 168 | self.assertListEqual([2], list(dbias.shape())) |
| 169 | |
| 170 | # 4D Forward Inference |
| 171 | |
| 172 | x2_shape = [1, 2, 4, 4] |
| 173 | x2 = singa_wrap.Tensor(x2_shape) |
| 174 | x2.CopyFloatDataFromHostPtr([ |
| 175 | 0.0736655, 0.0459045, 0.0779517, 0.0771059, 0.0586862, 0.0561263, |
| 176 | 0.0708457, 0.0977273, 0.0405025, -0.170897, 0.0208982, 0.136865, |
| 177 | -0.0367905, -0.0618205, -0.0103908, -0.0522777, -0.122161, |
| 178 | -0.025427, -0.0718576, -0.185941, 0.0166533, 0.178679, -0.0576606, |
| 179 | -0.137817, 0.150676, 0.153442, -0.0929899, -0.148675, -0.112459, |
| 180 | -0.106284, -0.103074, -0.0668811 |
| 181 | ]) |
nothing calls this directly
no test coverage detected