介绍:
## 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博客
数据:
# 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
一、
# Separate the features and label x = df[['Glucose','BloodPressure']]#特征值 y = df['Outcome']#标签
三、
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 #迭代次数
四~七、
# 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)
八、
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]])
猜你喜欢
- 17天前(a级景区评定机构)全国A级旅游景区创建与提升培训班在敦煌市举办
- 17天前(fender japan hybrid)Fender东京旗舰店盛大开幕在即,开售商品和店内服务提前揭晓
- 17天前(四川推进世界重要旅游目的地建设工作)四川推进世界重要旅游目的地建设
- 17天前(艾美酒店连锁)艾美酒店全球夏日计划回归,联手Wishbone主厨推出创新冰饮
- 17天前(福朋喜来登酒店宴会厅)福朋喜来登品牌亮相北部湾城市群 阳江中心福朋喜来登酒店开业
- 17天前(甘肃文旅项目)甘肃省文旅产业链招商引资推介会在天水成功举办
- 17天前(马尔代夫华尔道夫酒店多少钱)Chef Zhao就任马尔代夫伊挞富士岛华尔道夫酒店Li Long中餐厅新主厨
- 17天前(岭南东方大酒店)粤西成势 | 阳江阳春长兴岭南东方酒店正式签约,粤西文旅再添明珠
- 17天前(内蒙古交通旅游图)内蒙古着力提升交通与旅游服务水平
- 17天前(泸沽湖大酒店地址)泸沽湖岚岳酒店盛大开业|以摩梭文化为魂,打造高端度假新地标
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章