# 2_2Logistic Regression with a Neural Network mindset#
2_2逻辑回归在神经网络中的实现
Welcome to your first (required) programming assignment! You will build a
logistic regression classifier to recognize cats. This assignment will step
you through how to do this with a Neural Network mindset, and so will also
hone your intuitions about deep learning.
Instructions:
- 注意事项:Do not use loops (for/while) in your code, unless the instructions explicitly
ask you to do so.
#
- Build the general architecture of a learning algorithm, including:
- Initializing parameters
- Calculating the cost function and its gradient
- Using an optimization algorithm (gradient descent)
- Gather all three functions above into a main model function, in the right order.
## 1、准备实验所要用的包
numpy用于向量计算,h5py识别格式是H5 file的图片,matplotlib画图
First, let’s run the cell below to import all the packages that you will
need during this assignment.
- numpy is the fundamental package for scientific computing
with Python.
- h5py is a common package to interact with a dataset
that is stored on an H5 file.
- matplotlib is a famous library to plot graphs in Python.
- PIL and scipy
are used here to test your model with your own picture at the end.
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
get_ipython().magic(‘matplotlib inline’)
## 2 - 熟悉我们的实验数据(Overview of the Problem set)
Problem Statement: You are given a dataset (“data.h5”) containing:
- a training set of m_train images labeled as cat (y=1) or non-cat (y=0)
- a test set of m_test images labeled as cat or non-cat
- each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB).
Thus, each image is square (height = num_px) and (width = num_px).
You will build a simple image-recognition algorithm that can correctly classify pictures as cat or non-cat.
Let’s get more familiar with the dataset. Load the data by running the following code.
Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
train_dataset数据集是209个图片,每个图片都是64*64像素,3表示3种颜色,Red,Blue,Green
test_dataset数据集是50个图片,每个图片都是64*64像素,3表示3种颜色,Red,Blue,Green
We added “_orig” at the end of image datasets (train and test) because we are going to
preprocess them. After preprocessing, we will end up with train_set_x and test_set_x
(the labels train_set_y and test_set_y don’t need any preprocessing).
Each line of your train_set_x_orig and test_set_x_orig is an array representing an image.
You can visualize an example by running the following code. Feel free also to change the
index` value and re-run to see other images.
2.1显示一个不太清晰的图片
index = 5
plt.imshow(train_set_x_orig[index])
print (“y = ” + str(train_set_y[:, index]) + “, it’s a ‘” +
classes[np.squeeze(train_set_y[:, index])].decode(“utf-8”) + “’ picture.”)
plt.show()
classes是一个ndarry类型,保存2个值,一个是b’non-cat’,另一个是b’cat’
np.squeeze是去掉数组中维数为1的向量,如果不加这个函数,decode无法工作
train_set_y是一个(1,209)的一个行向量,train_set_y[:,index]意思是第index列元素的值
Many software bugs in deep learning come from having matrix/vector dimensions that don’t fit.
If you can keep your matrix/vector dimensions straight you will go a long way toward eliminating
many bugs.
Exercise: Find the values for:
- m_train (number of training examples)
- m_test (number of test examples)
- num_px (= height = width of a training image)
Remember that train_set_x_orig
is a numpy-array of shape (m_train, num_px, num_px, 3).
For instance, you can access m_train
by writing train_set_x_orig.shape[0]
.
2.2输出这几个关键参数的含义
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = train_set_x_orig.shape[1]
print (“Number of training examples: m_train = ” + str(m_train))#m_train = 209
print (“Number of testing examples: m_test = ” + str(m_test))#m_test = 50
print (“Height/Width of each image: num_px = ” + str(num_px))#num_px = 64
print (“Each image is of size: (” + str(num_px) + “, ” + str(num_px) + “, 3)”)#(64, 64, 3)
print (“train_set_x shape: ” + str(train_set_x_orig.shape))#(209, 64, 64, 3)
print (“train_set_y shape: ” + str(train_set_y.shape))#(1, 209),行向量
print (“test_set_x shape: ” + str(test_set_x_orig.shape))#(50, 64, 64, 3)
print (“test_set_y shape: ” + str(test_set_y.shape))#(1, 50),行向量