parent
d156dfa93b
commit
32244b528b
@ -1,54 +1,60 @@
|
|||||||
In an if_op, only inputs with condition satisfied will be run. The op could have multiple inputs and multiple outputs.
|
IfOp should have only one branch. An IfOp operator takes a `cond` variable whose value must be a vector of N boolean elements. Its return value has M (M<=N) instances, each corresponds to a true element in `cond`.
|
||||||
We should have the following design:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# A 1-d bool vector
|
import paddle as pd
|
||||||
cond = Var()
|
|
||||||
# create an op
|
x = var()
|
||||||
if = pd.if_op()
|
y = var()
|
||||||
|
cond = var()
|
||||||
with if.true_block() as block:
|
|
||||||
x1 = if.input(x1)
|
b = pd.create_ifop_builder(inputs=[x], output_num=1)
|
||||||
x2 = if.input(x2)
|
with b.true_block():
|
||||||
y = pd.add(x1, x2)
|
x = b.inputs(0)
|
||||||
y2 = pd.fc(x1) # contains (w,b)
|
z = operator.add(x, y)
|
||||||
if.output(y)
|
b.set_output(0, operator.softmax(z))
|
||||||
if.output(y2)
|
|
||||||
|
out = b(cond)
|
||||||
o1, o2 = if(cond)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
In an if_op, only inputs with condition satisfied will be run.
|
If we want the output still has N instances, we can use IfElseOp with a default value, whose minibatch size must be N:
|
||||||
We should have the following design:
|
|
||||||
```python
|
```python
|
||||||
# A 1-d bool vector
|
import paddle as pd
|
||||||
cond = Var()
|
|
||||||
# create an op
|
x = var()
|
||||||
if = pd.if_op()
|
y = var()
|
||||||
|
cond = var()
|
||||||
with if.true_block() as block:
|
default_value = var()
|
||||||
x1 = if.input(x1)
|
b = pd.create_ifelseop_builder(inputs=[x], output_num=1)
|
||||||
x2 = if.input(x2)
|
with b.true_block():
|
||||||
y = pd.add(x1, x2)
|
x = b.inputs(0)
|
||||||
y2 = pd.fc(x1) # contains (w,b)
|
z = operator.add(x, y)
|
||||||
if.output(y, name="y")
|
b.set_output(0, operator.softmax(z))
|
||||||
if.output(y2, name="y2")
|
|
||||||
|
with b.false_block():
|
||||||
with if.false_block() as block:
|
x = b.inputs(0)
|
||||||
x1 = if.input(x1)
|
z = layer.fc(x)
|
||||||
x2 = if.input(x2)
|
b.set_output(0, operator.softmax(z))
|
||||||
y = pd.fc(x2)
|
|
||||||
y2 = pd.softmax(x1)
|
out = b(cond)
|
||||||
if.output(y, name="y")
|
|
||||||
if.output(y2, name="y2")
|
|
||||||
|
|
||||||
o1, o2 = if(cond)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Some questions:
|
If only true_block is set in an IfElseOp, we can have a default value for false as:
|
||||||
1. how to know which inputs will be selected by condition?
|
```python
|
||||||
e.g. True_block():
|
import paddle as pd
|
||||||
y = pd.fc(x)
|
|
||||||
# we will have x, w, b all as inputs
|
x = var()
|
||||||
# but only x will be selected by cond, how can the block know?
|
y = var()
|
||||||
|
cond = var()
|
||||||
|
default_value = var()
|
||||||
|
b = pd.create_ifelseop_builder(inputs=[x], output_num=1, default_value)
|
||||||
|
|
||||||
|
with b.true_block():
|
||||||
|
x = b.inputs(0)
|
||||||
|
z = operator.add(x, y)
|
||||||
|
b.set_output(0, operator.softmax(z))
|
||||||
|
|
||||||
|
out = b(cond)
|
||||||
|
```
|
||||||
|
where default_value is a list of vars for `cond` == False.
|
||||||
|
|
||||||
|
Loading…
Reference in new issue