> For the complete documentation index, see [llms.txt](https://baozoulin.gitbook.io/neural-networks-and-deep-learning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](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/special-applications/410-feng-ge-dai-jia-han-shu-ff08-style-cost-function.md).

# 4.9 风格代价函数（Style cost function）

利用CNN网络模型，图片的风格可以定义成第$$l$$层隐藏层不同通道间激活函数的乘积（相关性）

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

选取第$$l$$层隐藏层，各通道使用不同颜色标注。因为每个通道提取图片的特征不同，比如1通道（红色）提取的是图片的垂直纹理特征，2通道（黄色）提取的是图片的橙色背景特征。那么这两个通道的相关性越大，表示原始图片及既包含了垂直纹理也包含了该橙色背景；相关性越小，表示原始图片并没有同时包含这两个特征。即计算不同通道的相关性，反映了原始图片特征间的相互关系，从某种程度上刻画了图片的“风格”

![](/files/-Le0cUOoS6tLVjGbkYWq)

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

接下来定义图片的风格矩阵（style matrix）为：

$$
G\_{kk^{'}}^{\[l]} = \sum\_{i = 1}^{n\_{H}^{\[l]}}{\sum\_{j = 1}^{n\_{W}^{\[l]}}{a\_{i, j,k}^{\[l]}a\_{i, j, k^{'}}^{\[l]}}}
$$

$$a\_{i, j, k}^{\[l]}$$为隐藏层$$l$$中$$(i,j,k)$$位置的激活项，$$i$$，$$j$$，$$k$$分别代表该位置的高度、宽度以及对应的通道数，k，$$k^{'}$$分别表示不同通道。风格矩阵$$G\_{kk^{'}}^{\[l]}$$计算第$$l$$层隐藏层不同通道对应的所有激活函数输出和，$$l$$层风格图像的矩阵$$G^{\[l]}$$是一个$$n\_{c} \times n\_{c}$$的矩阵：

![](/files/-Le0cUOsGx7rAKDyufOY)

若两个通道之间相似性高，则对应的$$G\_{kk^{'}}^{\[l]}$$较大；若两个通道之间相似性低，则对应的$$G\_{kk^{'}}^{\[l]}$$较小

风格矩阵$$G\_{kk'}^{[l](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/special-applications/S)}$$表征了风格图片$$S$$第$$l$$层隐藏层的“风格”。生成图片$$G$$也有$$G\_{kk'}^{[l](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/special-applications/G)}$$，$$G\_{kk'}^{[l](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/special-applications/S)}$$与$$G\_{kk'}^{[l](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/special-applications/G)}$$越相近，则表示$$G$$的风格越接近$$S$$。即$$J^{\[l]}\_{style}(S,G)$$定义为：

$$
J\_{style}^{\[l]}(S,G) = \frac{1}{4 \times {n\_C}^2 \times (n\_H \times n\_W)^2} \sum *{i=1}^{n\_C}\sum*{j=1}^{n\_C}(G^{(S)}*{ij} - G^{(G)}*{ij})^2
$$

然后使用梯度下降算法，不断迭代修正$$G$$的像素值，使$$J^{\[l]}\_{style}(S,G)$$不断减小

为了提取更多的“风格”，可以使用多层隐藏层，然后相加，表达式为：

$$
J\_{style}(S,G)=\sum\_l\lambda^{\[l]}\cdot J^{\[l]}\_{style}(S,G)
$$

$$\lambda^{\[l]}$$表示累加过程中各层$$J^{\[l]}\_{style}(S,G)$$的权重系数，为超参数

最终的cost function为：

$$
J(G)=\alpha \cdot J\_{content}(C,G)+\beta \cdot J\_{style}(S,G)
$$

之后用梯度下降法，或者更复杂的优化算法来找到一个合适的图像$$G$$，并计算$$J(G)$$的最小值，这样将能够得到非常好看的结果


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/special-applications/410-feng-ge-dai-jia-han-shu-ff08-style-cost-function.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.
