Neural Networks and Deep Learning week2 Python Basics with numpy (optional)

该实验作业的主要目的

  1. 熟悉使用jupyterlab来编写python代码
  2. sigmoid
  3. numpy一些函数的使用
  4. shape和reshape修改向量
  5. python的广播特性
  6. 向量化代码以减少循环loop
  7. numpy的帮助文件 https://numpy.org/doc/stable/index.html

总体进程

热身 写一个hello world

1.1 写一个sigmoid函数

当我们将该sigmoid函数进行移植时发现太局限,无法对数组等进行运算,因而我们需要对此进行修改

1.1 用numpy重构一个sigmoid函数

从最直观的梯度开始对函数参数进行修改

1.2 完成对sigmoid函数的求导

我们将要对图片进行分类,然而图像分为RGB三层,现在我们需要对图片进行大小转化

1.3使用shape将矩阵转换为列向量

1.4 标准化向量(数据规范化,回想以前的梯度过程,规范化可以让迭代更迅速,迭代图像更加圆润而非一条近似直线)

1.5 使用python的广播特性

2向量化

今后我们要面对很多很大的数据,如果仍按照以前的循环方式,这对工程很麻烦,好在我们由很多矩阵快速算法,因而我们需要想办法将数据向量化

2.1 完成损失函数

在需要写代码的前面我将提示任务目标

Python Basics with Numpy (optional assignment)

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!

Updates to Assignment

This is version 3a of the notebook.

If you were working on a previous version

  • If you were already working on version "3", you'll find your original work in the file directory.
  • To reach the file directory, click on the "Coursera" icon in the top left of this notebook.
  • Please still use the most recent notebook to submit your assignment.

List of Updates

  • softmax section has a comment to clarify the use of "m" later in the course
  • softmax function specifies (m,n) matrix dimensions to match the notation in the preceding diagram (instead of n,m)

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.

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

现实一个字符串  Hello World

### 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

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: 𝑠𝑖𝑔𝑚𝑜𝑖𝑑(𝑥)=\frac{1}{1+e^{-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().

完成sigmoid函数 \frac{1}{1+e^{-x}} 使用 math.exp(x)以完成指数运算

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

Expected Output:

basic_sigmo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值