Neural Networks and Deep Learning week3 Planar data classification with a hidden layer

此博客介绍了如何使用带有隐藏层的神经网络解决非线性分类问题,通过实验对比了线性回归和神经网络在二维数据集上的表现。文章详细讲解了神经网络的结构、初始化参数、前向传播、代价函数、反向传播和参数更新过程,并展示了不同隐藏层大小对模型性能的影响。
摘要由CSDN通过智能技术生成

该实验作业的主要目的

  1. 实现一个二元分类(多元分类就是分为目标类和非目标类两类)
  2. 使用非线性激活函数(希望你还记得为什么,在哪里使用什么函数的原因)
  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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值