You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/design/block.md
+48-48Lines changed: 48 additions & 48 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,96 +55,96 @@ Let us consolidate the discussion by presenting some examples.
55
55
The following C++ programs shows how blocks are used with the `if-else` structure:
56
56
57
57
```c++
58
+
namespacepd = paddle;
59
+
58
60
int x = 10;
59
-
int y = 20;
60
-
intout;
61
+
int y = 1;
62
+
int z = 10;
61
63
bool cond = false;
64
+
int o1, o2;
62
65
if (cond) {
63
66
int z = x + y;
64
-
out = softmax(z);
67
+
o1 = z;
68
+
o2 = pd::layer::softmax(z);
65
69
} else {
66
-
int z = fc(x);
67
-
out = z;
70
+
int d = pd::layer::fc(z);
71
+
o1 = d;
72
+
o2 = d+1;
68
73
}
74
+
69
75
```
70
76
71
77
An equivalent PaddlePaddle program from the design doc of the [IfElseOp operator](./if_else_op.md) is as follows:
72
78
73
79
```python
74
80
import paddle as pd
75
81
76
-
x = var(10)
77
-
y = var(20)
78
-
cond = var(false)
79
-
ie = pd.create_ifelseop(inputs=[x], output_num=1)
82
+
x = minibatch([10, 20, 30]) # shape=[None, 1]
83
+
y = var(1) # shape=[1], value=1
84
+
z = minibatch([10, 20, 30]) # shape=[None, 1]
85
+
cond = larger_than(x, 15) # [false, true, true]
86
+
87
+
ie = pd.ifelse()
80
88
with ie.true_block():
81
-
x = ie.inputs(true, 0)
82
-
z = operator.add(x, y)
83
-
ie.set_output(true, 0, operator.softmax(z))
89
+
d = pd.layer.add_scalar(x, y)
90
+
ie.output(d, pd.layer.softmax(d))
84
91
with ie.false_block():
85
-
x = ie.inputs(false, 0)
86
-
z = layer.fc(x)
87
-
ie.set_output(true, 0, operator.softmax(z))
88
-
out = b(cond)
92
+
d = pd.layer.fc(z)
93
+
ie.output(d, d+1)
94
+
o1, o2 = ie(cond)
89
95
```
90
96
91
-
In both examples, the left branch computes `softmax(x+y)` and the right branch computes `fc(x)`.
97
+
In both examples, the left branch computes `x+y` and `softmax(x+y)`, the right branch computes`x+1` and`fc(x)`.
92
98
93
99
A difference is that variables in the C++ program contain scalar values, whereas those in the PaddlePaddle programs are mini-batches of instances. The `ie.input(true, 0)` invocation returns instances in the 0-th input, `x`, that corresponds to true values in `cond` as the local variable `x`, where `ie.input(false, 0)` returns instances corresponding to false values.
94
100
101
+
95
102
### Blocks with `for` and `RNNOp`
96
103
97
104
The following RNN model from the [RNN design doc](./rnn.md)
98
105
99
106
```python
100
-
x = sequence([10, 20, 30])
101
-
m = var(0)
102
-
W = tensor()
103
-
U = tensor()
104
-
105
-
rnn = create_rnn(inputs=[input])
106
-
with rnn.stepnet() as net:
107
-
x = net.set_inputs(0)
108
-
h = net.add_memory(init=m)
109
-
fc_out = pd.matmul(W, x)
110
-
hidden_out = pd.matmul(U, h.pre(n=1))
111
-
sum= pd.add_two(fc_out, hidden_out)
112
-
act = pd.sigmoid(sum)
113
-
h.update(act) # update memory with act
114
-
net.set_outputs(0, act, hidden_out) # two outputs
115
-
107
+
x = sequence([10, 20, 30]) # shape=[None, 1]
108
+
m = var(0) # shape=[1]
109
+
W = var(0.314, param=true) # shape=[1]
110
+
U = var(0.375, param=true) # shape=[1]
111
+
112
+
rnn = pd.rnn()
113
+
with rnn.step():
114
+
h = rnn.memory(init= m)
115
+
hh = rnn.previous_memory(h)
116
+
a = layer.fc(W, x)
117
+
b = layer.fc(U, hh)
118
+
s = pd.add(a, b)
119
+
act = pd.sigmoid(s)
120
+
rnn.update_memory(h, act)
121
+
rnn.output(a, b)
116
122
o1, o2 = rnn()
117
-
print o1, o2
118
123
```
119
-
120
124
has its equivalent C++ program as follows
121
125
122
126
```c++
123
127
int* x = {10, 20, 30};
124
-
int m = 0;
125
-
int W = some_value();
126
-
int U = some_other_value();
128
+
int* m = {0};
129
+
int* W = {0.314};
130
+
int* U = {0.375};
127
131
128
132
int mem[sizeof(x) / sizeof(x[0]) + 1];
129
133
int o1[sizeof(x) / sizeof(x[0]) + 1];
130
134
int o2[sizeof(x) / sizeof(x[0]) + 1];
131
135
for (int i = 1; i <= sizeof(x)/sizeof(x[0]); ++i) {
132
136
int x = x[i-1];
133
137
if (i == 1) mem[0] = m;
134
-
int fc_out = W * x;
135
-
int hidden_out = Y * mem[i-1];
136
-
int sum = fc_out + hidden_out;
138
+
int a = W * x;
139
+
int b = Y * mem[i-1];
140
+
int s = fc_out + hidden_out;
137
141
int act = sigmoid(sum);
138
142
mem[i] = act;
139
143
o1[i] = act;
140
144
o2[i] = hidden_out;
141
145
}
142
-
143
-
print_array(o1);
144
-
print_array(o2);
145
146
```
146
147
147
-
148
148
## Compilation and Execution
149
149
150
150
Like TensorFlow programs, a PaddlePaddle program is written in Python. The first part describes a neural network as a protobuf message, and the rest part executes the message for training or inference.
@@ -210,11 +210,11 @@ a = pd.Varaible(shape=[20, 20])
0 commit comments