Skip to content

Commit 61444d9

Browse files
committed
Merge remote-tracking branch 'baidu/develop' into feature/sppnet
2 parents f173341 + 8d4c453 commit 61444d9

File tree

6 files changed

+51
-27
lines changed

6 files changed

+51
-27
lines changed

paddle/math/Matrix.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ MatrixPtr Matrix::subMatrix(size_t startRow, size_t endRow, size_t startCol,
187187
trans_, useGpu_);
188188
}
189189

190+
void Matrix::setDiag(real value) {
191+
CHECK(data_ != NULL);
192+
CHECK_EQ(height_, width_);
193+
194+
zeroMem();
195+
BaseMatrix diag(height_, 1, stride_ + 1, data_, false, useGpu_);
196+
diag.assign(value);
197+
}
198+
190199
GpuMatrix::GpuMatrix(size_t height, size_t width, bool trans)
191200
: Matrix(std::make_shared<GpuMemoryHandle>(height * width * sizeof(real)),
192201
height, width, trans, true) {}
@@ -202,6 +211,7 @@ void GpuMatrix::resetOne() {
202211
CHECK(data_ != NULL);
203212
one();
204213
}
214+
205215
void GpuMatrix::resize(size_t newHeight, size_t newWidth) {
206216
size_t newSize = newHeight * newWidth;
207217
if (NULL == memoryHandle_.get() ||

paddle/math/Matrix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ class Matrix : public BaseMatrix {
195195

196196
virtual void resetOne() { LOG(FATAL) << "Not implemented"; }
197197

198+
void setDiag(real value);
199+
198200
virtual void copyFrom(const Matrix& src) { LOG(FATAL) << "Not implemented"; }
199201

200202
virtual void trimFrom(const CpuSparseMatrix& src) {
@@ -330,6 +332,7 @@ class Matrix : public BaseMatrix {
330332

331333
virtual MatrixPtr getInverse() {
332334
LOG(FATAL) << "Not implemented";
335+
return nullptr;
333336
}
334337

335338
/**
@@ -1016,6 +1019,7 @@ class GpuMatrix : public Matrix {
10161019

10171020
void zeroMem();
10181021
void resetOne();
1022+
void setDiag(real value);
10191023

10201024
void resize(size_t newHeight, size_t newWidth);
10211025
void resize(size_t newHeight, size_t newWidth,
@@ -1280,6 +1284,8 @@ class CpuMatrix : public Matrix {
12801284

12811285
void zeroMem();
12821286
void resetOne();
1287+
void setDiag(real value);
1288+
12831289
void resize(size_t newHeight, size_t newWidth);
12841290
void resize(size_t newHeight, size_t newWidth,
12851291
size_t newNnz, /* used to allocate space */

paddle/math/tests/test_matrixCompare.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,20 +647,23 @@ void testMatrixInverse(int height) {
647647
MatrixPtr cpuI = std::make_shared<CpuMatrix>(height, height);
648648
MatrixPtr gpuI = std::make_shared<GpuMatrix>(height, height);
649649

650+
/* Make matrix well conditioned: cpu * cpuT + Identity */
650651
cpu->randomizeUniform();
652+
MatrixPtr cpuT = cpu->getTranspose();
653+
MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, height);
654+
outputCheck->mul(cpu, cpuT);
655+
cpu->setDiag(1.0);
656+
cpu->add(*outputCheck);
657+
651658
gpu->copyFrom(*cpu);
652659
cpu->inverse(cpuI, false);
653660
gpu->inverse(gpuI, false);
654661

655-
MatrixPtr outputCheck = std::make_shared<CpuMatrix>(height, height);
656662
outputCheck->copyFrom(*gpuI);
657663
MatrixCheckErr(*cpuI, *outputCheck);
658664

659665
outputCheck->mul(cpu, cpuI);
660-
cpu->zeroMem();
661-
for (int i = 0; i < height; i++) {
662-
cpu->getRowBuf(i)[i] = 1.0;
663-
}
666+
cpu->setDiag(1.0);
664667
MatrixCheckErr(*cpu, *outputCheck);
665668
}
666669

python/paddle/trainer_config_helpers/layers.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,17 @@ def __enter__(self):
592592
def __exit__(self, *args, **kwargs):
593593
del args, kwargs # unused parameter to suppress warning
594594
assert len(self.inputs) != 0
595-
MixedLayer(
595+
ml = MixedLayer(
596596
name=self.name,
597597
size=self.size,
598598
active_type=self.activation.name,
599599
bias=ParamAttr.to_bias(self.bias_attr),
600600
inputs=self.inputs,
601601
**ExtraLayerAttribute.to_kwargs(self.layer_attr)
602602
)
603+
# update the size which might be computed inside MixedLayer
604+
# according to the operator's output size
605+
self.size = ml.config.size
603606

604607

605608
@wrap_name_default("mixed")
@@ -2104,7 +2107,7 @@ def __reduce_concat_type__(a, b):
21042107

21052108
if layer_type == LayerType.CONCAT_LAYER:
21062109
assert not bias_attr
2107-
2110+
21082111
Layer(
21092112
name=name, type=layer_type,
21102113
inputs=[x.name for x in input] if is_concat_layer else input,
@@ -2682,7 +2685,7 @@ def out_prod_layer(input1, input2, name=None, layer_attr=None):
26822685
assert isinstance(input1, LayerOutput)
26832686
assert isinstance(input2, LayerOutput)
26842687
Layer(name=name,
2685-
type="out_prod",
2688+
type=LayerType.OUT_PROD_LAYER,
26862689
inputs=[input1.name, input2.name],
26872690
**ExtraLayerAttribute.to_kwargs(layer_attr))
26882691
return LayerOutput(name=name,
@@ -2849,7 +2852,7 @@ def __real_step__(*args):
28492852

28502853
def __cost_input__(input, label, weight=None):
28512854
"""
2852-
inputs and parents for cost layers.
2855+
inputs and parents for cost layers.
28532856
"""
28542857
ipts = [Input(input.name), Input(label.name)]
28552858
parents = [input, label]
@@ -2858,7 +2861,7 @@ def __cost_input__(input, label, weight=None):
28582861
ipts.append(Input(weight.name))
28592862
parents.append(weight)
28602863
return ipts, parents
2861-
2864+
28622865

28632866
@wrap_name_default()
28642867
@layer_support()
@@ -2943,7 +2946,7 @@ def __add_evaluator__(e):
29432946

29442947

29452948
def conv_operator(img, filter, filter_size, num_filters,
2946-
num_channel=None, stride=1, padding=0,
2949+
num_channels=None, stride=1, padding=0,
29472950
filter_size_y=None, stride_y=None, padding_y=None):
29482951
"""
29492952
Different from img_conv_layer, conv_op is an Operator, which can be used
@@ -2973,8 +2976,8 @@ def conv_operator(img, filter, filter_size, num_filters,
29732976
:type filter_size_y: int
29742977
:param num_filters: channel of output data.
29752978
:type num_filters: int
2976-
:param num_channel: channel of input data.
2977-
:type num_channel: int
2979+
:param num_channels: channel of input data.
2980+
:type num_channels: int
29782981
:param stride: The x dimension of the stride.
29792982
:type stride: int
29802983
:param stride_y: The y dimension of the stride.
@@ -2993,19 +2996,19 @@ def conv_operator(img, filter, filter_size, num_filters,
29932996
if padding_y is None:
29942997
padding_y = padding
29952998

2996-
if num_channel is None:
2997-
num_channel = img.num_filters
2999+
if num_channels is None:
3000+
num_channels = img.num_filters
29983001

29993002
assert isinstance(filter, LayerOutput)
30003003
if filter.size is not None:
3001-
filter.size = filter_size * filter_size_y * num_filters * num_channel
3004+
filter.size = filter_size * filter_size_y * num_filters * num_channels
30023005

30033006
op = ConvOperator(input_layer_names=[img.name, filter.name],
30043007
num_filters=num_filters,
30053008
conv_conf=Conv(filter_size=filter_size,
30063009
padding=padding,
30073010
stride=stride,
3008-
channels=num_channel,
3011+
channels=num_channels,
30093012
filter_size_y=filter_size_y,
30103013
padding_y=padding_y,
30113014
stride_y=stride_y,
@@ -3045,8 +3048,8 @@ def conv_projection(input, filter_size, num_filters,
30453048
:type filter_size_y: int
30463049
:param num_filters: channel of output data.
30473050
:type num_filters: int
3048-
:param num_channel: channel of input data.
3049-
:type num_channel: int
3051+
:param num_channels: channel of input data.
3052+
:type num_channels: int
30503053
:param stride: The x dimension of the stride.
30513054
:type stride: int
30523055
:param stride_y: The y dimension of the stride.
@@ -3537,15 +3540,15 @@ def maxout_layer(input,
35373540
- Input: output of a conv layer.
35383541
- Output: feature map size same as input. Channel is (input channel) / groups.
35393542
3540-
So groups should be larger than 1, and the num of channels should be able
3543+
So groups should be larger than 1, and the num of channels should be able
35413544
to devided by groups.
35423545
3543-
Please refer to Paper:
3546+
Please refer to Paper:
35443547
- Maxout Networks: http://www.jmlr.org/proceedings/papers/v28/goodfellow13.pdf
35453548
- Multi-digit Number Recognition from Street View \
35463549
Imagery using Deep Convolutional Neural Networks: \
35473550
https://arxiv.org/pdf/1312.6082v4.pdf
3548-
3551+
35493552
The simple usage is:
35503553
35513554
.. code-block:: python
@@ -3790,9 +3793,9 @@ def nce_layer(input, label, num_classes, weight=None,
37903793
:param weight: weight layer, can be None(default)
37913794
:type weight: LayerOutput
37923795
:param num_classes: number of classes.
3793-
:type num_classes: int
3796+
:type num_classes: int
37943797
:param num_neg_samples: number of negative samples. Default is 10.
3795-
:type num_neg_samples: int
3798+
:type num_neg_samples: int
37963799
:param neg_distribution: The distribution for generating the random negative labels.
37973800
A uniform distribution will be used if not provided.
37983801
If not None, its length must be equal to num_classes.
@@ -3813,7 +3816,7 @@ def nce_layer(input, label, num_classes, weight=None,
38133816
assert isinstance(neg_distribution, collections.Sequence)
38143817
assert len(neg_distribution) == num_classes
38153818
assert sum(neg_distribution) == 1
3816-
3819+
38173820
ipts_for_layer = []
38183821
parents = []
38193822
for each_input in input:

python/paddle/trainer_config_helpers/tests/configs/projections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
with mixed_layer() as m7:
3737
m7 += conv_operator(img=img, filter=flt, num_filters=64,
38-
num_channel=1, filter_size=3)
38+
num_channels=1, filter_size=3)
3939

4040
end = mixed_layer(input=[full_matrix_projection(input=m5),
4141
trans_full_matrix_projection(input=m6),

python/paddle/trainer_config_helpers/tests/layers_test_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
filter=y1,
3030
filter_size=1,
3131
num_filters=5,
32-
num_channel=5,
32+
num_channels=5,
3333
stride=1)])
3434

35+
assert z1.size > 0
36+
3537
y2 = fc_layer(input=y, size=15)
3638

3739
cos1 = cos_sim(a=x1, b=y1)

0 commit comments

Comments
 (0)