Skip to content

Commit dcfbbd3

Browse files
committed
Merge branch 'develop' into crf
2 parents 735737d + c3b46d1 commit dcfbbd3

File tree

209 files changed

+7033
-1915
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+7033
-1915
lines changed

cmake/configure.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ if(NOT WITH_GOLANG)
4949
endif(NOT WITH_GOLANG)
5050

5151
if(NOT WITH_GPU)
52-
add_definitions(-DPADDLE_ONLY_CPU)
5352
add_definitions(-DHPPL_STUB_FUNC)
5453

5554
list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS cu)
5655
else()
56+
add_definitions(-DPADDLE_WITH_CUDA)
57+
5758
FIND_PACKAGE(CUDA REQUIRED)
5859

5960
if(${CUDA_VERSION_MAJOR} VERSION_LESS 7)

doc/api/v1/index_cn.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Model Config API
2121
trainer_config_helpers/optimizers.rst
2222
trainer_config_helpers/data_sources.rst
2323
trainer_config_helpers/layers.rst
24-
trainer_config_helpers/activations.rst
24+
trainer_config_helpers/activations.rst
2525
trainer_config_helpers/poolings.rst
2626
trainer_config_helpers/networks.rst
2727
trainer_config_helpers/evaluators.rst

doc/api/v2/config/layer.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ clip
345345
.. autoclass:: paddle.v2.layer.clip
346346
:noindex:
347347

348+
resize
349+
------
350+
.. autoclass:: paddle.v2.layer.resize
351+
:noindex:
352+
348353
slope_intercept
349354
---------------
350355
.. autoclass:: paddle.v2.layer.slope_intercept

doc/design/block.md

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,96 +55,96 @@ Let us consolidate the discussion by presenting some examples.
5555
The following C++ programs shows how blocks are used with the `if-else` structure:
5656

5757
```c++
58+
namespace pd = paddle;
59+
5860
int x = 10;
59-
int y = 20;
60-
int out;
61+
int y = 1;
62+
int z = 10;
6163
bool cond = false;
64+
int o1, o2;
6265
if (cond) {
6366
int z = x + y;
64-
out = softmax(z);
67+
o1 = z;
68+
o2 = pd::layer::softmax(z);
6569
} else {
66-
int z = fc(x);
67-
out = z;
70+
int d = pd::layer::fc(z);
71+
o1 = d;
72+
o2 = d+1;
6873
}
74+
6975
```
7076
7177
An equivalent PaddlePaddle program from the design doc of the [IfElseOp operator](./if_else_op.md) is as follows:
7278
7379
```python
7480
import paddle as pd
7581
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()
8088
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))
8491
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)
8995
```
9096

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)`.
9298

9399
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.
94100

101+
95102
### Blocks with `for` and `RNNOp`
96103

97104
The following RNN model from the [RNN design doc](./rnn.md)
98105

99106
```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)
116122
o1, o2 = rnn()
117-
print o1, o2
118123
```
119-
120124
has its equivalent C++ program as follows
121125

122126
```c++
123127
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};
127131

128132
int mem[sizeof(x) / sizeof(x[0]) + 1];
129133
int o1[sizeof(x) / sizeof(x[0]) + 1];
130134
int o2[sizeof(x) / sizeof(x[0]) + 1];
131135
for (int i = 1; i <= sizeof(x)/sizeof(x[0]); ++i) {
132136
int x = x[i-1];
133137
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;
137141
int act = sigmoid(sum);
138142
mem[i] = act;
139143
o1[i] = act;
140144
o2[i] = hidden_out;
141145
}
142-
143-
print_array(o1);
144-
print_array(o2);
145146
```
146147

147-
148148
## Compilation and Execution
149149

150150
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])
210210
b = pd.fc(a, params=["fc.w", "fc.b"])
211211

212212
rnn = pd.create_rnn()
213-
with rnn.stepnet() as net:
214-
x = net.set_inputs(a)
213+
with rnn.stepnet()
214+
x = a.as_step_input()
215215
# reuse fc's parameter
216216
fc_without_b = pd.get_variable("fc.w")
217-
net.set_outputs(fc_without_b)
217+
rnn.output(fc_without_b)
218218

219219
out = rnn()
220220
```

doc/design/if_else_op.md

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
1-
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 N instances. If cond[i] == True, input instance input[i] will go through true_block() and generate output[i]; otherwise it will produce output from false_bloack().
1+
# The `IfElse` Operator
22

3-
```python
4-
import paddle as pd
3+
PaddlePaddle's `IfElse` operator differs from TensorFlow's:
54

6-
x = var()
7-
y = var()
8-
cond = var()
9-
default_value = var()
10-
b = pd.create_ifelseop(inputs=[x], output_num=1)
11-
with b.true_block():
12-
x = b.inputs(0)
13-
z = operator.add(x, y)
14-
b.set_output(0, operator.softmax(z))
15-
16-
with b.false_block():
17-
x = b.inputs(0)
18-
z = layer.fc(x)
19-
b.set_output(0, operator.softmax(z))
20-
21-
out = b(cond)
22-
```
5+
- the TensorFlow version takes a scalar boolean value as the condition so that the whole mini-batch goes to either the true or the false branch, whereas
6+
- the PaddlePaddle version takes a vector of boolean value as the condition, and instances corresponding to true values go to the true branch, those corresponding to false values go to the false branch.
7+
8+
## Example
9+
10+
The following PaddlePaddle program shows the usage of the IfElse operator:
2311

24-
If only true_block is set in an IfElseOp, a special case is that we can have a default value for false as:
2512
```python
2613
import paddle as pd
2714

28-
x = var()
29-
y = var()
30-
cond = var()
31-
default_value = var()
32-
b = pd.create_ifelseop(inputs=[x], output_num=1, default_value)
33-
34-
with b.true_block():
35-
x = b.inputs(0)
36-
z = operator.add(x, y)
37-
b.set_output(0, operator.softmax(z))
15+
x = minibatch([10, 20, 30]) # shape=[None, 1]
16+
y = var(1) # shape=[1], value=1
17+
z = minibatch([10, 20, 30]) # shape=[None, 1]
18+
cond = larger_than(x, 15) # [false, true, true]
19+
20+
ie = pd.ifelse()
21+
with ie.true_block():
22+
d = pd.layer.add(x, y)
23+
ie.output(d, pd.layer.softmax(d))
24+
with ie.false_block():
25+
d = pd.layer.fc(z)
26+
ie.output(d, d+1)
27+
o1, o2 = ie(cond)
28+
```
3829

39-
out = b(cond)
30+
A challenge to implement the `IfElse` operator is to infer those variables to be split, or, say, to identify the variable of the mini-batch or those derived from the mini-batch.
31+
32+
An equivalent C++ program is as follows:
33+
34+
```c++
35+
namespace pd = paddle;
36+
37+
int x = 10;
38+
int y = 1;
39+
int z = 10;
40+
bool cond = false;
41+
int o1, o2;
42+
if (cond) {
43+
int d = x + y;
44+
o1 = z;
45+
o2 = pd::layer::softmax(z);
46+
} else {
47+
int d = pd::layer::fc(z);
48+
o1 = d;
49+
o2 = d+1;
50+
}
4051
```
41-
where default_value is a list of vars for `cond` == False.

0 commit comments

Comments
 (0)