深度学习:通过感知器实现and函数

通过梯度下降思想实现感知器:

import random
import numpy as np
# and函数是二元函数 需要两个权值向量
b = random.random()
w1 = random.random()
w2 = random.random()
print(w1, w2, b)
# 学习率
a = 0.1
# 真值表
x = np.array([[1,1], [0,0], [1,0], [0,1]])
t = np.array([1, 0, 0, 0])
eps = 0.01
Z = 0
for i in range(0, 4):
    Z = b + x[i, 0] * w1 + x[i, 1] * w2
def S(x):
    return 1/(1+(2.7)**(-x))
Dw1 = 1
Dw2 = 1
Db  = 1
while Dw1 >= eps or Dw2 >= eps or Db >= eps:
    for i in range (0, 4):
        delta = t[i] - S(Z)
        w1 = w1 + a * delta * x[i, 0]
        w2 = w2 + a * delta * x[i, 1]
        b = b + a * delta
    Z = b + x[i, 0] * w1 + x[i, 1] * w2
    Dw1 = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z)) * x[0, 0]
    Dw2 = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z)) * x[0, 1]
    Db  = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z))
---------------------------------------------------------------------------

KeyboardInterrupt                         Traceback (most recent call last)

<ipython-input-199-308254797349> in <module>
     11     Dw1 = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z)) * x[0, 0]
     12     Dw2 = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z)) * x[0, 1]
---> 13     Db  = 2 * (t[0] - S(Z)) * S(Z) *(1 - S(Z))


<ipython-input-198-05863795717d> in S(x)
      1 def S(x):
----> 2     return 1/(1+(2.7)**(-x))


KeyboardInterrupt: 
print(w1, w2, b)
12305.25097849656 12305.02886071489 -12305.726717189342
def predict(x1, x2):
    return S(b + w1 * x1 + w2 * x2)
print('1 and 1 = %d' % predict(1, 1))
print('0 and 0 = %d' % predict(0, 0))
print('1 and 0 = %d' % predict(1, 0))
print('0 and 1 = %d' % predict(0, 1))
1 and 1 = 1
0 and 0 = 0
1 and 0 = 0
0 and 1 = 0


<ipython-input-198-05863795717d>:2: RuntimeWarning: overflow encountered in double_scalars
  return 1/(1+(2.7)**(-x))

但是由于eps取合适值较难,所以while循环需要强行跳出。中断后继续执行,and函数实现。

也可以使用for循环来实现:

import random
import numpy as np
# and函数是二元函数 需要两个权值向量
b = random.random()
w1 = random.random()
w2 = random.random()
# 学习率
a = 0.1
# 真值表
x = np.array([[1,1], [0,0], [1,0], [0,1]])
t = np.array([1, 0, 0, 0])
# 
eps = 0.001
# 求和Z
Z = 0
for i in range(0, 4):
    Z = b + x[i, 0] * w1 + x[i, 1] * w2
def S(x):
    return 1/(1+(2.7)**(-x))
for i in range (0,10): # 迭代十次
    for i in range (0, 4):
        Z = b + x[i, 0] * w1 + x[i, 1] * w2
        delta = t[i] - S(Z)
        w1 = w1 + a * delta * x[i, 0]
        w2 = w2 + a * delta * x[i, 1]
        b = b + a * delta
def predict(x1, x2):
    ans = S(b + w1 * x1 + w2 * x2)
    if(ans > 0.5):
        return 1
    else:
        return 0
print('1 and 1 = %d' % predict(1, 1))
print('0 and 0 = %d' % predict(0, 0))
print('1 and 0 = %d' % predict(1, 0))
print('0 and 1 = %d' % predict(0, 1))
1 and 1 = 1
0 and 0 = 0
1 and 0 = 0
0 and 1 = 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值