栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

人工神经网络ExperimentClass Two

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

人工神经网络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()

结果及效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772383.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号