机器学习——前馈神经网络多分类

本次主要讲向前传播过程,每层的权重已经给出了,只需要每层加权值,然后利用激活函数运算传递给下一层,最后收口在10个值,并且计算这10个值的最大值所在的索引。

  • 读取数据和每层权重
%matplotlib inline
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt #绘图模块
from scipy.special import expit
import scipy.io as sio #用来读取matlab文件
data = sio.loadmat("CourseraML/ex3/data/ex3data1.mat") #读取数据
X, y = data["X"], data["y"] #特征和标签 
X = np.insert(X, 0, 1, axis =1) #插入截距虚拟列
#print(X.shape, y.shape) #(5000, 401) (5000, 1)

weight_data = sio.loadmat("CourseraML/ex3/data/ex3weights.mat") #权重数据
theta1, theta2 = weight_data['Theta1'], weight_data['Theta2']
#print(theta1.shape, theta2.shape) #(25, 401) (10, 26)
  • 向前传播
def forwardPropagation(mytheta1, mytheta2, myX): #定义向前传播函数
    a1 = myX #第一层,输入层
    z1 = np.dot(myX, mytheta1.T) #加权重 
    a2 = expit(z1)#第一层激活函数运算
    a2 = np.insert(a2, 0, 1, axis =1) #加常数偏置度 (5000, 26)
    z2 = np.dot(a2, mytheta2.T)  #(5000, 26)*(26,10) = (5000, 10)
    a3 = expit(z2) #第二层激活函数运算,输出层
    classes = np.argmax(a3, axis =1) #按列找出每行最大预测值的序号 
    classes = classes + 1 #偏移1个单位
    return classes
  • 模型评估
classes = forwardPropagation(theta1, theta2, X)

n_correct, n_total = 0, 0
for i in range(X.shape[0]):
    n_total +=1
    if classes[i] == y[i]:
        n_correct +=1
accuarcy = n_correct/n_total
print("The accuarcy is %0.2f%%"%(accuarcy*100))

正确输出是 The accuarcy is 97.52%

延伸阅读

机器学习——逻辑回归多分类

机器学习——逻辑回归正则(二)

机器学习——逻辑回归不正则(一)

机器学习——多元线性回归模型

机器学习——单变量线性回归模型

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zengbowengood/article/details/107498895