这个是别人写的代码,我看了看,抄了抄,不过对其中有问题的可以问我
import numpy as np
import matplotlib.pyplot as plt
import h5py
import torch
from lr_utils import load_dataset
def dataset_processing():
train_img, train_label, test_img, test_label, classes = load_dataset()
# 显示图片信息
# index = 0
# plt.imshow(train_img[index])
# plt.show()
# 查看图片的维度等信息
# train_img (m_train,num_px,num_px,3)train_img.shape
# (209, 64, 64, 3)
# 将(64,64,3)的numpy数组重新构造为(64 x 64 x 3,1)的数组
# 构造成209行的数据,然后再进行转置,即为209列数据
train_img_flatten = train_img.reshape(train_img.shape[0], -1).T
test_img_flatten = test_img.reshape(test_img.shape[0], -1).T
# 将图像进行预处理,居中和标准化
train_img_x = train_img_flatten / 255
test_img_x = test_img_flatten / 255
return train_img_x, train_label,test_img_x,test_label,classes
def sigmoid(z):
s = 1 / (1 + np.exp(-z))
return s
def init_with_para(dim):
# w = np.random.rand(dim, 1)
w = np.zeros(shape=(dim, 1))
b = 0
# 使用断言来确保我要的数据是正确的
assert (w.shape == (dim, 1)) # w的维度是(dim,1)
assert (isinstance(b, float) or isinstance(b, int)) # b的类型是float或者是int
return w, b
def propagate(w,b,X,Y):
"""
实现前向和后向传播的成本函数及其梯度。
参数:
w - 权重,大小不等的数组(num_px * num_px * 3,1)
b - 偏差,一个标量
X - 矩阵类型为(num_px * num_px * 3,训练数量)
Y - 真正的“标签”矢量(如果非猫则为0,如果是猫则为1),矩阵维度为(1,训练数据数量)
返回:
cost- 逻辑回归的负对数似然成本
dw - 相对于w的损失梯度,因此与w相同的形状
db - 相对于b的损失梯度,因此与b的形状相同
"""
m = X.shape[1]
# 正向传播
A = sigmoid((np.dot(w.T, X))) + b
cost = (- 1 / m) * np.sum(Y * np.log(A + eps) + (1 - Y) * (np.log(1 - A + eps))) #计算成本,请参考公式3和4。
# cost = torch.nn.NLLLoss2d(A)
# 反向传播
dw = (1 / m) * np.dot(X, (A - Y).T)
db = (1 / m) * np.sum(A - Y)
assert (dw.shape == w.shape)
assert (db.dtype == float)
cost = np.squeeze(cost)
assert (cost.shape == ())
grads = {
"dw": dw,
"db": db
}
return grads,cost
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):
"""
此函数通过运行梯度下降算法来优化w和b
参数:
w - 权重,大小不等的数组(num_px * num_px * 3,1)
b - 偏差,一个标量
X - 维度为(num_px * num_px * 3,训练数据的数量)的数组。
Y - 真正的“标签”矢量(如果非猫则为0,如果是猫则为1),矩阵维度为(1,训练数据的数量)
num_iterations - 优化循环的迭代次数
learning_rate - 梯度下降更新规则的学习率
print_cost - 每100步打印一次损失值
返回:
params - 包含权重w和偏差b的字典
grads - 包含权重和偏差相对于成本函数的梯度的字典
成本 - 优化期间计算的所有成本列表,将用于绘制学习曲线。
提示:
我们需要写下两个步骤并遍历它们:
1)计算当前参数的成本和梯度,使用propagate()。
2)使用w和b的梯度下降法则更新参数。
"""
costs = []
for i in range(num_iterations):
grads, cost = propagate(w,b,X,Y)
dw = grads["dw"]
db = grads["db"]
w = w - learning_rate * dw
b = b - learning_rate * db
if i % 100 == 0:
costs.append(cost)
if (print_cost) and (i % 100 == 0):
print("迭代次数:%i,cost:%f"%(i, cost))
params = {
"w":w,
"b":b
}
grads = {
"dw": dw,
"db": db
}
return params, grads,costs
def predict(w,b,X):
"""
使用学习逻辑回归参数logistic (w,b)预测标签是0还是1,
参数:
w - 权重,大小不等的数组(num_px * num_px * 3,1)
b - 偏差,一个标量
X - 维度为(num_px * num_px * 3,训练数据的数量)的数据
返回:
Y_prediction - 包含X中所有图片的所有预测【0 | 1】的一个numpy数组(向量)
"""
m = X.shape[1] # 图片的数量
Y_prediction = np.zeros((1, m))
w = w.reshape(X.shape[0], 1)
# 计预测猫在图片中出现的概率
A = sigmoid(np.dot(w.T, X) + b)
for i in range(A.shape[1]):
# 将概率a [0,i]转换为实际预测p [0,i]
Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0
# 使用断言
assert (Y_prediction.shape == (1, m))
return Y_prediction
num_iterations = 20000
learning_rate = 0.01
eps = 1e-5
if __name__ == '__main__':
train_img, train_label, test_img, test_label, classes = dataset_processing()
w,b = init_with_para(train_img.shape[0])
parameters,grads,costs = optimize(w,b,train_img,train_label,num_iterations,learning_rate,True)
w, b = parameters["w"], parameters["b"]
test_img_predict = predict(w,b,test_img)
train_img_predict = predict(w, b, train_img)
print("测试集准确性:", format(100 - np.mean(np.abs(test_img_predict - test_label)) * 100), "%")
print("训练集准确性:", format(100 - np.mean(np.abs(train_img_predict - train_label)) * 100), "%")