Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add backward to fully connected. (_backward_FullyConnected)
Browse files Browse the repository at this point in the history
  • Loading branch information
larroy committed May 21, 2019
1 parent 5854b98 commit bca77ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/operator/nn/fully_connected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ struct FullyConnectedGrad {
}
};

std::vector<nnvm::NodeEntry> FullyConnectedBackwardGrad(
const nnvm::NodePtr& n,
const std::vector<nnvm::NodeEntry>& ograds) {
std::vector<nnvm::NodeEntry> ret;
size_t i = 0;
for (const auto& x : n->inputs) {
std::ostringstream os;
os << n->attrs.name << "_backward_" << i;
ret.emplace_back(nnvm::NodeEntry{MakeNode("zeros_like", os.str(), {x}, nullptr, &n), 0, 0});
++i;
}
return ret;
}

inline static bool FCStorageType(const nnvm::NodeAttrs& attrs,
const int dev_mask,
DispatchMode* dispatch_mode,
Expand Down Expand Up @@ -325,6 +339,7 @@ NNVM_REGISTER_OP(_backward_FullyConnected)
.set_attr<nnvm::FInplaceOption>("FInplaceOption", [](const NodeAttrs& attrs){
return std::vector<std::pair<int, int> >{{1, 0}};
})
.set_attr<nnvm::FGradient>("FGradient", FullyConnectedBackwardGrad)
.set_attr<FInferStorageType>("FInferStorageType", BackwardFCStorageType)
.set_attr_parser(ParamParser<FullyConnectedParam>)
#if MXNET_USE_MKLDNN == 1
Expand Down
20 changes: 19 additions & 1 deletion tests/python/unittest/test_gluon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import mxnet as mx
from mxnet import gluon
from mxnet.gluon import nn
from mxnet.test_utils import assert_almost_equal
from mxnet.test_utils import assert_almost_equal, same
from mxnet.ndarray.ndarray import _STORAGE_TYPE_STR_TO_ID
from common import (setup_module, with_seed, assertRaises, teardown,
assert_raises_cudnn_not_satisfied)
Expand Down Expand Up @@ -915,6 +915,24 @@ def test_sequential_warning():
assert len(w) == 1


@with_seed()
def test_dense_backward():
import mxnet.autograd as ag
import mxnet.ndarray as nd
x = nd.array([[1,2,3,400]])
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Dense(1, in_units=x.shape[1]))
net.initialize(mx.initializer.Constant(.5))
params = [p.data() for p in net.collect_params().values()]
x.attach_grad()
with ag.record():
y = net.forward(x)
y_grad = ag.grad(y, x, create_graph=True, retain_graph=True)[0]
y_grad.backward()
same(x.grad, nd.zeros(4))


@with_seed()
def test_global_norm_clip():
stypes = ['default', 'row_sparse']
Expand Down

0 comments on commit bca77ba

Please sign in to comment.