用神经网络识别猫——一个简单的逻辑回归代码实现
程序员文章站
2022-06-15 13:54:05
...
本文是我基于对https://blog.csdn.net/u013733326/article/details/79639509的理解完成的,也就是吴恩达老师的第一个编程作业
要识别猫的话,我们要构建的一个神经网络需要达到的功能是:
输入一张猫的图片,然后输入一个概率,即这张图片是猫的概率
# 算法框架:
# 我们的最终目的是学习w和b,而学习w和b实际就是优化w和b,
# 优化w和b的方法就是梯度下降
# 梯度下降要用到的参数是w, b, X, Y, num_iterations, learning_rate
import numpy as np
from lr_utils import load_dataset
import matplotlib.pyplot as plt
def sigmoid(z):# 此函数没问题
s = 1 / (1 + np.exp(-z))
return s
# 传播函数(包括正向传播和反向传播)
def propagate(m, w, b, X, Y):
# 正向传播
z = np.dot(w.T, X) + b
a = sigmoid(z)
cost = (-1 / m) * np.sum(Y * np.log(a) + (1 - Y) * np.log(1 - a))
cost = np.squeeze(cost)
assert (cost.shape == ())
# 反向传播
dw = (1 / m) * np.dot(X, (a - Y).T)
db = (1 / m) * np.sum(a - Y)
return (dw, db, cost)
# 梯度下降函数
# 假定:
# 函数中提到的图像矩阵X为m列的矩阵,每一列表示一个平坦的图像向量
# 而a - Y为预测值-真实值,得到的矩阵为一个一行m列的矩阵
# 此函数的参数w和b需要初始化,X和Y需要从文件中加载及预处理
def gradient_discent(w, b, X, Y, num_iterations, learning_rate):
m = X.shape[1]
costs = []
for i in range(num_iterations):
dw, db, cost = propagate(m, w, b, X, Y)
# 更新参数
w = w - learning_rate * dw
b = b - learning_rate * db
if i % 100 == 0:
costs.append(cost)
return w, b, costs
# 平铺矩阵
def flatten(data):
return data.reshape(data.shape[0],-1).T
# 初始化参数
def initialize_params(dim):
return np.zeros(shape = (dim, 1))
# 预测函数
def predict(w, b, test_x):
y = np.dot(w.T, test_x) + b
print(sigmoid(y))
# 整合的模型
def model(learning_rate = 0.005):
# 加载并处理训练集和测试集
train_set_x, train_set_y, test_set_x, test_set_y, classes = load_dataset()
X_train = flatten(train_set_x)
X_test = flatten(test_set_x)
X_train = X_train / 255
X_test = X_test / 255
# 初始化w和b
w = initialize_params(X_train.shape[0])
b = 0
# 使用断言来确保初始化的参数是我们想要的
assert(w.shape == (X_train.shape[0], 1))
assert(isinstance(b, float) or isinstance(b, int))
w, b, costs = gradient_discent(w, b, X_train, train_set_y, num_iterations = 1500, learning_rate = learning_rate)
# 将结果打包返回
res = {
'w': w,
'b': b,
'costs': costs
}
return res
if __name__ == '__main__':
learning_rates = [0.01, 0.001, 0.0001]
modelsRes = {}
for learning_rate in learning_rates:
modelsRes[str(learning_rate)] = model(learning_rate)
plt.plot(np.squeeze(modelsRes[str(learning_rate)]['costs']), label = str(learning_rate))
plt.legend(loc = 'upper center', shadow = True)
plt.xlabel('迭代次数(以百次计算)')
plt.ylabel('成本函数值')
plt.show()
# 利用训练好的w和b进行预测
w = modelsRes['0.001']['w']
b = modelsRes['0.001']['b']
train_set_x, train_set_y, test_set_x, test_set_y, classes = load_dataset()
plt.imshow(test_set_x[0])
plt.show()
plt.imshow(test_set_x[1])
plt.show()
plt.imshow(test_set_x[2])
plt.show()
test_set_x = flatten(test_set_x)
test_set_x = test_set_x / 255
print('测试集图片是猫的概率为:')
predict(w, b, test_set_x)
占个位置,具体的代码解释有时间了再补上:):):)