Skip to content

Commit b156c6a

Browse files
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into hsigmoid_gpu
2 parents ab1af66 + 985e4ab commit b156c6a

18 files changed

+176
-72
lines changed

paddle/operators/conv_transpose_op.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ Conv2DTransposeOpMaker::Conv2DTransposeOpMaker(
7474
"The format of output tensor is also NCHW.");
7575
AddAttr<std::vector<int>>(
7676
"strides",
77-
"(vector<int> defalut:{1, 1}), the strides(h_stride, w_stride) of "
77+
"(vector<int> default:{1, 1}), the strides(h_stride, w_stride) of "
7878
"convolution transpose operator.")
7979
.SetDefault({1, 1});
8080
AddAttr<std::vector<int>>(
8181
"paddings",
82-
"(vector<int> defalut:{0, 0}), the paddings(h_pad, w_pad) of convolution "
82+
"(vector<int> default:{0, 0}), the paddings(h_pad, w_pad) of convolution "
8383
"transpose operator.")
8484
.SetDefault({0, 0});
8585
AddComment(R"DOC(
@@ -101,8 +101,8 @@ The input(X) size and output(Out) size may be different.
101101
Output:
102102
Output shape: (N, C_out, H_out, W_out)
103103
where
104-
H_out = (H_in - 1) * strides[0] - 2 * paddings[0] + filter_size[0];
105-
W_out = (W_in - 1) * strides[1] - 2 * paddings[1] + filter_size[1];
104+
H_out = (H_in - 1) * strides[0] - 2 * paddings[0] + H_f;
105+
W_out = (W_in - 1) * strides[1] - 2 * paddings[1] + W_f;
106106
)DOC");
107107
}
108108

@@ -130,12 +130,12 @@ Conv3DTransposeOpMaker::Conv3DTransposeOpMaker(
130130
"the number of channels, D is the depth of the feature, H is the "
131131
"height of the feature, and W is the width of the feature.");
132132
AddAttr<std::vector<int>>("strides",
133-
"(vector<int> defalut:{1, 1, 1}), the "
133+
"(vector<int> default:{1, 1, 1}), the "
134134
"strides{d_stride, h_stride, w_stride} of "
135135
"convolution transpose operator.")
136136
.SetDefault({1, 1, 1});
137137
AddAttr<std::vector<int>>("paddings",
138-
"(vector<int> defalut:{0, 0, 0}), paddings(d_pad, "
138+
"(vector<int> default:{0, 0, 0}), paddings(d_pad, "
139139
"h_pad, w_pad) of convolution transpose operator.")
140140
.SetDefault({0, 0, 0});
141141
AddComment(R"DOC(
@@ -158,9 +158,9 @@ The input(X) size and output(Out) size may be different.
158158
Output:
159159
Output shape: (N, C_out, D_out, H_out, W_out)
160160
where
161-
D_out = (D_in - 1) * strides[0] - 2 * paddings[0] + filter_size[0];
162-
H_out = (H_in - 1) * strides[1] - 2 * paddings[1] + filter_size[1];
163-
W_out = (W_in - 1) * strides[2] - 2 * paddings[2] + filter_size[2];
161+
D_out = (D_in - 1) * strides[0] - 2 * paddings[0] + D_f;
162+
H_out = (H_in - 1) * strides[1] - 2 * paddings[1] + H_f;
163+
W_out = (W_in - 1) * strides[2] - 2 * paddings[2] + W_f;
164164
)DOC");
165165
}
166166

paddle/operators/detail/send_recv.proto

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ syntax = "proto3";
1717
package sendrecv;
1818

1919
service SendRecvService {
20-
// For parameter server round-robin like hashing, do not split tensors.
20+
// For parameter server round-robin like hashing, do not split tensors.
2121
// Send and recv only one tensor
2222
rpc SendVariable(VariableMessage) returns (VariableMessage) {}
2323
}
@@ -32,6 +32,4 @@ message VariableMessage {
3232
bytes serialized = 2;
3333
}
3434

35-
message VoidMessage {
36-
37-
}
35+
message VoidMessage {}

