第二周:神经网络的编程基础----------3、Python Basics with numpy (optional)

Python Basics with numpy (optional)
Welcome to your first (Optional) programming exercise of the deep learning specialization. In this assignment you will:

- Learn how to use numpy.

- Implement some basic core deep learning functions such as the softmax, sigmoid, dsigmoid, etc...

- Learn how to handle data by normalizing inputs and reshaping images.

- Recognize the importance of vectorization.

- Understand how python broadcasting works.

This assignment prepares you well for the upcoming assignment. Take your time to complete it and make sure you get the expected outputs when working through the different exercises. In some code blocks, you will find a "#GRADED FUNCTION: functionName" comment. Please do not modify it. After you are done, submit your work and check your results. You need to score 70% to pass. Good luck :) !

中文翻译------------>

numpy 的 Python 基础知识 (可选)

欢迎您的第一次 (可选) 编程练习的深度学习专门化。在本作业中, 您将:

-学习如何使用 numpy。

-实施一些基本的核心学习功能, 如 softmax、 sigmoid、dsigmoid 等。

-了解如何通过规范化输入和重塑图像来处理数据。

-认识到向量化的重要性。

-了解 python 广播的工作原理。

这个任务为即将到来的任务做好准备。用你的时间完成它, 通过不同的练习,确保你得到预期的结果, 。在某些代码块中, 您将找到一个 ""#GRADED FUNCTION: functionName" 的注释。请不要修改它。完成后, 提交您的工作, 并检查您的结果。你需要得分70% 才能过关。祝你好运:)!

--------------------------------------------------------------------------------------------------------------------------------------

