diff --git a/python/paddle/fluid/tests/unittests/test_sparse_conv_op.py b/python/paddle/fluid/tests/unittests/test_sparse_conv_op.py index 623d1b57b3eaab4788c65f65ea8bec33e8ba69e1..e1a9b2428babc1b407ac90a37c37a2322302fd97 100644 --- a/python/paddle/fluid/tests/unittests/test_sparse_conv_op.py +++ b/python/paddle/fluid/tests/unittests/test_sparse_conv_op.py @@ -117,3 +117,44 @@ class TestSparseConv(unittest.TestCase): #Currently, only support data_format='NDHWC' conv3d = paddle.incubate.sparse.nn.SubmConv3D( 1, 1, (1, 3, 3), data_format='NCDHW') + + def test_Conv3D_bias(self): + with _test_eager_guard(): + paddle.seed(0) + shape = [1, 4, 4, 4, 3] + x = paddle.randn(shape) + sp_x = x.to_sparse_coo(4) + conv3d = paddle.nn.Conv3D(3, 2, 3, data_format='NDHWC') + + sp_conv3d = paddle.incubate.sparse.nn.Conv3D(3, + 2, + 3, + data_format='NDHWC') + sp_conv3d.weight.set_value( + paddle.to_tensor(conv3d.weight.numpy().transpose(2, 3, 4, 1, + 0))) + sp_conv3d.bias.set_value(paddle.to_tensor(conv3d.bias.numpy())) + + x.stop_gradient = False + out = conv3d(x) + loss = out.mean() + loss.backward() + + sp_x.stop_gradient = False + sp_out = sp_conv3d(sp_x) + dense_out = sp_out.to_dense() + sp_loss = dense_out.mean() + sp_loss.backward() + assert np.allclose(out.numpy(), + dense_out.numpy(), + atol=1e-3, + rtol=1e-3) + assert np.allclose(conv3d.weight.grad.numpy().transpose( + 2, 3, 4, 1, 0), + sp_conv3d.weight.grad.numpy(), + atol=1e-3, + rtol=1e-3) + assert np.allclose(conv3d.bias.grad.numpy(), + sp_conv3d.bias.grad.numpy(), + atol=1e-5, + rtol=1e-5) diff --git a/python/paddle/incubate/sparse/nn/layer/conv.py b/python/paddle/incubate/sparse/nn/layer/conv.py index 05309e5bbfe443582c5704520a3e1042b32f1968..74574ee61e3c29078f7a5e2539c252b9459d08f9 100644 --- a/python/paddle/incubate/sparse/nn/layer/conv.py +++ b/python/paddle/incubate/sparse/nn/layer/conv.py @@ -82,9 +82,9 @@ class _Conv3D(Layer): shape=filter_shape, attr=self._param_attr, default_initializer=_get_default_param_initializer()) - #self.bias = self.create_parameter( - # attr=self._bias_attr, shape=[self._out_channels], is_bias=True) - self.bias = None + self.bias = self.create_parameter(attr=self._bias_attr, + shape=[self._out_channels], + is_bias=True) def forward(self, x): out = F.conv._conv3d(x,