提交 d874fca4 编写于 作者: L Liu Yiqun

Support multiple inputs in FCOp.

上级 c05d319c
...@@ -24,30 +24,49 @@ class FCOp : public NetOp { ...@@ -24,30 +24,49 @@ 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 auto x = Inputs("X");
auto w = Inputs("W");
PADDLE_ENFORCE_EQ(
x.size(), w.size(),
"The size of inputs X(%d) should be the same as that of weights W(%d).",
x.size(), w.size());
int n = x.size();
PADDLE_ENFORCE_GE(n, 1,
"The size of inputs X(%d) should be no less than 1.", n);
// mul_out = X[0] * W[0] + ... + X[n-1] * W[n-1]
AppendOp(
framework::OpRegistry::CreateOp("mul", {{"X", {x[0]}}, {"W", {w[0]}}},
{{"Out", {Output("mul_out")}}}, {}));
for (int i = 1; i < n; i++) {
// mul_out = mul_out + X[i] * W[i]
AppendOp(
framework::OpRegistry::CreateOp("mul", {{"X", {x[i]}}, {"Y", {w[i]}}},
{{"Out", {Output("add_out")}}}, {}));
AppendOp(framework::OpRegistry::CreateOp( AppendOp(framework::OpRegistry::CreateOp(
"mul", {{"X", {Input("X")}}, {"Y", {Input("W")}}}, "add", {{"X", {Output("mul_out")}}, {"Y", {Output("add_out")}}},
{{"Out", {Output("mul_out")}}}, {})); {{"Out", {Output("mul_out")}}}, {}));
}
std::string add_out_name = "mul_out";
auto b = Input("b"); auto b = Input("b");
std::string add_out = "mul_out";
if (b != framework::kEmptyVarName) { if (b != framework::kEmptyVarName) {
// add_out = mul_out + b // 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"; add_out = "add_out";
} else { } else {
auto add_out = Output("add_out"); if (Output("add_out") != framework::kEmptyVarName) {
if (add_out != framework::kEmptyVarName) { this->Rename(Output("add_out"), framework::kEmptyVarName);
this->Rename(add_out, framework::kEmptyVarName);
} }
} }
auto activation = GetAttr<std::string>("activation"); auto activation = Attr<std::string>("activation");
AppendOp(framework::OpRegistry::CreateOp(activation, AppendOp(framework::OpRegistry::CreateOp(
{{"X", {Output(add_out_name)}}}, activation, {{"X", {Output(add_out)}}}, {{"Y", {Output("Y")}}}, {}));
{{"Y", {Output("Out")}}}, {}));
CompleteAddOp(false); CompleteAddOp(false);
} }
}; };
...@@ -56,11 +75,11 @@ class FCOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -56,11 +75,11 @@ class FCOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
FCOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) FCOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X", "The 2D input matrix of FC operator."); AddInput("X", "The 2-D input matrix of FC operator.").AsDuplicable();
AddInput("W", "The 2D weight matrix of FC operator."); AddInput("W", "The 2-D weight matrix of FC operator.").AsDuplicable();
AddInput("b", "The 1D bias vector of FC operator"); AddInput("b", "The 1-D bias vector of FC operator");
AddOutput("Out", "The activated output matrix of FC operator"); AddOutput("Y", "The activated output matrix of FC operator");
AddOutput("mul_out", "The non-actived output of FC operator, X * W") AddOutput("mul_out", "The non-actived output of FC operator, X * W")
.AsIntermediate(); .AsIntermediate();
AddOutput("add_out", "The non-actived output of FC operator, X * W + b") AddOutput("add_out", "The non-actived output of FC operator, X * W + b")
...@@ -78,7 +97,7 @@ learned weights with a matrix multiplication followed by a bias offset ...@@ -78,7 +97,7 @@ learned weights with a matrix multiplication followed by a bias offset
(optionally). (optionally).
Equation: Equation:
Out = Act(sum_n{X_i * W_i} + b) Y = Act(sum_n{X_i * W_i} + b)
where X_i is a 2D matrix of size (M x K), usually M is the minibatch size and where X_i is a 2D matrix of size (M x K), usually M is the minibatch size and
K is the number of features. W_i is also a 2D matrix of size (K x N), K is the number of features. W_i is also a 2D matrix of size (K x N),
......
import unittest import unittest
import numpy as np import numpy as np
from gradient_checker import GradientChecker, create_op from op_test import OpTest
from op_test_util import OpTestMeta import paddle.v2.framework.core as core
from paddle.v2.framework.op import Operator from paddle.v2.framework.op import Operator
class TestFCOp(unittest.TestCase): class TestFCOp(OpTest):
__metaclass__ = OpTestMeta
def setUp(self): def setUp(self):
self.type = "fc" print "Run"
self.op_type = "fc"
x0 = np.random.random((32, 256)).astype("float32")
x1 = np.random.random((32, 256)).astype("float32")
w0 = np.random.random((256, 100)).astype("float32")
w1 = np.random.random((256, 100)).astype("float32")
b = np.random.random(100).astype("float32")
self.inputs = { self.inputs = {
"X": np.random.random((32, 784)).astype("float32"), "X": {
"W": np.random.random((784, 1000)).astype("float32"), "X0": x0,
"b": np.random.random(1000).astype("float32") "X1": x1
},
"W": {
"W0": w0,
"W1": w1
},
"b": b
} }
self.attrs = {"activation": "sigmoid"} #self.attrs = {"activation": "sigmoid"}
mul_out = np.dot(self.inputs["X"], self.inputs["W"]) mul_out = np.dot(x0, w0) + np.dot(x1, w1)
add_out = np.add(mul_out, self.inputs["b"]) add_out = np.add(mul_out, b)
sigmoid_out = 1 / (1 + np.exp(-add_out)) #sigmoid_out = 1 / (1 + np.exp(-add_out))
sigmoid_out = add_out
self.outputs = { self.outputs = {
"mul_out": mul_out, "mul_out": mul_out,
"add_out": add_out, "add_out": add_out,
"Out": sigmoid_out "Y": sigmoid_out
} }
def test_check_output(self):
self.check_output(core.CPUPlace())
self.check_output(core.GPUPlace(0))
class TestFCGradOp(GradientChecker): #def test_check_grad(self):
def test_normal(self): # self.check_grad(["X0", "X1", "W0", "W1", "b"], "Y")
self.inputs = {
"X": np.random.random((32, 256)).astype("float32"),
"W": np.random.random((256, 100)).astype("float32"),
"b": np.random.random(100).astype("float32")
}
op = Operator(
"fc",
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__':
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册