Welcome to your first assignment. This exercise gives you a brief introduction to Python. Even if you‘ve used Python before, this will help familiarize you with functions we‘ll need.
Instructions:
You will be using Python 3.
Avoid using for-loops and while-loops, unless you are explicitly told to do so.
Do not modify the (# GRADED FUNCTION [function name]) comment in some cells. Your work would not be graded if you change this. Each cell containing that comment should only contain one function.
After coding your function, run the cell right below it to check if your result is correct.
After this assignment you will:
Be able to use iPython Notebooks
Be able to use numpy functions and numpy matrix/vector operations
Understand the concept of "broadcasting"
Be able to vectorize code
Let‘s get started!

中文翻译------------>

欢迎您的第一次任务。本练习将简要介绍 Python。即使您以前使用过 Python, 这将有助于您熟悉我们需要的功能。

说明:

您将使用 Python 3。

避免使用 for 循环和循环, 除非明确告诉你这样做。

不要修改某些单元格中的 (# GRADED FUNCTION [function name]) 注释。如果你改变了这一点, 你的工作将不会被评分。包含该注释的每个单元格只应包含一个函数。

在对函数进行编码后, 运行下面的单元格, 检查结果是否正确。

完成此任务后, 您将:

能够使用 iPython 笔记本

能够使用 numpy 函数和 numpy 矩阵/向量运算

理解 "广播" 的概念

能够向量化代码

我们开始吧!

--------------------------------------------------------------------------------------------------------------------------------------

About iPython Notebooks
iPython Notebooks are interactive coding environments embedded in a webpage. You will be using iPython notebooks in this class. You only need to write code between the ### START CODE HERE ### and ### END CODE HERE ### comments. After writing your code, you can run the cell by either pressing "SHIFT"+"ENTER" or by clicking on "Run Cell" (denoted by a play symbol) in the upper bar of the notebook.
We will often specify "(≈ X lines of code)" in the comments to tell you about how much code you need to write. It is just a rough estimate, so don‘t feel bad if your code is longer or shorter.

中文翻译------------>

关于 iPython 笔记本

iPython 笔记本是嵌入在网页中的交互式编码环境。您将在本课中使用 iPython 笔记本。您只需要在 ### START CODE HERE ### and ### END CODE HERE ### 编写代码, 然后可以通过按 "SHIFT" + "ENTER" 或在笔记本的上部栏中单击 "Run Cell"  (由播放符号表示) 来运行单元格。

我们经常会在注释中指定 "(≈ X 行代码)" 来告诉您需要编写多少代码。这只是一个粗略的估计, 所以如果您的代码更长或更短, 请不要感到遗憾。

--------------------------------------------------------------------------------------------------------------------------------------

Exercise: Set test to "Hello World" in the cell below to print "Hello World" and run the two cells below.

中文翻译------------>

练习: 将测试设置为 "Hello World", 在下面的单元格中打印 "Hello World"并运行下面的两个单元格。

code--------->

### START CODE HERE ### (≈ 1 line of code)
test = "Hello World"
### END CODE HERE ###
print ("test: " + test)

Expected output: test: Hello World

--------------------------------------------------------------------------------------------------------------------------------------

What you need to remember:
Run your cells using SHIFT+ENTER (or "Run cell")
Write code in the designated areas using Python 3 only
Do not modify the code outside of the designated areas

中文翻译------------>

您需要记住的内容:

使用 SHIFT + ENTER(或 "Run cell") 运行单元格

仅使用 Python 3 在指定区域中编写代码

不要修改指定区域之外的代码

--------------------------------------------------------------------------------------------------------------------------------------

1 - Building basic functions with numpy

Numpy is the main package for scientific computing in Python. It is maintained by a large community (www.numpy.org). In this exercise you will learn several key numpy functions such as np.exp, np.log, and np.reshape. You will need to know how to use these functions for future assignments.

1.1 - sigmoid function, np.exp()

Before using np.exp(), you will use math.exp() to implement the sigmoid function. You will then see why np.exp() is preferable to math.exp().

Exercise: Build a function that returns the sigmoid of a real number x. Use math.exp(x) for the exponential function.
Reminder: sigmoid(x)=is sometimes also known as the logistic function. It is a non-linear function used not only in Machine Learning (Logistic Regression), but also in Deep Learning.

To refer to a function belonging to a specific package you could call it using package_name.function(). Run the code below to see an example with math.exp().

中文翻译------------>

1-建设基本功能与 numpy

Numpy 是 Python 中科学计算的主要软件包。它由一个大社区维护 (www.numpy.org)。在本练习中, 您将学习一些关键的 numpy 函数, 如 np. exp、np.log 和 np.reshape。您需要知道如何将这些函数用于将来的作业。

1.1-sigmoid function, np.exp()

在使用 np. exp () 之前, 您将使用math. exp () 来实现sigmoid 函数。然后你就会明白为什么 np.exp() 比math.exp()更好了。

练习: 生成返回实数 x 的sigmoid函数. 使用 math.exp(x)来代替指数。

提示: sigmoid(x)=有时也称为逻辑函数。它是一种非线性函数, 不仅用于机器学习 (逻辑回归), 而且还用于深度学习。

要引用属于特定包的函数, 可以使用 package_name.function()来调用它。运行下面的代码以查看math. exp () 的示例。

code--------->

# GRADED FUNCTION: basic_sigmoid

import math

def basic_sigmoid(x):
    """
    Compute sigmoid of x.

    Arguments:
    x -- A scalar

    Return:
    s -- sigmoid(x)
    """

    ### START CODE HERE ### (≈ 1 line of code)
    s = 1/(1+math.exp(-x))
    ### END CODE HERE ###

    return s
basic_sigmoid(3)

Expected Output:

basic_sigmoid(3) 0.9525741268224334

--------------------------------------------------------------------------------------------------------------------------------------

Actually, we rarely use the "math" library in deep learning because the inputs of the functions are real numbers. In deep learning we mostly use matrices and vectors. This is why numpy is more useful.

中文翻译------------>

实际上, 我们很少在深入学习中使用 "math" 库, 因为函数的输入是实数。在深入学习中, 我们主要使用矩阵和向量。这就是为什么 numpy 更有用。

code--------->

### One reason why we use "numpy" instead of "math" in Deep Learning ###
x = [1, 2, 3]
basic_sigmoid(x)
# you will see this give an error when you run it, because x is a vector.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2e11097d6860> in <module>()
      1 ### One reason why we use "numpy" instead of "math" in Deep Learning ###
      2 x = [1, 2, 3]
----> 3 basic_sigmoid(x) # you will see this give an error when you run it, because x is a vector.

<ipython-input-4-951c5721dbfa> in basic_sigmoid(x)
     15
     16     ### START CODE HERE ### (≈ 1 line of code)
---> 17     s = 1/(1+math.exp(-x))
     18     ### END CODE HERE ###
     19 

TypeError: bad operand type for unary -: ‘list‘
 

In fact, if x=(x1,x2,...,xn) is a row vector then np.exp(x)will apply the exponential function to every element of x. The output will thus be: np.exp(x)=(ex1,ex2,...,exn)

import numpy as np

# example of np.exp
x = np.array([1, 2, 3])
print(np.exp(x)) # result is (exp(1), exp(2), exp(3))

result:

[  2.71828183   7.3890561   20.08553692]

Furthermore, if x is a vector, then a Python operation such as s=x+3s=x+3 or s=1xs=1x will output s as a vector of the same size as x.

# example of vector operation
x = np.array([1, 2, 3])
print (x + 3)

result:

[4 5 6]

--------------------------------------------------------------------------------------------------------------------------------------

Any time you need more info on a numpy function, we encourage you to look at the official documentation.
You can also create a new cell in the notebook and write np.exp? (for example) to get quick access to the documentation.

中文翻译------------>

任何时候, 你需要更多的信息 numpy 功能, 我们鼓励你看看官方文件

您还可以在笔记本中创建一个新单元, 并编写 np. exp?(例如) 获取文档的快速访问。

--------------------------------------------------------------------------------------------------------------------------------------

Exercise: Implement the sigmoid function using numpy.
Instructions: x could now be either a real number, a vector, or a matrix. The data structures we use in numpy to represent these shapes (vectors, matrices...) are called numpy arrays. You don‘t need to know more for now.

中文翻译------------>

练习: 使用 numpy 实现sigmoid函数。

说明: x 现在可以是实数、向量或矩阵。我们在 numpy 中使用的数据结构来表示这些形状 (向量、矩阵......) 称为 numpy 数组。你现在不需要知道更多。

code----------->

# GRADED FUNCTION: sigmoid

import numpy as np # this means you can access numpy functions by writing np.function() instead of numpy.function()

def sigmoid(x):
    """
    Compute the sigmoid of x

    Arguments:
    x -- A scalar or numpy array of any size

    Return:
    s -- sigmoid(x)
    """

    ### START CODE HERE ### (≈ 1 line of code)
    s = 1/(1+np.exp(-x))
    ### END CODE HERE ###

    return s
x = np.array([1, 2, 3])
# x=[1,2,3]   #这种写法会报错
sigmoid(x)

result:

array([ 0.73105858,  0.88079708,  0.95257413])

Expected Output:

sigmoid([1,2,3]) array([ 0.73105858, 0.88079708, 0.95257413])

--------------------------------------------------------------------------------------------------------------------------------------

时间: 2024-08-23 00:13:19

第二周:神经网络的编程基础----------3、Python Basics with numpy (optional)的相关文章

第二章 Matlab面向对象编程基础

DeepLab是一款基于Matlab面向对象编程的深度学习工具箱,所以了解Matlab面向对象编程的特点是必要的.笔者在做Matlab面向对象编程的时候发现无论是互联网上还是书店里卖的各式Matlab编程书上都对Matlab面向对象所提甚少甚至没提.因此在DeepLab的使用说明书中专门添加Matlab面向对象编程基础是有必要的.第一节 Matlab面向对象大体结构代码清单2.1.1 classdef className<handle & superclass1 & supercla

第二周:神经网络的编程基础----------0、学习目标

1. Build a logistic regression model, structured as a shallow neural network2. Implement the main steps of an ML algorithm, including making predictions, derivative computation, and gradient descent.3. Implement computationally efficient, highly vect

第二周:神经网络的编程基础----------2、编程作业常见问题与答案(Programming Assignment FAQ)

Please note that when you are working on the programming exercise you will find comments that say "# GRADED FUNCTION: functionName". Do not edit that comment. The function in that code block will be graded. 1) What is a Jupyter notebook? A Jupyt

第二周:神经网络的编程基础-----------10个测验题(Neural Network Basics)

--------------------------------------------------中文翻译----------------------------------------------------------------------------------------- 1.神经元的计算是什么?(B) A. 在将输出应用到激活函数之前, 神经元计算所有特征的平均值 B. 神经元计算一个线性函数 (z = Wx + b), 然后是一个激活函数 C. 神经元计算一个激活函数, 后跟一

从零开始学Python第八周:网络编程基础(socket)

Socket网络编程 一,Socket编程 (1)Socket方法介绍 Socket是网络编程的一个抽象概念.通常我们用一个Socket表示"打开了一个网络链接",而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可. 套接字是一个双向的通信信道的端点.套接字可能在沟通过程,进程之间在同一台机器上,或在不同的计算机之间的进程 要创建一个套接字,必须使用Socket模块的socket.socket()方法 在socket模块中的一般语法: s = socket.

【python之路】【2、编程基础】Python简介和入门

1.Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 最新的TIOBE排行榜,Python赶超PHP占据第五!!! 由上图可见,Python整体呈上升趋势,反映出Python应用越来越广泛并且也逐渐得到业内的认可!!! Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业

【第二周】结对编程体会

到今天接触结对编程差不多两周了,对于结对编程有了更加深刻地理解,通过结对编程,我们可以及时的发现并解决后面可能会出现的问题,并且还能提升我们与别人进行交流的能力,与人沟通对我们程序员来说也是极为重要的,同时,结对编程使程序员频繁进行交流,而且要提升自己的技术能力,对我们是一种压力,也是一种动力.更多感想还需要后期的继续.

吴恩达《深度学习》第一门课(2)神经网络的编程基础

2.1二分类 (1)以一张三通道的64×64的图片做二分类识别是否是毛,输出y为1时认为是猫,为0时认为不是猫: y输出是一个数,x输入是64*64*3=12288的向量. (2)以下是一些符号定义(数据集变成矩阵之后进行矩阵运算代替循环运算,更加高效) x:表示一个nx维数据,维度为(nx,1) y:表示输出结果,取值为(0,1): (x(i),y(i)):表示第i组数据: X=[x(1),x(2),--,x(m)]:表示按列将所有的训练数据集的输入值堆叠成一个矩阵:其中m表示样本数目: Y=

linux学习第二周;shell编程

pstree -p #显示进程编号(树状) echo $$ #显示当前shell进程编号 脚本的基本结构 : #!SHEBANG (表明使用什么shell打开文件,一般使用bash) CONFIGURATION_VARIABLES #定义变量 FUNCTION_DEFINITIONS#定义函数 MAIN_CODE #主要执行命令 检测脚本中的语法错误 bash -n /path/to/some_script 调试执行 bash -x /path/to/some_script 变量命名法则: 1.