Python如何实现感知器的逻辑电路

作者:何雨龙 时间:2021-01-09 00:41:38 

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

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


pip install pytest

与门、与非门、或门

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

先写测试代码 test_perception.py:


from perception import and_operate, nand_operate, or_operate

def test_and_operate():
"""
测试与门
:return:
"""
assert and_operate(1, 1) == 1
assert and_operate(1, 0) == 0
assert and_operate(0, 1) == 0
assert and_operate(0, 0) == 0

def test_nand_operate():
"""
测试与非门
:return:
"""
assert nand_operate(1, 1) == 0
assert nand_operate(1, 0) == 1
assert nand_operate(0, 1) == 1
assert nand_operate(0, 0) == 1

def test_or_operate():
"""
测试或门
:return:
"""
assert or_operate(1, 1) == 1
assert or_operate(1, 0) == 1
assert or_operate(0, 1) == 1
assert or_operate(0, 0) == 0

写完测试代码,后面直接输入命令  pytest -v  即可测试代码。

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


import numpy as np

def step_function(x):
"""
阶跃函数
:param x:
:return:
"""
if x <= 0:
 return 0
else:
 return 1

def and_operate(x1, x2):
"""
与门
:param x1:
:param x2:
:return:
"""
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.7
return step_function(np.sum(w * x) + b)

def nand_operate(x1, x2):
"""
与非门
:param x1:
:param x2:
:return:
"""
x = np.array([x1, x2])
w = np.array([-0.5, -0.5])
b = 0.7
return step_function(np.sum(w * x) + b)

def or_operate(x1, x2):
"""
或门
:param x1:
:param x2:
:return:
"""
x = np.array([x1, x2])
w = np.array([0.5, 0.5])
b = -0.3
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 ============================================================================

异或门

Python如何实现感知器的逻辑电路

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

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

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


from perception import and_operate, nand_operate, or_operate, xor_operate

以及


def test_xor_operate():
"""
测试异或门
:return:
"""
assert xor_operate(1, 1) == 0
assert xor_operate(1, 0) == 1
assert xor_operate(0, 1) == 1
assert xor_operate(0, 0) == 0

在 perception.py 文件里加入异或门的函数:


def xor_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 ============================================================================

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

来源:https://www.cnblogs.com/noluye/p/11465389.html

标签:python,感知器,逻辑电路
0
投稿

猜你喜欢

  • PHP 使用header函数设置HTTP头的示例解析 表头

    2023-09-03 19:12:49
  • 使用PHP获取当前url路径的函数以及服务器变量

    2023-10-31 02:31:34
  • 简单谈谈Python中的闭包

    2021-10-20 12:50:46
  • ASP中汉字与UTF-8的互相转换

    2009-06-24 11:15:00
  • numpy实现神经网络反向传播算法的步骤

    2021-02-11 10:54:34
  • 用Mysql查询语句记录

    2011-02-16 12:29:00
  • Python连接Redis的基本配置方法

    2023-09-29 21:28:27
  • 安装MySQL5.0后出现1607异常的解决办法

    2009-02-26 15:52:00
  • SQL Server数据库于应用程序的关系

    2010-07-26 14:21:00
  • sql server中通过查询分析器实现数据库的备份与恢复方法分享

    2012-05-22 18:41:58
  • Python爬取当网书籍数据并数据可视化展示

    2023-11-20 11:31:14
  • 关于pytorch处理类别不平衡的问题

    2023-04-08 19:11:56
  • PHP count()函数讲解

    2023-06-04 11:46:41
  • python中requests爬去网页内容出现乱码问题解决方法介绍

    2023-09-14 01:00:11
  • Oracle SQL性能优化系列学习三

    2010-07-23 13:08:00
  • 一个表单焦点效果函数

    2008-01-19 10:59:00
  • 从IIS到SQL Server数据库安全

    2008-12-24 15:58:00
  • Python将Excel表格按某列拆分为多个sheet实现过程

    2021-06-11 06:59:54
  • z-index在IE中的迷惑

    2007-05-11 16:50:00
  • python网络编程之TCP通信实例和socketserver框架使用例子

    2023-02-03 05:53:09
  • asp之家 网络编程 m.aspxhome.com