Planar data classification with one 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
原始数据集
不同的颜色表示不同的类别,可以看到数据不是简单的线性可分类型。下面使用逻辑回归和两层神经网络分别对数据进行分类。
使用sklearn包的LogisticRegression做二分类
# Package imports
import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
import sklearn.linear_model
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
np.random.seed(1) # set a seed so that the results are consistent
X, Y = load_planar_dataset()
### START CODE HERE ### (≈ 3 lines of code)
shape_X = X.shape
shape_Y = Y.shape
m = shape_X[1] # training set size
### END CODE HERE ###
# Train the logistic regression classifier
clf = sklearn.linear_model.LogisticRegressionCV();
clf.fit(X.T, Y.T);
# Plot the decision boundary for logistic regression
plot_decision_boundary(lambda x: clf.predict(x), X, Y)
plt.title("Logistic Regression")
# Print accuracy
LR_predictions = clf.predict(X.T)
print ('Accuracy of logistic regression: %d ' % float((np.dot(Y,LR_predictions) + np.dot(1-Y,1-LR_predictions))/float(Y.size)*100) +
'% ' + "(percentage of correctly labelled datapoints)")
plt.show()
运行结果:
Accuracy of logistic regression: 47 % (percentage of correctly labelled datapoints)
这里可以看到,数据分布比较混乱,直接使用逻辑回归的准确率只有47%,还没过半,甚至不如随便猜(50%)。
构建自己的浅层神经网络
该编程作业将实现神经网络的步骤通过一个个的函数来实现,从上到下依次为:
1. 确定各层神经元个数;
2. 初始化参数;
3. 前向传播,求神经网络的输出值;
4. 计算cost;
5. 后向传播,求各参数的偏导数值;
6. 更新各参数值(使用偏导数、学习效率alpha),完成一次迭代;
7. 达到迭代次数,确定神经网络的最终参数;
8. 使用参数修正的神经网络预测样本;
9. 求准确率。
# GRADED FUNCTION: layer_sizes
def layer_sizes(X, Y):
"""
Arguments:
X -- input dataset of shape (input size, number of examples)
Y -- labels of shape (output size, number of examples)
Returns:
n_x -- the size of the input layer
n_h -- the size of the hidden layer
n_y -- the size of the output layer
"""
### START CODE HERE ### (≈ 3 lines of code)
n_x = X.shape[0] # size of input layer
n_h = 4
n_y = Y.shape[0] # size of output layer
### END CODE HERE ###
return (n_x, n_h, n_y)
# GRADED FUNCTION: initial