import numpy as np
from torch.utils.data import DataLoader
import torchvision
from torchvision import datasets
import os
root = os.getcwd() # 获取根目录
# MNIST dataset
train_dataset = datasets.MNIST(root = root, #选择数据集的根目录
train = True, #选择训练集
transform = torchvision.transforms.ToTensor(), #转换成tensor变量
download = True) #从网络上下载图片
test_dataset = datasets.MNIST(root = root, #选择数据集的根目录
train = False, #选择测试集
transform = torchvision.transforms.ToTensor(), #转换成tensor变量
download = True) #从网络上下载图片
def _relu(in_data):
return np.maximum(0,in_data)
def sigmoid(x):
return 1/(1 + np.exp(-x))
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 forward_relu(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
def forward_sigmoid(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 = sigmoid(a1)
a2 = z1.dot(w2) + b2
z2 = sigmoid(a2)
a3 = z2.dot(w3) + b3
y = a3
return y
def accuracy(x, labels, opt):
assert opt == 'R' or opt == 'S', 'opt must R or S, R代表ReLU, S代表Sigmoid'
accuracy_cnt = 0
if(opt == 'R'):
for i in range(len(x)):
y = forward_relu(network, x[i])
p = np.argmax(y) #获取概率最高的元素的索引
if p == labels[i]:
accuracy_cnt += 1
print("ReLU_Activation Accuracy:" + str(float(accuracy_cnt) / len(x) * 100) + "%")
elif(opt == 'S'):
for i in range(len(x)):
y = forward_sigmoid(network, x[i])
p = np.argmax(y) #获取概率最高的元素的索引
if p == labels[i]:
accuracy_cnt += 1
print("Sigmoid_Activation Accuracy:" + str(float(accuracy_cnt) / len(x) * 100) + "%")
network = init_network()
# x = test_dataset.data.numpy().reshape(-1,28*28)
# labels = test_dataset.targets.numpy() #tensor 转numpy
# accuracy(x, labels, 'R')
# accuracy(x, labels, 'S')
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_relu(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) + "%")