加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Python实现感知器的逻辑电路(与门、与非门、或门、异或门)

发布时间:2021-07-06 06:10:23 所属栏目:大数据 来源: https://www.jb51.cc
导读:在神经网络入门回顾(感知器、多层感知器)中整理了关于感知器和多层感知器的理论,这里实现关于 与门 、 与非门 、 或门 、 异或门 的代码,以便对感知器有更好的感觉。 此外,我们使用 pytest 框架进行测试。 pip install pytest @H_301_25@ 与门、与非门

在神经网络入门回顾(感知器、多层感知器)中整理了关于感知器和多层感知器的理论,这里实现关于与门与非门或门异或门的代码,以便对感知器有更好的感觉。

此外,我们使用 pytest 框架进行测试。

pip install pytest
@H_301_25@

与门、与非门、或门

通过一层感知器就可以实现与门与非门或门

先写测试代码 test_perception.py:

 1 from perception import and_operate,nand_operate,or_operate
 2 
 3 
 4 def test_and_operate():
 5     """
 6     测试与门
 7     :return:
 8      9     assert and_operate(1,1) == 1
10      0
11     assert and_operate(0,1) ==12     13 
14 
15  test_nand_operate():
16     17     测试与非门
18 19     20     assert nand_operate(1,1)">21     22     assert nand_operate(0,1)">23     24 
25 
26  test_or_operate():
27     28     测试或门
29 30     31     assert or_operate(1,1)">32     33     assert or_operate(0,1)">34     pytest -v? 即可测试代码。

这三个门的权重偏置是根据人的直觉或者画图得到的,并且不是唯一的。以下是简单的实现,在 perception.py 中写上:

 numpy as np
 step_function(x):
    阶跃函数
    :param x:
 8  9     if x <= 0:
11         returnelse:
13         return 1
15 
16  and_operate(x1,x2):
17         与门
19     :param x1:
20     :param x2:
21 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  nand_operate(x1,1)">31     与非门
32 33 34 35     36     x =37     w = np.array([-0.5,-0.538     b = 0.7
39     40 
41 
42  or_operate(x1,1)">43     44     或门
45 46 47 48     49     x =50     w = np.array([0.5,1)">51     b = -0.3
52     return step_function(np.sum(w * x) + b)
@H_301_25@

运行??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 ============================================================================
@H_301_25@

异或门

?

?

如上图所示,由于异或门不是线性可分的,因此需要多层感知器的结构。

使用两层感知器可以实现异或门。

修改 test_perception.py 文件,加入异或门的测试代码 :

import and_operate,or_operate,xor_operate
@H_301_25@

以及

 test_xor_operate():
    """
    测试异或门
    :return:
    """
    assert xor_operate(1,1)"> 0
    assert xor_operate(0,1) == 1
     xor_operate(x1,x2):
    
    异或门
    :param x1:
    :param x2:
    :return:
    """
    s1 =return and_operate(s1,s2)
@H_301_25@

我们通过与非门和或门的线性组合实现了异或门。

运行命令??pytest -v?测试成功。

========================================================================== test session starts ===========================================================================
platform darwin -- Python 3.6.8,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 ============================================================================
@H_301_25@

?

原文作者:雨先生
原文链接:https://www.cnblogs.com/noluye/p/11465389.html??
许可协议:知识共享署名-非商业性使用 4.0 国际许可协议

?

参考

  • 神经网络入门回顾(感知器、多层感知器)
  • 《Neural networks and deep learning》by Aurélien Géron
  • 《Deep learning from scratch》by 斋藤康毅

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读