在神经网络入门回顾(感知器、多层感知器)中整理了关于感知器和多层感知器的理论,这里实现关于与门、与非门、或门、异或门的代码,以便对感知器有更好的感觉。
此外,我们使用 pytest 框架进行测试。
pip install pytest
与门、与非门、或门
通过一层感知器就可以实现与门、与非门、或门。
先写测试代码 test_perception.py:
1 from perception importand_operate, nand_operate, or_operate2
3
4 deftest_and_operate():5 """
6 测试与门7 :return:8 """
9 assert and_operate(1, 1) == 1
10 assert and_operate(1, 0) ==011 assert and_operate(0, 1) ==012 assert and_operate(0, 0) ==013
14
15 deftest_nand_operate():16 """
17 测试与非门18 :return:19 """
20 assert nand_operate(1, 1) ==021 assert nand_operate(1, 0) == 1
22 assert nand_operate(0, 1) == 1
23 assert nand_operate(0, 0) == 1
24
25
26 deftest_or_operate():27 """
28 测试或门29 :return:30 """
31 assert or_operate(1, 1) == 1
32 assert or_operate(1, 0) == 1
33 assert or_operate(0, 1) == 1
34 assert or_operate(0, 0) == 0
写完测试代码,后面直接输入命令 pytest -v 即可测试代码。
这三个门的权重和偏置是根据人的直觉或者画图得到的,并且不是唯一的。以下是简单的实现,在 perception.py 中写上:
1 importnumpy as np2
3
4 defstep_function(x):5 """
6 阶跃函数7 :param x:8 :return:9 """
10 if x <=0:11 return012 else:13 return 1
14
15
16 defand_operate(x1, x2):17 """
18 与门19 :param x1:20 :param x2:21 :return:22 """
23 x =np.array([x1, x2])24 w = np.array([0.5, 0.5])25 b = -0.7
26 return step_function(np.sum(w * x) +b)27
28
29 defnand_operate(x1, x2):30 """
31 与非门32 :param x1:33 :param x2:34 :return:35 """
36 x =np.array([x1, x2])37 w = np.array([-0.5, -0.5])38 b = 0.7
39 return step_function(np.sum(w * x) +b)40
41
42 defor_operate(x1, x2):43 """
44 或门45 :param x1:46 :param x2:47 :return:48 """
49 x =np.array([x1, x2])50 w = np.array([0.5, 0.5])51 b = -0.3
52 return step_function(np.sum(w * x) + b)
运行 pytest -v 确认测试通过。
========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- /Users/mac/.virtualenvs/work/bin/python3
...
collected 3 items
test_perception.py::test_and_operate PASSED [ 33%]
test_perception.py::test_nand_operate PASSED [ 66%]
test_perception.py::test_or_operate PASSED [100%]
=========================================================================== 3 passed in 0.51s ============================================================================
异或门
如上图所示,由于异或门不是线性可分的,因此需要多层感知器的结构。
使用两层感知器可以实现异或门。
修改 test_perception.py 文件,加入异或门的测试代码 :
from perception import and_operate, nand_operate, or_operate, xor_operate
以及
deftest_xor_operate():"""测试异或门
:return:"""
assert xor_operate(1, 1) ==0assert xor_operate(1, 0) == 1
assert xor_operate(0, 1) == 1
assert xor_operate(0, 0) == 0
在 perception.py 文件里加入异或门的函数:
defxor_operate(x1, x2):"""异或门
:param x1:
:param x2:
:return:"""s1=nand_operate(x1, x2)
s2=or_operate(x1, x2)return and_operate(s1, s2)
我们通过与非门和或门的线性组合实现了异或门。
运行命令 pytest -v 测试成功。
========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.6.8, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 -- /Users/mac/.virtualenvs/work/bin/python3
...
collected 4 items
test_perception.py::test_and_operate PASSED [ 25%]
test_perception.py::test_nand_operate PASSED [ 50%]
test_perception.py::test_or_operate PASSED [ 75%]
test_perception.py::test_xor_operate PASSED [100%]
=========================================================================== 4 passed in 0.60s ============================================================================
参考
《Neural networks and deep learning》by Aurélien Géron
《Deep learning from scratch》by 斋藤康毅