Design of IfThenElse operator and layer
Created by: wangkuiyi
After talking with @zchen0211 and @Canpio , here is the very early draft of the idea of IfThenElse
operator and layer:
A Simple Example
The Python Application Program
cost = paddle.layer.mse(
paddel.layer.fc(
paddle.layer.data(name="features"),
output_size = 100),
paddle.layer.data(name="label"))
parameters = paddle.train(cost, minist_reader(), {"features":0, "label":1})
print paddle.inf(cost, parameters, inference_input_reader())
FC Layer as a Python Function
def fc(input):
output = paddle.framework.tensor()
W = paddle.framework.tensor(label="parameter")
b = paddle.framework.tensor(label="parameter")
paddle.operator.fc(input, W, b, output)
return output
FC Operator as a C++ Class
class FC : public framework:Operator {...};
The IfThenElse Layer and Operator
Another Example Using IfThenElse Layer
def a_network(input):
A = paddle.layer.fc(input, parameter_name="A")
B = paddle.layer.fc(input, parameter_name="B")
return paddle.layer.if_then_else(
paddle.layer.less_than(A, B),
paddle.layer.fc(
paddle.layer.fc(
paddle.layer.fc
paddle.layer.fc(A, output_size = 100))),
paddle.layer.fc(
paddle.layer.fc(
paddle.layer.fc(B, output_size = 100))))
The IfThenElse Layer
def if_then_else(cond, left, right) :
ret = paddle.framework.tensor()
paddle.operator.if_then_else(cond, left, right, ret)
return ret
The IfThenElseOp C++ Class
class IfThenElseOp : public framework::Operator {
// Check that cond returns a minibatch of boolean values.
// Check that left and right outputs are of the same size.
};