《深度学习——Andrew Ng》第一课第三周编程作业

本文介绍了如何使用单隐藏层神经网络对非线性可分的平面数据进行分类,对比了逻辑回归的性能,并探讨了隐藏层神经元数量对模型效果的影响,展示了增加隐藏层神经元如何改善模型的适应性,同时提到了正则化的应用。
摘要由CSDN通过智能技术生成

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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值