# 1.4 Padding

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/d21e2642815d03b15396f7998ba4459a.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/d21e2642815d03b15396f7998ba4459a.png)

如果有一个$$n\times n$$的图像，用$$f\times f$$的过滤器做卷积，输出的维度就是$$(n-f+1)\times (n-f+1)$$

这样的话会有两个缺点:

* 每次做卷积操作，**输出图片尺寸缩小**
* **原始图片边缘信息对输出贡献得少，输出图片丢失边缘信息**

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/170e076ceaeb70339baa7b25ad5f5e6c.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/170e076ceaeb70339baa7b25ad5f5e6c.png)

> 角落边缘的像素（绿色阴影标记）只被一个输出所触碰或者使用，中间的像素点（红色方框标记）会有许多3×3的区域与之重叠。角落或者边缘区域的像素点在输出中采用较少，丢掉了图像边缘位置的许多信息

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/208104bae9256fba5d8e37e22a9f5408.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/208104bae9256fba5d8e37e22a9f5408.png)

可以在卷积操作之前填充这幅图像。沿着图像边缘再填充一层像素,6×6的图像填充成8×8的图像。就得到了一个尺寸和原始图像6×6的图像。习惯上，可以用0去填充，如果$$p$$是填充的数量，输出也就变成了$$(n+2p-f+1)\times (n+2p-f+1)$$。涂绿的像素点（左边矩阵）影响了输出中的这些格子（右边矩阵）。这样角落或图像边缘的信息发挥的作用较小的这一缺点就被削弱了

选择填充多少像素，通常有两个选择，分别叫做**Valid**卷积和**Same**卷积

**Valid**卷积意味着不填充，如果有一个$$n\times n$$的图像，用一个$$f\times f$$的过滤器卷积，会给一个$$(n-f+1)\times (n-f+1)$$维的输出

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/0663e1a9e477e2737067d9e79194208d.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/0663e1a9e477e2737067d9e79194208d.png)

另一个叫做**Same**卷积，填充后输出大小和输入大小是一样的。由$$n-f+1$$，当填充$$p$$个像素点，$$n$$就变成了$$n+2p$$，公式变为：

$$
n+2p-f+1
$$

即：

$$
p=\frac{f-1}{2}
$$

当$$f$$是一个奇数，只要选择相应的填充尺寸就能确保得到和输入相同尺寸的输出

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/ca5382358f30c1349fff98d1e52366b4.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/ca5382358f30c1349fff98d1e52366b4.png)

计算机视觉中，$$f$$通常是奇数，有两个原因：

* 如果$$f$$是偶数，只能使用一些不对称填充
* 当有一个奇数维过滤器，比如3×3或者5×5的，它就有一个中心点，便于指出过滤器的位置


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-si-men-ke-juan-ji-shen-jing-wang-luo-convolutional-neural-networks/convolutional-neural-networks/foundations-of-convolutional-neural-networks/14-padding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
