吴恩达教授的深度学习类视频非常棒,小弟近期也在潜心钻研(ps 本人是公务员),本科、硕士都是学习的工科,在职场混了这么多年,依然怀念那个写代码的时代,加上近期深度学习非常热门,因此就好好看看神经网络,准备做个小测试来看看编程水平还剩多少。
import numpy as np
import h5py
import matplotlib.pyplot as plt
#定义sigmoid函数及其导函数
def sigmoid(x):
return 1.0/(1 + np.exp(-x))
def D_sigmoid(x):
return (1 - sigmoid(x)) * sigmoid(x)
#定义g(x)及其导函数
def g(x):
return np.tanh(x)
def D_g(x):
return 1 - g(x) * g(x)
#定义ReLU(x)函数及其导函数
def ReLU(x):
x[x < 0] = 0
return x
def D_ReLU(x):
x[x < 0] = 0
x[x >= 0] = 1
return x
这部分代码就是定义三个数学函数,sigmoid(x)、tanh(x) 、ReLU(x)及其导函数的公式,没什么可聊的。
def GetLoss(y, y_hat):
p,q = np.shape(y)
return - np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat)) / (p * q)
#从h5文件中倒入图像矩阵
def load_dataset():
train_dataset = h5py.File('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('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&#