Neural Networks deep learning Python Basics with numpy (optional) Homework__python

Source: Internet
Author: User
Python Basics with NumPy (optional assignment)


Welcome to your the assignment. This is a brief introduction to Python exercise gives. Even if you ' ve used Python before, this'll help familiarize your with functions we ' ll need.



Instructions:
-You'll be using Python 3.
-Avoid using for-loops and while-loops, unless you are explicitly.
-Don't modify the (# graded function [function name]) comment in some cells. Your work would is graded if you are change this. Each cell containing this comment should only contain one function.
-After coding your function, run the cell right below it to check if your the 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"
-Is able to vectorize code



Let ' s get started! About IPython Notebooks



IPython notebooks are interactive coding environments embedded in a webpage. You are using IPython notebooks in this class. You have 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'll often specify "(≈x lines of code)" In the comments to tell you about so much code for you need to write. It is just a rough estimate and so don ' t feel bad if your the code is longer or shorter.



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



### START code here ### (≈1 line of code)
test = None
test= ' Hello world ' 
### end code here ###

Print ("Test:" + test)

Test:hello World


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
-Does not modify the code outside of the designated areas 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'll learn several key numpy functions such as Np.exp, Np.log, and Np.reshape. You'll need to the know how-to-use this functions for future assignments. 1.1-sigmoid function, Np.exp ()


Before using Np.exp (), you'll use MATH.EXP () to implement the Sigmoid function. You'll then why Np.exp () is preferable to Math.exp ().



Exercise: Build a function that returns the sigmoid's a real number X. Use MATH.EXP (x) for the exponential funct Ion.


Reminder:

Sigmoid (x) =11+e−x sigmoid (x) = \frac{1}{1+e^{-x} is sometimes also known as the The logistic function. It is a non-linear function used does not be in Machine Learning (Logistic regression), but also in Deep Learning.






To refer to a function belonging to a specific package your could call it using package_name.function (). Run the code below to the example with Math.exp ().



# 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 = None
    s=1/(1+math.exp (x))
    ### end CODE Here ### return

    s

Basic_sigmoid (3)

0.9525741268224334


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 are more useful.



### One reason why we use "numpy" instead of "math" in Deep Learning ###
x = [1, 2, 3]
basic_sigmoid (x) # The Give an error while you run it, because X is a vector.

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

typeerror                                 traceback (most Recent call last)

<ipython-input-26-2e11097d6860> in <module> ()
      1 ### One reason why we use "NumPy" in  stead of "math" in Deep Learning ###
      2 x = [1, 2, 3]
----> 3 basic_sigmoid (x) # and you'll have to be here give an error When you run it, the because X is a vector.


<ipython-input-24-f2ee07699056> in Basic_sigmoid (x)     ### START CODE here ### (≈1 line of code)
     17s = None
--->     s=1/(1+math.exp (x))
     ### end     CODE here ### 


Typeerror:bad Operand type for unary-: ' list '


In fact, if x= (x1,x2,..., xn) x = (x_1, x_2, ..., x_n) is a row vector then Np.exp (x) np.exp (x) would apply the exponential function to every element of x. The output would thus be:np.exp (x) = (ex1,ex2,..., exn) np.exp (x) = (E^{x_1}, E^{x_2}, ..., e^{x_n})



Import NumPy as NP

# Example of np.exp
x = Np.array ([1, 2, 3])
print (Np.exp (x)) # result is (exp (1), exp (2), E XP (3))

[  2.71828183   7.3890561   20.08553692]


Furthermore, if X is a vector, then a Python operation such as s=x+3 s = x + 3 or s=1x s = \frac{1}{x} 'll output S as a Vector of the same size as x.



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

[4 5 6]


Any time for your need more info on a numpy function, we are encourage you to look at the official documentation.



Can also create a new cell in the notebook and write np.exp? [For example] to get quick access to the documentation.



Exercise: Implement the sigmoid function using NumPy.


Instructions : 
X could now is either a real number, a vector, or a matrix. The data structures we numpy to represent the shapes (vectors, matrices ...) are called the NumPy. You don ' t need to know the more for now.

For x∈rn, sigmoid (x) =sig


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.