Skip to content

Commit 07205c7

Browse files
committed
fix elementwise add bug
1 parent 090372f commit 07205c7

4 files changed

Lines changed: 32 additions & 18 deletions

File tree

paddle/operators/elementwise_op.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class ElementwiseOp : public framework::OperatorWithKernel {
3434

3535
auto x_dim = ctx->GetInputDim("X");
3636
auto y_dim = ctx->GetInputDim("Y");
37-
LOG(INFO) << x_dim;
38-
LOG(INFO) << y_dim;
3937
PADDLE_ENFORCE_GE(x_dim.size(), y_dim.size(),
4038
"Rank of first input must >= rank of second input.")
4139
ctx->SetOutputDim("Out", x_dim);

paddle/operators/elementwise_op_function.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void ElementwiseCompute(const framework::ExecutionContext& ctx) {
108108
PADDLE_ENFORCE_GE(x_dims.size(), y_dims.size(),
109109
"Rank of first input must >= rank of second input.")
110110

111-
if (x_dims == y_dims || product(y_dims) == 1) {
111+
if (x_dims == y_dims) {
112112
functor f;
113113
f.template Run<Place, T>(x, y, z, ctx);
114114
return;
@@ -174,12 +174,6 @@ void ElementwiseGradCompute(const framework::ExecutionContext& ctx) {
174174
return;
175175
}
176176

177-
if (product(y_dims) == 1) {
178-
functor1 f;
179-
f(place, x, y, out, dx, dy, dout);
180-
return;
181-
}
182-
183177
int axis = ctx.Attr<int>("axis");
184178
axis = (axis == -1 ? x_dims.size() - y_dims.size() : axis);
185179

python/paddle/v2/framework/tests/test_elementwise_add_op.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,33 @@ def setUp(self):
9292
}
9393

9494

95+
class TestElementwiseAddOp_rowwise_add_0(TestElementwiseOp):
96+
def setUp(self):
97+
self.op_type = "elementwise_add"
98+
self.inputs = {
99+
'X': np.random.rand(2, 3, 4).astype(np.float32),
100+
'Y': np.random.rand(3, 4).astype(np.float32)
101+
}
102+
103+
self.attrs = {'axis': 1}
104+
self.outputs = {
105+
'Out': self.inputs['X'] + self.inputs['Y'].reshape(1, 3, 4)
106+
}
107+
108+
109+
class TestElementwiseAddOp_rowwise_add_1(TestElementwiseOp):
110+
def setUp(self):
111+
self.op_type = "elementwise_add"
112+
self.inputs = {
113+
'X': np.random.rand(2, 1).astype(np.float32),
114+
'Y': np.random.rand(1).astype(np.float32)
115+
}
116+
117+
self.attrs = {'axis': 1}
118+
self.outputs = {
119+
'Out': self.inputs['X'] + self.inputs['Y'].reshape(1, 1)
120+
}
121+
122+
95123
if __name__ == '__main__':
96124
unittest.main()

python/paddle/v2/framework/tests/test_fit_a_line.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
cost = layers.square_error_cost(input=y_predict, label=y, program=program)
1818
avg_cost = layers.mean(x=cost, program=program)
1919

20-
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.01)
20+
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.005)
2121
opts = sgd_optimizer.minimize(avg_cost)
2222

2323
# print str(program)
2424

25-
BATCH_SIZE = 2
25+
BATCH_SIZE = 16
2626

2727
train_reader = paddle.batch(
2828
paddle.reader.shuffle(
@@ -32,23 +32,17 @@
3232
place = core.CPUPlace()
3333
exe = Executor(place)
3434

35-
PASS_NUM = 1
35+
PASS_NUM = 5
3636
for pass_id in range(PASS_NUM):
3737
for data in train_reader():
3838
x_data = np.array(map(lambda x: x[0], data)).astype("float32")
3939
y_data = np.array(map(lambda x: x[1], data)).astype("float32")
40-
# y_data = np.expand_dims(y_data, axis=1)
41-
# print x_data
42-
# print type(x_data)
43-
# print y_data
4440

4541
tensor_x = core.LoDTensor()
4642
tensor_x.set(x_data, place)
47-
# print tensor_x.get_dims()
4843

4944
tensor_y = core.LoDTensor()
5045
tensor_y.set(y_data, place)
51-
# print tensor_y.get_dims()
5246
outs = exe.run(program,
5347
feed={'x': tensor_x,
5448
'y': tensor_y},

0 commit comments

Comments
 (0)