使用程序设计流程图解析并建立神经网络(不依赖深度学习library)

介绍:

Flow chart for a simple neural network:

#(1)Take inputs 输入

#(2)Add bias (if required)

#(3)Assign random weights to input features 随机一个权重

#(4)Run the code for training. 训练集训练

#(5)Find the error in prediction. 找预测损失

#(6)Update the weight by gradient descent algorithm. 根据梯度下降更新权重

#(7)Repeat the training phase with updated weights. 重复训练更新权重

#(8)Make predictions. 做预测

参考: 深度学习使用python建立最简单的神经元neuron-CSDN博客

数据:

python 复制代码
# Import the required libraries
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

# Load the data
df = pd.read_csv('Lesson44-data.csv') 
df

一、

python 复制代码
# Separate the features and label
x = df[['Glucose','BloodPressure']]#特征值
y = df['Outcome']#标签

三、

python 复制代码
np.random.seed(10)#初始化
label = y.values.reshape(y.shape[0],1)
weights = np.random.rand(2,1)#随机一个权重
bias = np.random.rand(1)
learning_rate = 0.0000004#梯度下降步长
epochs = 1000 #迭代次数

四~七、

python 复制代码
# Define the sigmoid function
def sigmoid(input):    
    output = 1 / (1 + np.exp(-input))
    return output



# Define the sigmoid derivative function基于sigmoid导数
def sigmoid_derivative(input):
    return sigmoid(input) * (1.0 - sigmoid(input))




def train_network(x,y,weights,bias,learning_rate,epochs):  #Epochs. 来回 One Epoch is when an ENTIRE dataset is passed forward and backward through the neural network only ONCE.
    j=0                                                    #weights 权重
    k=[]                                                   #learning_rate梯度下降的步长
    l=[]
    for epoch in range(epochs):       
        dot_prod = np.dot(x, weights) + bias#np.dot矩阵乘积
        # using sigmoid
        preds = sigmoid(dot_prod)
        # Calculating the error
        errors = preds - y  #计算错误,预测-实际
        # sigmoid derivative
        deriva_preds = sigmoid_derivative(preds)
        deriva_product = errors * deriva_preds
        #update the weights
        weights = weights -  np.dot(x.T, deriva_product) * learning_rate
        loss = errors.sum()
        j=j+1
        k.append(j)
        l.append(loss)
        print(j,loss)
    for i in deriva_product:
        bias = bias -  i * learning_rate
    plt.plot(k,l)
    return weights,bias

weights_final, bias_final = train_network(x,label,weights,bias,learning_rate,epochs)

八、

python 复制代码
weights_final
'''结果:
array([[ 0.06189634],
       [-0.12595182]])
'''

bias_final
#结果:array([0.633647])

# Prediction
inputs = [[101,76]]
dot_prod = np.dot(inputs, weights_final) + bias_final
preds = sigmoid(dot_prod) >= 1/2
preds
#结果:array([[False]])

inputs = [[137,40]]
dot_prod = np.dot(inputs, weights_final) + bias_final
preds = sigmoid(dot_prod) >= 1/2
preds
#结果:array([[ True]])
相关推荐
zy_destiny2 分钟前
【工业场景】用YOLOv8实现抽烟识别
人工智能·python·算法·yolo·机器学习·计算机视觉·目标跟踪
(●—●)橘子……18 分钟前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
闲看云起20 分钟前
Bert:从“读不懂上下文”的AI,到真正理解语言
论文阅读·人工智能·深度学习·语言模型·自然语言处理·bert
nueroamazing1 小时前
PPT-EA:PPT自动生成器
vue.js·python·语言模型·flask·大模型·项目·ppt
韩曙亮1 小时前
【自动驾驶】自动驾驶概述 ⑨ ( 自动驾驶软件系统概述 | 预测系统 | 决策规划 | 控制系统 )
人工智能·机器学习·自动驾驶·激光雷达·决策规划·控制系统·预测系统
一壶浊酒..1 小时前
python 爬取百度图片
开发语言·python·百度
该用户已不存在1 小时前
工具用得好,Python写得妙,9个效率工具你值得拥有
后端·python·编程语言
广龙宇1 小时前
【一起学Rust · 项目实战】使用getargs库来获取命令行参数
开发语言·python
信息快讯2 小时前
【机器学习赋能的智能光子学器件系统研究与应用】
人工智能·神经网络·机器学习·光学
程序员大雄学编程2 小时前
「机器学习笔记14」集成学习全面解析:从Bagging到Boosting的Python实战指南
笔记·机器学习·集成学习