人工神经网络ExperimentClass Two

请添加图片描述
Experiment One:

import math
import numpy as np
def Binary_step(x):
    for i in range(len(x)):
        if x[i] < 0:
            x[i] = 0
        else:
            x[i] = 1
    return x

def Sigmoid(x):
    for i in range(len(x)):
        x[i] = 1/(1+math.exp(-x[i]))
    return x

def ReLU(x):
    x = np.maximum(x, 0)
    return x

def Softmax(x):
    ans = []
    for i in range(len(x)):
        ans.append(np.exp(x[i])/sum(np.exp(x)))
    return ans

Experiment Two:

import random
import math
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import 实验一 as p

with open('./X.txt', 'r') as fx, open('./y.txt', 'r') as fy:
    temp = fx.readlines()  # 读取 X.txt 数据
    for i in range(len(temp)):
        temp[i] = list((map(float, temp[i].split(','))))  # 将 str 转为 float
    x = np.transpose(np.array(temp))  # 将 temp 转置, 形状由 (150, 2) 转为 (2, 150)
    x_b = np.ones(len(temp))  # 加入 b 的系数, 为 1
    x = np.vstack((x, x_b))  # 在垂直方向将x, x_b拼接

    y = np.array(list(map(int, fy.readline().split(','))))  # 读取 y.txt 数据

def monitor(y, y_pred):  # 计算并输出准确度
    tol = len(y)
    cnt = 0
    for i in range(len(y)):
        if y[i] == y_pred[i]:
            cnt += 1
    return (cnt, tol, cnt/tol)

np.random.seed(16)  # 随机种子,确保每一次产生的随机数都一样,便于调试

k = 100  # 退出条件,循环100次后退出
a = 0.1  # 学习率
A = np.array(np.random.rand(3) * 10)  # 初始化系数 w1, w2, b

# 输出矩阵形状
'''
A shape:  (3,)
x shape:  (3, 150)
y shape:  (150,)
'''
print('A shape: ', A.shape)
print('x shape: ', x.shape)
print('y shape: ', y.shape)

for cnt in range(k):
    # A_(1, 3) @ x(3, 150) ==> y_pred_naive(1, 150) ==Binary step(Activation function)==> y_pred(1, 150)
    y_pred = p.Binary_step(np.transpose(A) @ x)

    # 更新系数
    # y-y_pred(1, 150) @ x_(150, 3) ==> temp(1, 3) * a + A(1, 3) ==> A_updated(1, 3)
    A = A + a * (y - y_pred) @ np.transpose(x)

    if cnt % 10 == 0:
        print(A)
        print(monitor(y, y_pred))


# -------------------------------- 绘制 3D 图形 --------------------------------

fig = plt.figure()  # 创建画布
ax = Axes3D(fig)  # 创建 3D 坐标系


ax.scatter(x[0], x[1], y)  # 绘制三维散点图
# 设置坐标轴及样式
ax.set_zlabel('Z', fontdict={'size': 15, 'color': 'red'})
ax.set_ylabel('Y', fontdict={'size': 15, 'color': 'red'})
ax.set_xlabel('X', fontdict={'size': 15, 'color': 'red'})


# -------------------------------- 分界 平面 --------------------------------

# 随机选取样本坐标, 选取样本点,用于绘制平面
random_index = [np.random.randint(0, 150) for i in range(20)]
X, Y = np.meshgrid(x[0][random_index], x[1][random_index])
# 绘制平面
ax.plot_surface(X,
                Y,
                Z= X*A[0] + Y*A[1] + A[2],
                color='r',
                alpha=0.1 # 透明度
               )
plt.show()

结果及效果
在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是丝豆呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值