资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

利用Python怎么编写一个感知器的逻辑电路-创新互联

本篇文章为大家展示了利用Python怎么编写一个感知器的逻辑电路,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

我们提供的服务有:成都网站设计、网站制作、微信公众号开发、网站优化、网站认证、道县ssl等。为近千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的道县网站制作公司
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 ============================================================================

上述内容就是利用Python怎么编写一个感知器的逻辑电路,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


名称栏目:利用Python怎么编写一个感知器的逻辑电路-创新互联
网站链接:http://cdkjz.cn/article/diccgp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220