Reduction.md 1.8 KB
Newer Older
S
sunyanfang01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
## 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
    }
}
```


### [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)
```python
paddle.fluid.layers.reduce_sum(
    input, 
    dim=None, 
    keep_dim=False, 
    name=None
)

paddle.fluid.layers.reduce_mean(
    input, 
    dim=None, 
    keep_dim=False, 
    name=None
)
```  

### 功能差异
#### 输入参数的差异
Caffe:一个层里面可以是`SUM``ASUM``SUMSQ`或者`MEAN`这四种操作。                                          
PaddlePaddle:只能完成里面的两种操作。同时Caffe可以设置`coeff`来将每个值乘以一个系数。

#### 输出的差异
Caffe:`axis`往后的每个维度都会缩减为一个维度。              
PaddlePaddle:只会缩减`dim`中list定义的维度,并根据`keep_dim`确定是否在输出Tensor中保留减小的维度。
### 代码示例
```  
# 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)
```