该实验作业的主要目的
- 实现一个二元分类(多元分类就是分为目标类和非目标类两类)
- 使用非线性激活函数(希望你还记得为什么,在哪里使用什么函数的原因)
- 实现前向传播和后向传播
你可能对以下文献感兴趣
- http://scs.ryerson.ca/~aharley/neural-networks/ 打不开了???
- http://cs231n.github.io/neural-networks-case-study/ 多元分类(3元)
总体进程
载入文件
显示数据集
2 获取数据集信息
线性回归显示,发现效果不好,于是我们决定使用神经网络
总述一下我们神经网络的过程
4.1获取神经网络的一些信息(超参量)
4.2 初始化参数
4.3前向传播
4.3代价函数
4.3后向传播
4.3更新参数
4.4将模块化整合
4.5预测
由于我们已经模块化了神经网络,现在我们可以快速的对网络进行修改,比如修改隐藏层的大小
我们也可以用这个网络去测试其他集
在需要写代码的前面我将提示任务目标
Updates to Assignment
If you were working on the older version:
- Please click on the "Coursera" icon in the top right to open up the folder directory.
- Navigate to the folder: Week 3/ Planar data classification with one hidden layer. You can see your prior work in version 6b: "Planar data classification with one hidden layer v6b.ipynb"
List of bug fixes and enhancements
- Clarifies that the classifier will learn to classify regions as either red or blue.
- compute_cost function fixes np.squeeze by casting it as a float.
- compute_cost instructions clarify the purpose of np.squeeze.
- compute_cost clarifies that "parameters" parameter is not needed, but is kept in the function definition until the auto-grader is also updated.
- nn_model removes extraction of parameter values, as the entire parameter dictionary is passed to the invoked functions.
Planar data classification with one hidden layer
Welcome to your week 3 programming assignment. It's time to build your first neural network, which will have a hidden layer. You will see a big difference between this model and the one you implemented using logistic regression.
You will learn how to:
- Implement a 2-class classification neural network with a single hidden layer
- Use units with a non-linear activation function, such as tanh
- Compute the cross entropy loss
- Implement forward and backward propagation
1 - Packages
Let's first import all the packages that you will need during this assignment.
- numpy is the fundamental package for scientific computing with Python.
- sklearn provides simple and efficient tools for data mining and data analysis.
- matplotlib is a library for plotting graphs in Python.
- testCases provides some test examples to assess the correctness of your functions
- planar_utils provide various useful functions used in this assignment
# Package imports
import numpy as np
import matplotlib.pyplot as plt
from testCases_v2 import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
%matplotlib inline
np.random.seed(1) # set a seed so that the results are consistent
2 - Dataset
First, let's get the dataset you will work on. The following code will load a "flower" 2-class dataset into variables X
and Y
.
X, Y = load_planar_dataset()
Visualize the dataset using matplotlib. The data looks like a "flower" with some red (label y=0) and some blue (y=1) points. Your goal is to build a model to fit this data. In other words, we want the classifier to define regions as either red or blue.
# Visualize the data:
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);
You have:
- a numpy-array (matrix) X that contains your features (x1, x2)
- a numpy-array (vector) Y that contains your labels (red:0, blue:1).
Lets first get a better sense of what our data is like.
Exercise: How many training examples do you have? In addition, what is the shape
of the variables X
and Y
?
Hint: How do you get the shape of a numpy array? (help)
获取向量XY的大小信息
一般情况下,Y应为一个列向量(m,1) m行为示例的个数,1是因为我们结果只能输出一个,无法输出多个,X为(m,nx) m个实例,每一个实例中有nx的特征值
### START CODE HERE ### (≈ 3 lines of code)
shape_X = X.shape
shape_Y = Y.shape
m = X.shape[1] # training set size
### END CODE HERE ###
print ('The shape of X is: ' + str(shape_X))
print ('The shape of Y is: ' + str(shape_Y))
print ('I have m = %d training examples!' % (m))
Expected Output:
shape of X | (2, 400) |
shape of Y | (1, 400) |
m | 400 |
3 - Simple Logistic Regression
Before building a full neural network, lets first see how logistic regression performs on this problem. You can use sklearn's built-in functions to do that. Run the code below to train a logistic regression classifier on the dataset.
# Train the logistic regression classifier
clf = sklearn.linear_model.LogisticRegressionCV();
clf.fit(X.T, Y.T);
You can now plot the decision boundary of these models. Run the code below.
# Plot the decision boundary