> 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/49-nei-rong-dai-jia-han-shu-ff08-content-cost-function.md).

# 4.8 内容代价函数（Content cost function）

$$J(G)$$的第一部分$$J\_{content}(C,G)$$，它表示内容图片C与生成图片G之间的相似度

使用的CNN网络是之前训练好的模型，例如Alex-Net。C，S，G共用相同模型和参数

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

CNN的每个隐藏层分别提取原始图片的不同深度特征，由简单到复杂。如果$$l$$太小，则G与C在像素上会非常接近，没有迁移效果；如果$$l$$太深，则G上某个区域将直接会出现C中的物体。所以在实际中，层$$l$$在网络中既不会选的太浅也不会选的太深

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

衡量内容图片和生成图片在内容上的相似度：

令$$a^{\[l]\[C]}$$和$$a^{\[l]\[G]}$$代表图片$$C$$和$$G$$的$$l$$层的激活函数值。如果这两个激活值相似，意味着两个图片的内容相似

定义：

$$
J\_{content}(C,G) =  \frac{1}{4 \times n\_H \times n\_W \times n\_C}\sum \_{ \text{all entries}} (a^{\[l]\[C]} - a^{\[l]\[C]})^2
$$

为两个激活值不同或者相似的程度

后面如果对$$J(G)$$做梯度下降来找$$G$$的值时，整个代价函数会激励这个算法来找到图像$$G$$，使得隐含层的激活值和内容图像的相似
