# 稀疏自动编码器

## 原理

加入稀疏性惩罚项，让编码器的输入**结果稀疏**

编码器网络隐含层的第i个神经元的平均激活度为对所有训练样本的激活函数值的均值，记为$$\hat{\rho}=\frac{1}{m}\sum\_{i=1}^m a^{(2)}\_j (x^{(i)})$$，

p为人工指定的活跃度，是一个接近于0的数

稀疏性可以被简单地解释如下：如果当神经元的输出接近于1的时候我们认为它被激活，而输出接近于0的时候认为它被抑制，那么使得神经元大部分的时间都是被抑制的限制则被称作稀疏性限制。这里我们假设的神经元的激活函数是sigmoid函数。如果你使用tanh作为激活函数的话，当神经元输出为-1的时候，我们认为神经元是被抑制的。

使用**相对熵**构造惩罚项

$$
\begin{aligned}
\sum\_{i=1}^{n}(p ln \frac{p}{\hat p\_i}+(1-p)ln \frac{1-p}{1-\hat p\_i})
\end{aligned}
$$

加上惩罚项后的目标函数变为：

$$
\begin{aligned}
min\frac{1}{2l}\sum\_{i=1}^{l}||x\_i-g\_{\theta}(h\_{\theta}(x\_i))||*2^2 +\beta \sum*{i=1}^{n}(p ln \frac{p}{\hat p\_i}+(1-p)ln \frac{1-p}{1-\hat p\_i})
\end{aligned}
$$

也就是希望解码器后的向量尽量和输入的向量是一样的，同时还稀疏（有些维度上面是0）

## 应用

这样得到的是稀疏有些是0，有些和原来一样的向量，0的部分表示原理的那个维度那个值不用了，也就是相当于做特征选择的作用，去掉没用的维度特征值。


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shen-du-xue-xi-li-lun/01/03.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.
