提交 734a9eea 编写于 作者: L Liu Yiqun

Correct the definition of Operator in TestFCGradOp, and rename the output name

of identity to Y.
上级 3285b00d
...@@ -24,30 +24,30 @@ class FCOp : public NetOp { ...@@ -24,30 +24,30 @@ class FCOp : public NetOp {
const framework::VariableNameMap &outputs, const framework::VariableNameMap &outputs,
const framework::AttributeMap &attrs) const framework::AttributeMap &attrs)
: NetOp(type, inputs, outputs, attrs) { : NetOp(type, inputs, outputs, attrs) {
// mul_out = X * W
AppendOp(framework::OpRegistry::CreateOp( AppendOp(framework::OpRegistry::CreateOp(
"mul", {{"X", {Input("X")}}, {"Y", {Input("W")}}}, "mul", {{"X", {Input("X")}}, {"Y", {Input("W")}}},
{{"Out", {Output("mul_out")}}}, {})); {{"Out", {Output("mul_out")}}}, {}));
std::string add_out_name = "mul_out";
auto b = Input("b"); auto b = Input("b");
if (b != framework::kEmptyVarName) { if (b != framework::kEmptyVarName) {
// add_out = mul_out + b
AppendOp(framework::OpRegistry::CreateOp( AppendOp(framework::OpRegistry::CreateOp(
"rowwise_add", {{"X", {Output("mul_out")}}, {"b", {Input("b")}}}, "rowwise_add", {{"X", {Output("mul_out")}}, {"b", {Input("b")}}},
{{"Out", {Output("add_out")}}}, {})); {{"Out", {Output("add_out")}}}, {}));
add_out_name = "add_out";
} else { } else {
AppendOp(framework::OpRegistry::CreateOp( auto add_out = Output("add_out");
"identity", {{"X", {Output("mul_out")}}}, if (add_out != framework::kEmptyVarName) {
{{"Out", {Output("add_out")}}}, {})); this->Rename(add_out, framework::kEmptyVarName);
}
} }
auto activation = GetAttr<std::string>("activation"); auto activation = GetAttr<std::string>("activation");
if (activation == "identity") { AppendOp(framework::OpRegistry::CreateOp(activation,
AppendOp(framework::OpRegistry::CreateOp(activation, {{"X", {Output(add_out_name)}}},
{{"X", {Output("add_out")}}}, {{"Y", {Output("Out")}}}, {}));
{{"Out", {Output("Out")}}}, {}));
} else {
AppendOp(framework::OpRegistry::CreateOp(activation,
{{"X", {Output("add_out")}}},
{{"Y", {Output("Out")}}}, {}));
}
CompleteAddOp(false); CompleteAddOp(false);
} }
}; };
......
...@@ -27,7 +27,7 @@ class IdentityOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -27,7 +27,7 @@ class IdentityOpMaker : public framework::OpProtoAndCheckerMaker {
framework::OpAttrChecker *op_checker) framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X", "input tensor of identity op"); AddInput("X", "input tensor of identity op");
AddOutput("Out", "output tensor of identity op"); AddOutput("Y", "output tensor of identity op");
AddComment("identity operator. Just a alias of scale op which scale = 1.0"); AddComment("identity operator. Just a alias of scale op which scale = 1.0");
} }
}; };
...@@ -40,7 +40,7 @@ class IdentityOp : public NetOp { ...@@ -40,7 +40,7 @@ class IdentityOp : public NetOp {
const framework::AttributeMap &attrs) const framework::AttributeMap &attrs)
: NetOp(type, inputs, outputs, attrs) { : NetOp(type, inputs, outputs, attrs) {
AppendOp(framework::OpRegistry::CreateOp( AppendOp(framework::OpRegistry::CreateOp(
"scale", {{"X", {Input("X")}}}, {{"Out", {Output("Out")}}}, "scale", {{"X", {Input("X")}}}, {{"Out", {Output("Y")}}},
{{"scale", static_cast<AttrType>(1)}})); {{"scale", static_cast<AttrType>(1)}}));
CompleteAddOp(false); CompleteAddOp(false);
} }
......
...@@ -65,7 +65,7 @@ class MinusGradOp : public NetOp { ...@@ -65,7 +65,7 @@ class MinusGradOp : public NetOp {
// x_grad = out_grad // x_grad = out_grad
AppendOp(framework::OpRegistry::CreateOp("identity", {{"X", {out_grad}}}, AppendOp(framework::OpRegistry::CreateOp("identity", {{"X", {out_grad}}},
{{"Out", {x_grad}}}, {})); {{"Y", {x_grad}}}, {}));
framework::AttributeMap scale_attr; framework::AttributeMap scale_attr;
scale_attr["scale"] = static_cast<AttrType>(-1); scale_attr["scale"] = static_cast<AttrType>(-1);
......
...@@ -18,6 +18,7 @@ py_test(test_gather_op SRCS test_gather_op.py) ...@@ -18,6 +18,7 @@ py_test(test_gather_op SRCS test_gather_op.py)
py_test(test_scatter_op SRCS test_scatter_op.py) py_test(test_scatter_op SRCS test_scatter_op.py)
py_test(test_fill_zeros_like_op SRCS test_fill_zeros_like_op.py) py_test(test_fill_zeros_like_op SRCS test_fill_zeros_like_op.py)
py_test(test_fc_op SRCS test_fc_op.py) py_test(test_fc_op SRCS test_fc_op.py)
py_test(test_minus_op SRCS test_minus_op.py)
py_test(gradient_checker SRCS gradient_checker.py) py_test(gradient_checker SRCS gradient_checker.py)
......
...@@ -277,10 +277,6 @@ class GradientChecker(unittest.TestCase): ...@@ -277,10 +277,6 @@ class GradientChecker(unittest.TestCase):
if no_grad_set is None: if no_grad_set is None:
no_grad_set = set() no_grad_set = set()
no_tmp_out = forward_op.no_intermediate_outputs()
if len(no_tmp_out) != 1:
raise ValueError("non temp out_names should be 1")
inputs = forward_op.inputs() inputs = forward_op.inputs()
in_names = [item for k in inputs for item in inputs[k]] in_names = [item for k in inputs for item in inputs[k]]
for no_grad in no_grad_set: for no_grad in no_grad_set:
......
...@@ -29,13 +29,20 @@ class TestFCOp(unittest.TestCase): ...@@ -29,13 +29,20 @@ class TestFCOp(unittest.TestCase):
class TestFCGradOp(GradientChecker): class TestFCGradOp(GradientChecker):
def test_normal(self): def test_normal(self):
self.inputs = { self.inputs = {
"X": np.random.random((4, 4)).astype("float32"), "X": np.random.random((32, 256)).astype("float32"),
"W": np.random.random((4, 4)).astype("float32"), "W": np.random.random((256, 100)).astype("float32"),
"b": np.random.random(4).astype("float32") "b": np.random.random(100).astype("float32")
} }
op = Operator( op = Operator(
"fc", X="X", W="W", b="b", Out="Out", activation="sigmoid") "fc",
#self.check_grad(op, self.inputs, ["X", "W", "b"], "Out") X="X",
W="W",
b="b",
Out="Out",
mul_out="mul_out",
add_out="add_out",
activation="sigmoid")
self.check_grad(op, self.inputs, ["X", "W", "b"], "Out")
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -4,7 +4,7 @@ from gradient_checker import GradientChecker, create_op ...@@ -4,7 +4,7 @@ from gradient_checker import GradientChecker, create_op
from op_test_util import OpTestMeta from op_test_util import OpTestMeta
class MinusOpTest(unittest.TestCase): class TestMinusOp(unittest.TestCase):
__metaclass__ = OpTestMeta __metaclass__ = OpTestMeta
def setUp(self): def setUp(self):
...@@ -16,7 +16,7 @@ class MinusOpTest(unittest.TestCase): ...@@ -16,7 +16,7 @@ class MinusOpTest(unittest.TestCase):
self.outputs = {'Out': (self.inputs['X'] - self.inputs['Y'])} self.outputs = {'Out': (self.inputs['X'] - self.inputs['Y'])}
class MinusGradTest(GradientChecker): class TestMinusGrad(GradientChecker):
def test_left(self): def test_left(self):
op = create_op("minus") op = create_op("minus")
inputs = { inputs = {
......
...@@ -11,14 +11,14 @@ class IdentityTest(unittest.TestCase): ...@@ -11,14 +11,14 @@ class IdentityTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.type = "identity" self.type = "identity"
self.inputs = {'X': np.random.random((32, 784)).astype("float32")} self.inputs = {'X': np.random.random((32, 784)).astype("float32")}
self.outputs = {'Out': self.inputs['X']} self.outputs = {'Y': self.inputs['X']}
class IdentityGradOpTest(GradientChecker): class IdentityGradOpTest(GradientChecker):
def test_normal(self): def test_normal(self):
op = create_op("identity") op = create_op("identity")
inputs = {"X": np.random.random((10, 10)).astype("float32")} inputs = {"X": np.random.random((10, 10)).astype("float32")}
self.check_grad(op, inputs, set("X"), "Out") self.check_grad(op, inputs, set("X"), "Y")
class ScaleTest(unittest.TestCase): class ScaleTest(unittest.TestCase):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册