1、加载包和数据
__author__ = ‘Qian Chenglong‘ import numpy as np import matplotlib.pyplot as plt import h5py import scipy from scipy import ndimage from lr_utils import load_dataset #load data train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
2、处理数据
1)看一下数据的形状
#get she shape of thed data print(train_set_x_orig.shape) print(train_set_y.shape) print(test_set_x_orig.shape) print(test_set_y.shape) print(classes.shape)
2)获取训练数据个数和测试数据个数,图片是64*64*3的格式
#the number of data,train number is 209 test number is50 m_train = train_set_x_orig.shape[0] m_test = test_set_x_orig.shape[0] #the picture‘s row * col =64*64 channel=3 num_px = train_set_x_orig.shape[1]
3)重构数据形状
A trick when you want to flatten a matrix X of shape (a,b,c,d) to a matrix X_flatten of shape (b∗c∗d, a) is to use:
X_flatten = X.reshape(X.shape[0], -1).T # X.T is the transpose of X
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
4)标准化(归一化)处理数据,因为图片的数据都是0~255的所以我们直接/255
# standardize our dataset. train_set_x = train_set_x_flatten/255. test_set_x = test_set_x_flatten/255.
5)定义要使用的函数
sigmoid函数在这里被称为激活函数。 ??????????????(z)=1+(1+exp(-z))
def sigmoid(z): s=1/(1+np.exp(-z)) return s
初始化函数(注意,在多神经元的神经网络中,w必须采用随机初始化,b可以采用zero初始化)
#dim: the number of features def initialize_with_zeros(dim): w=np.zeros((dim,1)) b=0 assert (w.shape == (dim, 1)) #assert()判断()条件是否为真,为假会报错 assert (isinstance(b, float) or isinstance(b, int)) return w,b
传播函数:(loss函数选择的交叉熵函数??(??)log(??(??))+(1−??(??))log(1−??(??))))
原文:https://www.cnblogs.com/long5683/p/10800472.html