1.图像预处理
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
def load_dataset(): #在相应路径下读取数据
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
#读取训练集和测试集的维数,用来进行数据集预处理
m_train=train_set_x_orig.shape[0] #样本数
m_test=test_set_x_orig.shape[0] #测试集数量
num_px=train_set_x_orig.shape[1] #图像是正方形形式,所以读取样本像素的行或列就可以了(1 or 2)
print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("Height/Width of each image: num_px = " + str(num_px))
print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)")
print ("train_set_x shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
#进行矩阵重塑,将像素值统一作为矩阵的列来排放,类似于X=[x1,x2,x3,x4.......xm]
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
train_set_x_flatten=train_set_x_orig.reshape(train_set_x_orig.shape[0],-1).T
test_set_x_flatten=test_set_x_orig.reshape(test_set_x_orig.shape[0],-1).T
print(train_set_x_orig.shape) #打印矩阵的维数,发现从三维矩阵成功转换为一维矩阵
print(train_set_x_flatten.shape)
#将像素值全部除以255,把颜色强度标准化
train_set_x=train_set_x_flatten/255
test_set_x=test_set_x_flatten/255
train_set_x_orig:未经处理的训练样本的像素初值
train_set_y_orig:训练样本所对应的真值 (0 or 1)
test_set_x_orig:训练好w和b的值后用来测试的样本
test_set_y_orig:对应的真值(需要将模拟的真值与其比较,得出最优算法)
这里矩阵重塑就是将矩阵重塑为以像素值作为一列,样本数量确定列数的矩阵(具体方法可以参考吴恩达的视频)
预处理完毕后,我们会得到(12288(三通道像素值总个数),209(样本数量))的rgb三色分布在(0,1)的样本矩阵。
2.算法的一般架构
对于一个样本:
然后通过训练所有的样本并进行求和计算cost函数
理解了算法部分,就该进行代码实战了
3.编写代码
编写代码分为三步:
1.定义模型的结构
2.初始化模型参数
3.循环进行参数迭代修正
1.定义模型结构
(i)定义sigmoid函数:
def sigmoid(z):
s=1/(1+np.exp(-z))
return s
前篇有讲过numpy的优势,可参考(36条消息) 深度学习笔记(二):逻辑回归的理解_fyjyyds的博客-CSDN博客https://blog.csdn.net/fyjyyds/article/details/118935150?spm=1001.2014.3001.5501详情恕不赘述
(ii)定义参数初始化函数:
def initialize_with_zeros(dim):
w = np.zeros((dim, 1)) #定义一个维数为(dim,1)的矩阵
b = 0
assert (w.shape == (dim, 1)) #assert函数确保矩阵维数正确
assert (isinstance(b, float) or isinstance(b, int))
return w, b
(iii)定义正向反向传播函数:
这里只给出较关键参数算法
此处参数与上文一致
def propagate(w, b, X, Y):
#w和b是sigmoid函数参数
#X,Y分别是训练样本集和对应的真值集
m = X.shape[1]
# 求出cost函数
A = sigmoid(np.dot(w.T, X) + b)
cost = -1 / m * np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A))
# 求导
dw = np.dot(X, (A - Y).T) / m
db = np.sum(A - Y) / m
assert (dw.shape == w.shape)
assert (db.dtype == float)
cost = np.squeeze(cost)
grads = {"dw": dw,
"db": db}
return grads, cost
返回的grads中保存了dw矩阵(里面的元素是cost函数对w1,w2......的求导)和db(cost对b参数的求导)
有了dw和db后,就可以在循环中一遍一遍的优化w和b参数,得到损失最小的cost函数
(iv)定义参数优化函数:
num_iterations为迭代次数
learning_rate为dw和db的权重
print_cost:是否打印cost的值
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
costs = []
for i in range(num_iterations):
#计算cost函数,得到dw,db
grads, cost = propagate(w, b, X, Y)
#设置dw,db参数
dw = grads["dw"]
db = grads["db"]
#进行迭代优化参数
b = b - learning_rate * db
w = w - dw * learning_rate
#每遍历一百遍打印损失
if i % 100 == 0:
costs.append(cost)
if print_cost and i % 100 == 0:
print("Cost after iteration %i: %f" % (i, cost))
#返回最终的w,b值
params = {"w": w,
"b": b}
grads = {"dw": dw,
"db": db}
return params, grads, costs
(v)猜测图像含义函数:
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
#用优化过的w和b进行真值计算
A = sigmoid(np.dot(w.T, X) + b)
#对算出来的真值进行二值化,分为0和1
for i in range(A.shape[1]):
#
if (A[0, i] >= 0.5):
Y_prediction[0, i] = 1
else:
Y_prediction[0, i] = 0
assert (Y_prediction.shape == (1, m))
return Y_prediction
最后只要将这些模块整合起来,就可以使用神经网络进行图像遍历并猜测图像含义了
def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
w, b = initialize_with_zeros(X_train.shape[0])
#求出参数
params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
#导入参数
w = params["w"]
b = params["b"]
# 猜测
Y_prediction_test = predict(w, b, X_test)
Y_prediction_train = predict(w, b, X_train)
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))
d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train": Y_prediction_train,
"w": w,
"b": b,
"learning_rate": learning_rate,
"num_iterations": num_iterations}
return d
将自己的参数带入即可
ps:我们还可以通过画图的方式优化learning_rate的选值
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()
多试几次就可以得到较理想的learning_rate啦