该实验作业的主要目的
- 通过该实验指导完成对猫图片的识别
- 在此过程中需要a初始化参量b计算代价函数c使用连续梯度算法
你可能对以下参考文献感兴趣
http://www.wildml.com/2015/09/implementing-a-neural-network-from-scratch/ 这是一篇从头开始搭建神经网络的文章,可以更好的帮助你理解神经网络的细节,该文章有github的同步文件,你也可以去看看github上同步实现操作
https://stats.stackexchange.com/questions/211436/why-normalize-images-by-subtracting-datasets-image-mean-instead-of-the-current 这是一个论坛,可以帮助你理解我们之前对图片进行标准化的原因
总体进程
载入文件
2加载数据集,并进行大小预处理
学习算法的一般结构 a初始化模型的参数b学习参数c使用参数进行预测d分析结果得出结论
现在开始我们搭建自己的神经网络
4.1 辅助函数 sigmoid
4.2 初始化模型参数 initialize_with_zeros(dim)
4.3 前向和后向传播 propagate(w, b, X, Y) 注意,本次中我们仅1层网络(没有掩蔽层),因此前向传播和后向传播写在一起,一般情况下,前向传播和后向传播是分开写的,这将在下周或者下下周的代码中有所体现
4.4 训练参数optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False)
4.4 预测结果 predict(w, b, X)
5 整合神经网络的操作 model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False)
现在你已经完成了作业,为了更好的理解过程(正如机器学习那门课一样)我们先来看梯度下降曲线
更改我们的学习率,当太大会出现折叠,太小下降缓慢
测试我们自己的其他图片
在需要写代码的前面我将提示任务目标
Logistic Regression with a Neural Network mindset
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.
You will learn to:
- 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.
Updates
This notebook has been updated over the past few months. The prior version was named "v5", and the current versionis now named '6a'
If you were working on a previous version:
- You can find your prior work by looking in the file directory for the older files (named by version name).
- To view the file directory, click on the "Coursera" icon in the top left corner of this notebook.
- Please copy your work from the older versions to the new version, in order to submit your work for grading.
List of Updates
- Forward propagation formula, indexing now starts at 1 instead of 0.
- Optimization function comment now says "print cost every 100 training iterations" instead of "examples".
- Fixed grammar in the comments.
- Y_prediction_test variable name is used consistently.
- Plot's axis label now says "iterations (hundred)" instead of "iterations".
- When testing the model, the test image is normalized by dividing by 255.
1 - Packages
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
%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()
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.
# Example of a picture
index = 25
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.")
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.