Reduction.md 2.0 KB
Newer Older
S
sunyanfang01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## Reduction


### [Reduction](http://caffe.berkeleyvision.org/tutorial/layers/reshape.html)
```
layer {
    name: "reduce"
    type: "Reduction"
    bottom: "reduce"
    top: “reduce"
    reduction_param{
        operation: SUM
	axis: 1
	coeff: 2
    }
}
```


J
Jason 已提交
20 21
### [paddle.fluid.layers.reduce_sum](http://paddlepaddle.org/documentation/docs/zh/1.3/api_cn/layers_cn.html#permalink-127-reduce_sum)
### [paddle.fluid.layers.reduce_mean](http://paddlepaddle.org/documentation/docs/zh/1.3/api_cn/layers_cn.html#permalink-124-reduce_mean)
S
sunyanfang01 已提交
22 23 24 25 26 27 28
```python
paddle.fluid.layers.reduce_sum(
    input, 
    dim=None, 
    keep_dim=False, 
    name=None
)
J
Jason 已提交
29 30
```
```
S
sunyanfang01 已提交
31 32 33 34 35 36 37 38 39
paddle.fluid.layers.reduce_mean(
    input, 
    dim=None, 
    keep_dim=False, 
    name=None
)
```  

### 功能差异
J
Jason 已提交
40 41 42 43 44 45 46
#### 操作类型
Caffe:通过`operation`参数支持`SUM``ASUM``SUMSQ``MEAN`四种操作;                                          
PaddlePaddle:`reduce_sum``reduce_mean`分别对应Caffe的`SUM``MEAN`操作,另外两种无对应。

#### 计算方式
Caffe:`axis``int`型参数,该维及其后维度,均会被降维,且不保留对应部分的维度,如shape为`(30, 3, 6, 8)``axis`为2的情况下,得到的输出shape为`(30, 3)`
PaddlePaddle:`dim`参数为`list`型参数,其指定的维度才会被降维,且当`keep_dim``True`时,降维的维度仍会以`1`的形式保留下来,如shape为`(30, 3, 6, 8)``dim``[2, 3]``keep_dim``True`的情况下,得到的输出shape为`(30, 3, 1, 1)`
S
sunyanfang01 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

### 代码示例
```  
# Caffe示例:  
# 输入shape:(30,3,6,8)
layer {
    name: "reduce"
    type: "Reduction"
    bottom: "reduce"
    top: “reduce"
    reduction_param{
	operation: SUM
	axis: 2
	coeff: 2
    }
}
# 输出shape:(30,3,)
```  
```python 
# PaddlePaddle示例:  
# 输入shape:(30,3,6,8)
output1 = fluid.layers.reduce_mean(input = inputs, dim=[1])
# 输出shape:(30,6,8)
output2 = fluid.layers.reduce_mean(input = inputs, dim=[1], keep_dim=True, name=None)
# 输出shape:(30,1,6,8)
```