1、神经网络介绍
基础依然是线性分类,通过激活函数引入了非线性的成分,使其能拟合任意的函数,通过反向传播自动寻找合适的线性分类器的参数
1.1、前向计算误差:
1.2、反向传播
1)更新W5参数
求导,链式法则
2)更新W1参数
1.3、基础代码
import numpy as np
import h5py
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
def predict(w, b, X):
Y_prediction = np.zeros((1, X.shape[1]))
w = w.reshape(X.shape[0], 1) #(12288, 1)
# 计算结果
A = 1 / (1 + np.exp(-(np.dot(w.T, X) + b)))
for i in range(A.shape[1]):
if A[0, i] <= 0.5:
Y_prediction[0, i] = 0
else:
Y_prediction[0, i] = 1
return Y_prediction
# 1、读取样本数据
train_x, train_y, test_x, test_y, classes = load_dataset()
# print("训练集的样本数: ", train_x.shape[0]) #209
# print("测试集的样本数: ", test_x.shape[0]) #50
# print("train_x形状: ", train_x.shape) #(209, 64, 64, 3)
# print("train_y形状: ", train_y.shape) #(1, 209)
# print("test_x形状: ", test_x.shape) #(50, 64, 64, 3)
# print("test_x形状: ", test_y.shape) #(1, 50)
# 输入数据的形