😊你好，我是小航，一个正在变秃、变强的文艺倾年。
🔔笔记来自B站UP主Ele实验室的《小白也能听懂的人工智能原理》。
🔔本文讲解激活函数：给机器注入灵魂，一起卷起来叭！

• ​​一、“分类”​​
• ​​二、代码实现​​

## 二、代码实现

`import numpy as npdef get_beans(counts):  xs = np.random.rand(counts)  xs = np.sort(xs)  ys = np.zeros(counts)  for i in range(counts):    x = xs[i]    yi = 0.7*x+(0.5-np.random.rand())/50+0.5    if yi > 0.8:      ys[i] = 1  return xs,ys`

`import datasetimport matplotlib.pyplot as pltimport numpy as np# 豆豆数量mm = 100xs, ys = dataset.get_beans(m)# 配置图像plt.title("Size-Toxicity Function", fontsize=12)plt.xlabel("Bean Size")plt.ylabel("Toxicity")plt.scatter(xs, ys)w = 0.1b = 0.1z = w * xs + ba = 1 / (1 + np.exp(-z)) # 加入激活函数plt.plot(xs, a)plt.show()# alpha为学习率alpha = 0.01# 训练5000次for _ in range(5000):    for i in range(100):        x = xs[i]        y = ys[i]        # 三个函数        z = w * x + b        a = 1 / (1 + np.exp(-z))        e = (y - a) ** 2        # 对w和b求偏导        deda = -2 * (y - a)        dadz = a * (1 - a)        dzdw = x        dzdb = 1        dedw = deda * dadz * dzdw        dedb = deda * dadz * dzdb        w = w - alpha * dedw        b = b - alpha * dedb    if _ % 100 == 0:        # 绘制动态        plt.clf()  ## 清空窗口        plt.scatter(xs, ys)        z = w * xs + b        a = 1 / (1 + np.exp(-z))  # 加入激活函数        plt.xlim(0, 1)        plt.ylim(0, 1.2)        plt.plot(xs, a)        plt.pause(0.01)  # 暂停0.01秒`