python/paddle/v2/fluid/evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Evaluator(object):
2626
name(str): The name of evaluator. such as, "accuracy". Used for generate
2727
temporary variable name.
2828
main_program(Program, optional): The evaluator should be added to this
29-
main_program. Default g_main_program
29+
main_program. Default default_main_program()
3030
startup_program(Program, optional):The parameter should be added to this
31-
startup_program. Default g_startup_program
31+
startup_program. Default default_startup_program()
3232
3333
Attributes:
3434
states(list): The list of state variables. states will be reset to zero

python/paddle/v2/fluid/executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22
from . import core
3-
from framework import Program, g_main_program
3+
from framework import Program, default_main_program
44

55
__all__ = ['Executor', 'g_scope']
66

@@ -103,7 +103,7 @@ def run(self,
103103
fetch_list = []
104104

105105
if program is None:
106-
program = g_main_program
106+
program = default_main_program()
107107

108108
if not isinstance(program, Program):
109109
raise TypeError()

python/paddle/v2/fluid/framework.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__all__ = [
88
'Block', 'Variable', 'Program', 'Operator', 'default_startup_program',
9-
'default_main_program', 'g_startup_program', 'g_main_program'
9+
'default_main_program'
1010
]
1111

1212

@@ -654,13 +654,13 @@ def __init__(self, block, shape, dtype, **kwargs):
654654

655655

656656
# program is a global instance.
657-
g_main_program = Program()
658-
g_startup_program = Program()
657+
_main_program_ = Program()
658+
_startup_program_ = Program()
659659

660660

661661
def default_startup_program():
662-
return g_startup_program
662+
return _startup_program_
663663

664664

665665
def default_main_program():
666-
return g_main_program
666+
return _main_program_

python/paddle/v2/fluid/io.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import os
22
import cPickle as pickle
33

4-
from paddle.v2.fluid.framework import Program, Parameter, g_main_program, \
5-
Variable
4+
from paddle.v2.fluid.framework import Program, Parameter, default_main_program, Variable
65

76
__all__ = [
87
'save_vars', 'save_params', 'save_persistables', 'load_vars', 'load_params',
@@ -46,7 +45,7 @@ def save_vars(executor, dirname, main_program=None, vars=None, predicate=None):
4645
"""
4746
if vars is None:
4847
if main_program is None:
49-
main_program = g_main_program
48+
main_program = default_main_program()
5049
if not isinstance(main_program, Program):
5150
raise TypeError("program should be as Program type or None")
5251

@@ -98,7 +97,7 @@ def load_vars(executor, dirname, main_program=None, vars=None, predicate=None):
9897
:param executor: executor that save variable
9998
:param dirname: directory path
10099
:param main_program: program. If vars is None, then filter all variables in this
101-
program which fit `predicate`. Default g_program.
100+
program which fit `predicate`. Default default_main_program().
102101
:param predicate: The Predicate describes a callable that returns a variable
103102
as a bool. If it returns true, the variables will be loaded.
104103
:param vars: variables need to be loaded. If specify vars, program &
@@ -107,7 +106,7 @@ def load_vars(executor, dirname, main_program=None, vars=None, predicate=None):
107106
"""
108107
if vars is None:
109108
if main_program is None:
110-
main_program = g_main_program
109+
main_program = default_main_program()
111110
if not isinstance(main_program, Program):
112111
raise TypeError("program's type should be Program")
113112

@@ -154,7 +153,7 @@ def load_persistables(executor, dirname, main_program=None):
154153

155154
def get_inference_program(target_vars, main_program=None):
156155
if main_program is None:
157-
main_program = g_main_program
156+
main_program = default_main_program()
158157
if not isinstance(target_vars, list):
159158
target_vars = [target_vars]
160159

@@ -177,12 +176,12 @@ def save_inference_model(dirname,
177176
:param target_vars: Variables from which we can get inference results.
178177
:param executor: executor that save inference model
179178
:param main_program: original program, which will be pruned to build the inference model.
180-
Default g_main_program.
179+
Default default_main_program().
181180
182181
:return: None
183182
"""
184183
if main_program is None:
185-
main_program = g_main_program
184+
main_program = default_main_program()
186185
if not isinstance(target_vars, list):
187186
target_vars = [target_vars]
188187

@@ -272,10 +271,10 @@ def get_parameter_value_by_name(name, executor, program=None):
272271
:param executor: executor for retrieving the value
273272
:param name: the name of the parameter
274273
:param program: the program where the variable is found
275-
Default g_main_program.
274+
Default default_main_program().
276275
:return: the LoDTensor for the variable
277276
"""
278277
if program is None:
279-
program = g_main_program
278+
program = default_main_program()
280279
var = program.global_block().var(name)
281280
return get_parameter_value(var, executor)

python/paddle/v2/fluid/layer_helper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import copy
22
import itertools
33

4-
from framework import Variable, g_main_program, \
5-
g_startup_program, unique_name, dtype_is_floating
4+
from framework import Variable, default_main_program, default_startup_program, unique_name, dtype_is_floating
65
from paddle.v2.fluid.initializer import Constant, Xavier
76

87

@@ -22,15 +21,15 @@ def name(self):
2221
def main_program(self):
2322
prog = self.kwargs.get('main_program', None)
2423
if prog is None:
25-
return g_main_program
24+
return default_main_program()
2625
else:
2726
return prog
2827

2928
@property
3029
def startup_program(self):
3130
prog = self.kwargs.get('startup_program', None)
3231
if prog is None:
33-
return g_startup_program
32+
return default_startup_program()
3433
else:
3534
return prog
3635

python/paddle/v2/fluid/layers.py

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from . import core
1+
import core
22
import proto.framework_pb2 as framework_pb2
33
from framework import OpProtoHolder, Variable, Program, Operator
4-
from initializer import Constant, Normal, Xavier
4+
from initializer import Constant, Normal, Xavier, Initializer
55
from paddle.v2.fluid.layer_helper import LayerHelper, unique_name
66
import re
77
import cStringIO
@@ -1587,6 +1587,97 @@ def array_length(array, main_program=None):
15871587
return tmp
15881588

15891589

1590+
def conv2d_transpose(input,
1591+
num_filters,
1592+
output_size=None,
1593+
filter_size=None,
1594+
padding=None,
1595+
stride=None,
1596+
param_attr=None,
1597+
param_initializer=None,
1598+
main_program=None,
1599+
startup_program=None):
1600+
"""
1601+
The transpose of conv2d layer.
1602+
1603+
This layer is also known as deconvolution layer.
1604+
1605+
Args:
1606+
input(Variable): The input image with [N, C, H, W] format.
1607+
num_filters(int): The number of filter. It is as same as the output
1608+
image channel.
1609+
output_size(int|tuple|None): The output image size. If output size is a
1610+
tuple, it must contain two integers, (image_H, image_W). This
1611+
parameter only works when filter_size is None.
1612+
filter_size(int|tuple|None): The filter size. If filter_size is a tuple,
1613+
it must contain two integers, (filter_size_H, filter_size_W).
1614+
Otherwise, the filter will be a square. None if use output size to
1615+
calculate filter_size
1616+
padding(int|tuple): The padding size. If padding is a tuple, it must
1617+
contain two integers, (padding_H, padding_W). Otherwise, the
1618+
padding_H = padding_W = padding.
1619+
stride(int|tuple): The stride size. If stride is a tuple, it must
1620+
contain two integers, (stride_H, stride_W). Otherwise, the
1621+
stride_H = stride_W = stride.
1622+
param_attr: Parameter Attribute.
1623+
param_initializer(Initializer): Parameter Initializer. Default is Xavier
1624+
main_program(Program): the main program
1625+
startup_program(Program): the startup program
1626+
1627+
Returns:
1628+
Variable: Output image.
1629+
"""
1630+
helper = LayerHelper("conv2d_transpose", **locals())
1631+
if not isinstance(input, Variable):
1632+
raise TypeError("Input of conv2d_transpose must be Variable")
1633+
input_channel = input.shape[1]
1634+
1635+
op_attr = dict()
1636+
1637+
if isinstance(padding, int):
1638+
op_attr['paddings'] = [padding, padding]
1639+
elif padding is not None:
1640+
op_attr['paddings'] = padding
1641+
1642+
if isinstance(stride, int):
1643+
op_attr['strides'] = stride
1644+
elif stride is not None:
1645+
op_attr['strides'] = stride
1646+
1647+
if filter_size is None:
1648+
if output_size is None:
1649+
raise ValueError("output_size must be set when filter_size is None")
1650+
if isinstance(output_size, int):
1651+
output_size = [output_size, output_size]
1652+
1653+
padding = op_attr.get('paddings', [0, 0])
1654+
stride = op_attr.get('strides', [1, 1])
1655+
1656+
h_in = input.shape[2]
1657+
w_in = input.shape[3]
1658+
filter_size_h = output_size[0] - (h_in - 1) * stride[0] + 2 * padding[0]
1659+
filter_size_w = output_size[1] - (w_in - 1) * stride[1] + 2 * padding[1]
1660+
filter_size = [filter_size_h, filter_size_w]
1661+
elif isinstance(filter_size, int):
1662+
filter_size = [filter_size, filter_size]
1663+
1664+
filter_shape = [input_channel, num_filters] + filter_size
1665+
img_filter = helper.create_parameter(
1666+
dtype=input.dtype,
1667+
shape=filter_shape,
1668+
attr=helper.param_attr,
1669+
initializer=param_initializer)
1670+
1671+
out = helper.create_tmp_variable(dtype=input.dtype)
1672+
helper.append_op(
1673+
type='conv2d_transpose',
1674+
inputs={'Input': [input],
1675+
'Filter': [img_filter]},
1676+
outputs={'Output': out},
1677+
attrs=op_attr)
1678+
return out
1679+
1680+
15901681
class ConditionalBlockGuard(BlockGuard):
15911682
def __init__(self, block):
15921683
if not isinstance(block, ConditionalBlock):

python/paddle/v2/fluid/tests/test_array_read_write_op.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import paddle.v2.fluid.layers as layers
44
from paddle.v2.fluid.executor import Executor
55
from paddle.v2.fluid.backward import append_backward_ops
6-
from paddle.v2.fluid.framework import g_main_program
6+
from paddle.v2.fluid.framework import default_main_program
77
import numpy
88

99

@@ -66,7 +66,7 @@ def test_read_write(self):
6666

6767
append_backward_ops(total_sum_scaled)
6868

69-
g_vars = map(g_main_program.global_block().var,
69+
g_vars = map(default_main_program().global_block().var,
7070
[each_x.name + "@GRAD" for each_x in x])
7171
g_out = [
7272
item.sum()

python/paddle/v2/fluid/tests/test_conditional_block.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
import paddle.v2.fluid.layers as layers
33
import paddle.v2.fluid.core as core
4-
from paddle.v2.fluid.framework import g_startup_program, g_main_program
4+
from paddle.v2.fluid.framework import default_startup_program, default_main_program
55
from paddle.v2.fluid.executor import Executor
66
from paddle.v2.fluid.backward import append_backward_ops
77
import numpy
@@ -19,7 +19,7 @@ def test_forward(self):
1919

2020
cpu = core.CPUPlace()
2121
exe = Executor(cpu)
22-
exe.run(g_startup_program)
22+
exe.run(default_startup_program())
2323

2424
x = numpy.random.random(size=(10, 1)).astype('float32')
2525

@@ -29,7 +29,9 @@ def test_forward(self):
2929
append_backward_ops(loss=loss)
3030
outs = exe.run(
3131
feed={'X': x},
32-
fetch_list=[g_main_program.block(0).var(data.name + "@GRAD")])[0]
32+
fetch_list=[
33+
default_main_program().block(0).var(data.name + "@GRAD")
34+
])[0]
3335
print outs
3436

3537

0 commit comments

Comments
 (0)