码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-11-10 00:15:48      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:test   arguments   create   bad   highlight   练习   import   before   wrap   

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])
--------------------------------------------------------------------------------------------------------------------------------------
 

 

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

标签:test   arguments   create   bad   highlight   练习   import   before   wrap   

原文地址:http://www.cnblogs.com/hezhiyao/p/7811925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!