# 4.5 面部验证与二分类（Face verification and binary classification）

另一个训练神经网络的方法是选取一对神经网络，选取**Siamese**网络，使其同时计算这些嵌入，比如说128维的嵌入（编号1），或者更高维，然后将其输入到逻辑回归单元进行预测，如果是相同的人，那么输出是1，若是不同的人，输出是0。这就把人脸识别问题转换为一个二分类问题，训练这种系统时可以替换**Triplet loss**的方法

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

最后的逻辑回归单元怎么处理：

比如说**sigmoid**函数应用到某些特征上，输出$$\hat y$$会变成：

$$
\hat y = \sigma(\sum\_{k = 1}^{128}{w\_{i}| f( x^{( i)})*{k} - f( x^{( j)})*{k}| + b})
$$

把这128个元素当作特征，然后把他们放入逻辑回归中，最后的逻辑回归可以增加参数$$w\_{i}$$和$$b$$，就像普通的逻辑回归一样。然后在这128个单元上训练合适的权重，用来预测两张图片是否是一个人

$$\hat y$$的另外一种表达式为：

$$
\hat y=\sigma(\sum\_{k=1}^Kw\_k\frac{(f(x^{(i)})\_k-f(x^{(j)})\_k)^2}{f(x^{(i)})\_k+f(x^{(j)})\_k}+b)
$$

这个公式也被叫做$$\chi^{2}$$公式，也被称为$$\chi$$平方相似度

上面神经网络拥有的参数和下面神经网络的相同（编号3和4所示的网络），两组参数是绑定的，这样的系统效果很好

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

如果这是一张新图片（编号1），当员工走进门时，希望门可以自动为他们打开，这个（编号2）是在数据库中的图片，不需要每次都计算这些特征（编号6），可以提前计算好，当一个新员工走近时，使用上方的卷积网络来计算这些编码（编号5），和预先计算好的编码进行比较，然后输出预测值$$\hat y$$

总结：把人脸验证当作一个监督学习，创建一个只有成对图片的训练集，不是三个一组，而是成对的图片，目标标签是1表示一对图片是一个人，目标标签是0表示图片中是不同的人。利用不同的成对图片，使用反向传播算法去训练**Siamese**神经网络

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


---

# 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/special-applications/45-mian-bu-yan-zheng-yu-er-fen-lei-ff08-face-verification-and-binary-classification.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.
