通过多层神经网络来预测图片二分类,训练数据为h5文件,测试数据为jpg
参考:
https://www.cnblogs.com/xushu/p/8686538.html
理解内容转至:
https://blog.csdn.net/Koala_Tree/article/details/78092337
https://blog.csdn.net/Hansry/article/details/78406225?fps=1&locationNum=5
https://blog.csdn.net/qq_25436597/article/details/78858588
multilayer.py
输入:
'''
Created on 2018年7月19日
@author: hcl
'''
import numpy as np
import h5py
import matplotlib.pyplot as plt
import time
import scipy
from PIL import Image
from scipy import ndimage
np.random.seed(1)
#sigmoid
def sigmoid(Z):
A = 1/(1+np.exp(-Z))
cache = Z
return A, cache
#relu
def relu(Z):
A = np.maximum(0,Z)
cache = Z
return A, cache
#sigmoid求导
def sigmoid_backward(dA, cache):
Z = cache
s = 1/(1+np.exp(-Z))
dZ = dA * s * (1-s)
return dZ
#relu求导
def relu_backward(dA, cache):
Z = cache
dZ = np.array(dA, copy=True)
dZ[Z <= 0] = 0
return dZ
#初始化参数
def initialize_parameters_deep(layer_dims):
np.random.seed(1)
parameters = {}
L = len(layer_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) / np.sqrt(layer_dims[l-1]) #初始化为随机值
parameters['b' + str(l)] = np.zeros((layer_dims[l], 1)) #初始化为0
return parameters
#前向传播中的线性函数
def linear_forward(A, W, b):
Z = np.dot(W, A) + b
cache = (A, W, b)
return Z,