吴恩达深度学习第一课第三周课后作业

阅读: 评论:0

吴恩达深度学习第一课第三周课后作业

吴恩达深度学习第一课第三周课后作业

第三周作业,对于作业环境安装不知道的可以看一下上一篇文章:

这一周把文档也考过来了。

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
导入本周课程作业用到的包和模块

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

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.

使用load_planar_dataset()获得用的数据集,返回X表示坐标点半径和角度,Y表示颜色red or blue

def load_planar_dataset():np.random.seed(1)m = 400 # number of examplesN = int(m/2) # number of points per class,分为两类,每类是红或蓝D = 2 # dimensionality二维X = np.zeros((m,D)) # data matrix where each row is a single exampleY = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue)a = 4 # maximum ray of the flower 花的最大值#linspace以指定的时间间隔返回均匀间隔的数字。for j in range(2):ix = range(N*j,N*(j+1))#ix=(0,199)(200,399)t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta角度,产生200个角度并加入随机数,保证角度随机分开,图像开起来稀疏程度不一r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius半径,4sin(4*t),并加入一定的随机,图像轨道不平滑X[ix] = np.c_[r*np.sin(t), s(t)] #生成坐标点Y[ix] = j #red or blueX = X.TY = Y.Treturn X, Y

数据集生成点云图:

plt.scatter(X[0, :], X[1, :], c=np.squeeze(Y), s=40, Spectral)#此处要squeeze一下,否则可能报错

结果:

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)

### START CODE HERE ### (≈ 3 lines of code)
shape_X=np.shape(X)
shape_Y=np.shape(Y)
m=np.shape(X[0,:])
### 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))

结果:

The shape of X is: (2, 400)
The shape of Y is: (1, 400)
I have m = 400 training examples!

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)
clf.fit(X.T, Y.T.ravel())**#将多维数组降位一维**

You can now plot the decision boundary of these models. Run the code below.

# 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)**#得到预测值Y_hat,标签**
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)")#Y*Y_hat+(1-Y)*(1-Y_hat)#**看预测和真实匹配程度**

结果:

Accuracy of logistic regression: 47 % (percentage of correctly labelled datapoints)


可以看到逻辑回归分类准确率很低,无法正确分类。

4 - Neural Network model

Logistic regression did not work well on the “flower dataset”. You are going to train a Neural Network with a single hidden layer.

Here is our model:

Mathematically:

Given the predictions on all the examples, you can also compute the cost J as follows:

Reminder: The general methodology to build a Neural Network is to:
1. Define the neural network structure ( # of input units, # of hidden units, etc).
2. Initialize the

本文发布于:2024-01-29 06:30:53,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170648105813367.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:作业   课后   一课   深度   第三周
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23