步骤
1.加载数据集
2.使用ReLU激活函数使得范围在【0,1】之间
3.初始化网络
4.运用前向传播函数forward
代码
import torch
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import numpy as np
def init_network():
network ={}
weight_scale =1e-3
network['W1'] = np.random.randn(784,50) * weight_scale
network['b1'] = np.ones(50)
network['W2'] = np.random.randn(50, 100) * weight_scale
network['b2'] = np.ones(100)
network['W3'] = np.random.randn(100, 10) * weight_scale
network['b3'] = np.ones(10)
return network
def _relu(x):
return np.maximum(0,x)
def forward(network,x):
w1,w2,w3 =network['W1'],network['W2'],network['W3']
b1,b2,b3 = network['b1'],network['b2'],network['b3']
a1 = x.dot(w1) + b1
z1=_relu(a1)
a2 = z1.dot(w2) + b2
z2 = _relu(a2)
a3 = z2.dot(w3) + b3
y = a3
return y
if __name__ == '__main__':
train_dataset = dsets.MNIST(root='/ml/pymnist', # 选择数据的根目录
train=True, # 选择训练集
transform=None, # 不考虑使用任何数据预处理
download=True) # 从网络上download图片
test_dataset = dsets.MNIST(root='/ml/pymnist', # 选择数据的根目录
train=False, # 选择测试集
transform=None, # 不考虑使用任何数据预处理
download=True) # 从网络上download图片
network = init_network()
accuracy_cnt = 0
x = test_dataset.data.numpy().reshape(-1,28*28)
labels = test_dataset.targets.numpy()
for i in range(len(x)):
y = forward(network,x[i])
p = np.argmax(y)
if p == labels[i]:
accuracy_cnt+=1
print("Accuracy:" + str(float(accuracy_cnt)/len(x)*100)+"%")
结果
代码优化(批处理)
import torch
from torch.utils.data import DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import numpy as np
def init_network():
network ={}
weight_scale =1e-3
network['W1'] = np.random.randn(784,50) * weight_scale
network['b1'] = np.ones(50)
network['W2'] = np.random.randn(50, 100) * weight_scale
network['b2'] = np.ones(100)
network['W3'] = np.random.randn(100, 10) * weight_scale
network['b3'] = np.ones(10)
return network
# def _relu(x):
# return np.maximum(0,x)
def forward(network,x):
w1,w2,w3 =network['W1'],network['W2'],network['W3']
b1,b2,b3 = network['b1'],network['b2'],network['b3']
a1 = x.dot(w1) + b1
z1=_softmax(a1)
a2 = z1.dot(w2) + b2
z2 = _softmax(a2)
a3 = z2.dot(w3) + b3
y = a3
return y
def _softmax(x):
if x.ndim ==2:
c = np.max(x,axis=1)
x = x.T -c #溢出对策
y = np.exp(x)/np.sum(np.exp(x),axis=0)
return y.T
c = np.max(x)
exp_x = np.exp(x-c)
return exp_x / np.sum(exp_x)
if __name__ == '__main__':
train_dataset = dsets.MNIST(root='/ml/pymnist', # 选择数据的根目录
train=True, # 选择训练集
transform=None, # 不考虑使用任何数据预处理
download=True) # 从网络上download图片
test_dataset = dsets.MNIST(root='/ml/pymnist', # 选择数据的根目录
train=False, # 选择测试集
transform=None, # 不考虑使用任何数据预处理
download=True) # 从网络上download图片
network = init_network()
accuracy_cnt = 0
batch_size = 100
x = test_dataset.data.numpy().reshape(-1,28*28)
labels = test_dataset.targets.numpy()
for i in range(0,len(x),batch_size):
x_batch = x[i:i+batch_size]
y_batch = forward(network,x_batch)
p = np.argmax(y_batch,axis=1)
accuracy_cnt+=np.sum(p==labels[i:i+batch_size])
print("Accuracy:" + str(float(accuracy_cnt)/len(x)*100)+"%")
softmax和ReLU区别
ReLU一般处理二分类问题
softmax一般处理三分类以上的问题