Skip to content

Commit 2e1cd33

Browse files
author
ranqiu
committed
Update dot_prod_layer
1 parent aa25071 commit 2e1cd33

File tree

7 files changed

+66
-5
lines changed

7 files changed

+66
-5
lines changed

doc/api/v2/config/layer.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ bilinear_interp
335335
.. autoclass:: paddle.v2.layer.bilinear_interp
336336
:noindex:
337337

338+
dot_prod
339+
---------
340+
.. autoclass:: paddle.v2.layer.dot_prod
341+
:noindex:
342+
343+
out_prod
344+
--------
345+
.. autoclass:: paddle.v2.layer.out_prod
346+
:noindex:
347+
338348
power
339349
-----
340350
.. autoclass:: paddle.v2.layer.power

paddle/gserver/layers/DotProdLayer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License. */
2020
namespace paddle {
2121

2222
/**
23-
* @brief A layer for computing the dot product of two vectors
23+
* @brief A layer for computing the dot product of two vectors.
2424
* Input1: vector (batchSize * dim)
2525
* Input2: vector (batchSize * dim)
2626
* Output: a matrix: (batchSize * 1)
@@ -46,7 +46,8 @@ bool DotProdLayer::init(const LayerMap& layerMap,
4646
Layer::init(layerMap, parameterMap);
4747

4848
CHECK_EQ(inputLayers_.size(), 2U);
49-
CHECK_EQ(1, getSize()) << "Dimension mismatch";
49+
CHECK_EQ(1UL, getSize())
50+
<< "The output dimensionality of this layer should be fixed to 1.";
5051

5152
return true;
5253
}
@@ -59,6 +60,7 @@ void DotProdLayer::forward(PassType passType) {
5960

6061
size_t batchSize = inV0->getHeight();
6162
CHECK_EQ(inV1->getHeight(), batchSize);
63+
CHECK_EQ(inV0->getWidth(), inV1->getWidth());
6264

6365
{
6466
REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());

paddle/gserver/tests/test_LayerGrad.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ TEST(Layer, DotProdLayer) {
10921092
config.layerConfig.add_inputs();
10931093

10941094
for (auto useGpu : {false, true}) {
1095-
testLayerGrad(config, "dot_prod", 100, false, useGpu);
1095+
testLayerGrad(config, "dot_prod", 10, false, useGpu);
10961096
}
10971097
}
10981098

python/paddle/trainer/config_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3214,7 +3214,10 @@ class DotProdLayer(LayerBase):
32143214
def __init__(self, name, inputs, device=None):
32153215
super(DotProdLayer, self).__init__(
32163216
name, 'dot_prod', 0, inputs, device=device)
3217-
config_assert(len(inputs) == 2, 'DotProdLayer must have 2 inputs')
3217+
config_assert(len(inputs) == 2, 'DotProdLayer must have 2 inputs.')
3218+
config_assert(
3219+
self.get_input_layer(0).size == self.get_input_layer(1).size,
3220+
"Two inputs should have the same size.")
32183221
self.set_layer_size(1)
32193222

32203223

python/paddle/trainer_config_helpers/tests/configs/file_list.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test_prelu_layer test_row_conv test_detection_output_layer test_multibox_loss_la
1010
test_recursive_topology test_gated_unit_layer test_clip_layer test_row_l2_norm_layer
1111
test_kmax_seq_socre_layer test_sub_nested_seq_select_layer test_scale_shift_layer
1212
test_seq_slice_layer test_cross_entropy_over_beam test_roi_pool_layer test_pooling3D_layer
13-
test_conv3d_layer test_deconv3d_layer test_BatchNorm3D test_resize_layer test_scale_sub_region_layer)
13+
test_conv3d_layer test_deconv3d_layer test_BatchNorm3D test_resize_layer test_scale_sub_region_layer
14+
test_dot_prod_layer)
1415

1516
export whole_configs=(test_split_datasource)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
type: "nn"
2+
layers {
3+
name: "vector1"
4+
type: "data"
5+
size: 10
6+
active_type: ""
7+
}
8+
layers {
9+
name: "vector2"
10+
type: "data"
11+
size: 10
12+
active_type: ""
13+
}
14+
layers {
15+
name: "__dot_prod_layer_0__"
16+
type: "dot_prod"
17+
size: 1
18+
active_type: ""
19+
inputs {
20+
input_layer_name: "vector1"
21+
}
22+
inputs {
23+
input_layer_name: "vector2"
24+
}
25+
}
26+
input_layer_names: "vector1"
27+
input_layer_names: "vector2"
28+
output_layer_names: "__dot_prod_layer_0__"
29+
sub_models {
30+
name: "root"
31+
layer_names: "vector1"
32+
layer_names: "vector2"
33+
layer_names: "__dot_prod_layer_0__"
34+
input_layer_names: "vector1"
35+
input_layer_names: "vector2"
36+
output_layer_names: "__dot_prod_layer_0__"
37+
is_recurrent_layer_group: false
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from paddle.trainer_config_helpers import *
2+
3+
vec1 = data_layer(name='vector1', size=10)
4+
vec2 = data_layer(name='vector2', size=10)
5+
dot_product = dot_prod_layer(input1=vec1, input2=vec2)
6+
7+
outputs(dot_product)

0 commit comments

Comments
 (0)