神经网络02
2020-09-17 本文已影响0人
平头哥2
softmax 函数
import numpy as np
def sigmoid(x):
return 1/(1 + np.exp(-x))
def identity_function(x):
return x
def softmax(a):
c = np.max(a) # 为了方式溢出,取信号的最大值
exp_a = np.exp(a-c)
sum_exp_a = np.sum(exp_a)
y = exp_a /sum_exp_a
return y
a = np.array([1,2,3,4,5])
print(softmax(a)) # [0.01165623 0.03168492 0.08612854 0.23412166 0.63640865]
显示图像
import sys, os
print(os.getcwd())
sys.path.append(os.getcwd())
from mnist import load_mnist
import numpy as np
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
print(x_train.shape) # (60000, 784)
print(t_train.shape) # (60000,)
print(x_test.shape) # (10000, 784)
print(t_test.shape) # (10000,)
from PIL import Image
def img_show(img):
pil_img = Image.fromarray(np.uint8(img))
pil_img.show()
img = x_train[0]
label = t_train[0]
print(label) # 5
print(img.shape) # (784,)
img = img.reshape(28,28)
print(img.shape) # (28, 28)
img_show(img) # 图像显示就是5
神经网络推理处理
import sys, os
print(os.getcwd())
sys.path.append(os.getcwd())
from mnist import load_mnist
import numpy as np
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
# t_train 一维数组 [5 0 4 ... 5 6 8] 长度:60000
# t_test 一维数组 [7 2 1 ... 4 5 6]
# x_train 二维数组 维度值: (60000, 784) 60000行, 784 列
# print(x_train[59999]) 第60000行的值
# x_test 二维数组 维度值: (10000, 784)
train_size = x_train.shape[0] # (60000, 784) 的第一个元素 60000
batch_size = 10;
#
batch_mask = np.random.choice(train_size, batch_size)
print(batch_mask) # [28259 6378 19047 4077 55177 24777 25800 17717 7067 32450]
def get_data():
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
return x_test, t_test
import pickle
def init_network():
with open("sample_weight.pkl", "rb") as f:
network = pickle.load(f)
return network
def sigmoid(x):
return 1/(1 + np.exp(-x))
def identity_function(x):
return x
def softmax(a):
c = np.max(a) # 为了方式溢出,取信号的最大值
exp_a = np.exp(a-c)
sum_exp_a = np.sum(exp_a)
y = exp_a /sum_exp_a
return y
def predict(network, X):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
A1 = np.dot(X, W1) + b1
Z1 = sigmoid(A1)
A2 = np.dot(Z1, W2) + b2
Z2 = sigmoid(A2)
A3 = np.dot(Z2, W3) + b3
Y = softmax(A3)
return Y
x, t = get_data()
network = init_network()
accuracy_cnt = 0
for i in range(len(x)):
y = predict(network, x[i])
p = np.argmax(y) # 获取概率最高的元素的索引
if p == t[i]:
accuracy_cnt += 1
print(accuracy_cnt) # 9207
print (len(x)) # 10000
print("accuracy:" + str(float(accuracy_cnt) / len(x)))# accuracy:0.9207
# 批处理 -- 替换上面最后那一部分代码
x, t = get_data()
network = init_network()
accuracy_cnt = 0
batch_size = 100
for i in range(0, len(x), batch_size):
x_batch = x[i: i+batch_size]
y_batch = predict(network, x_batch)
p = np.argmax(y_batch, axis=1) # 获取概率最高的元素的索引
accuracy_cnt += np.sum(p == t[i: i+batch_size])
print("accuracy:" + str(float(accuracy_cnt) / len(x)))# accuracy:0.9207