• 吴恩达机器学习week2实验答案Practice Lab Linear Regression【C1_W2_Linear_Regression】


    Practice Lab: Linear Regression

    Exercise 1

    Complete the compute_cost below to:

    • Iterate over the training examples, and for each example, compute:

      • The prediction of the model for that example
        f w b ( x ( i ) ) = w x ( i ) + b f_{wb}(x^{(i)}) = wx^{(i)} + b fwb(x(i))=wx(i)+b

      • The cost for that example c o s t ( i ) = ( f w b − y ( i ) ) 2 cost^{(i)} = (f_{wb} - y^{(i)})^2 cost(i)=(fwby(i))2

    • Return the total cost over all examples
      J ( w , b ) = 1 2 m ∑ i = 0 m − 1 c o s t ( i ) J(\mathbf{w},b) = \frac{1}{2m} \sum\limits_{i = 0}^{m-1} cost^{(i)} J(w,b)=2m1i=0m1cost(i)

      • Here, m m m is the number of training examples and ∑ \sum is the summation operator

    If you get stuck, you can check out the hints presented after the cell below to help you with the implementation.

    # UNQ_C1
    # GRADED FUNCTION: compute_cost
    
    def compute_cost(x, y, w, b): 
        # number of training examples
        m = x.shape[0] 
        # You need to return this variable correctly
        total_cost = 0
        
        ### START CODE HERE ###  
        for i in range(m):
            total_cost+=((x[i]*w+b)-y[i])**2
        total_cost=total_cost/(2*m)
        ### END CODE HERE ### 
        
        return total_cost
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Exercise 2

    Please complete the compute_gradient function to:

    • Iterate over the training examples, and for each example, compute:

      • The prediction of the model for that example
        f w b ( x ( i ) ) = w x ( i ) + b f_{wb}(x^{(i)}) = wx^{(i)} + b fwb(x(i))=wx(i)+b

      • The gradient for the parameters w , b w, b w,b from that example
        ∂ J ( w , b ) ∂ b ( i ) = ( f w , b ( x ( i ) ) − y ( i ) ) \frac{\partial J(w,b)}{\partial b}^{(i)} = (f_{w,b}(x^{(i)}) - y^{(i)}) bJ(w,b)(i)=(fw,b(x(i))y(i))
        ∂ J ( w , b ) ∂ w ( i ) = ( f w , b ( x ( i ) ) − y ( i ) ) x ( i ) \frac{\partial J(w,b)}{\partial w}^{(i)} = (f_{w,b}(x^{(i)}) -y^{(i)})x^{(i)} wJ(w,b)(i)=(fw,b(x(i))y(i))x(i)

    • Return the total gradient update from all the examples
      ∂ J ( w , b ) ∂ b = 1 m ∑ i = 0 m − 1 ∂ J ( w , b ) ∂ b ( i ) \frac{\partial J(w,b)}{\partial b} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} \frac{\partial J(w,b)}{\partial b}^{(i)} bJ(w,b)=m1i=0m1bJ(w,b)(i)

      ∂ J ( w , b ) ∂ w = 1 m ∑ i = 0 m − 1 ∂ J ( w , b ) ∂ w ( i ) \frac{\partial J(w,b)}{\partial w} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} \frac{\partial J(w,b)}{\partial w}^{(i)} wJ(w,b)=m1i=0m1wJ(w,b)(i)

      • Here, m m m is the number of training examples and ∑ \sum is the summation operator

    If you get stuck, you can check out the hints presented after the cell below to help you with the implementation.

    # UNQ_C2
    # GRADED FUNCTION: compute_gradient
    def compute_gradient(x, y, w, b):   
        # Number of training examples
        m = x.shape[0]
        # You need to return the following variables correctly
        dj_dw = 0
        dj_db = 0
        
        ### START CODE HERE ### 
        for i in range(m):
            f_wb=w*x[i]+b
            dj_dw_i=(f_wb-y[i])*x[i]
            dj_db_i=f_wb-y[i]
            dj_dw+=dj_dw_i
            dj_db+=dj_db_i
        dj_dw=dj_dw/m
        dj_db=dj_db/m
        ### END CODE HERE ### 
            
        return dj_dw, dj_db
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    Netty之非阻塞处理
    工业机器人发展趋势
    spring initializr脚手架搭建详解
    Oracle数据不常用的函数
    Hadoop学习笔记(2)——HDFS(1)
    操作系统-进程与线程(进程同步与互斥,进程互斥的软硬件实现方式)
    SpringCloud无介绍快使用,Ribbon负载均衡工具与OpenFeign的使用(十五)
    IDA的各个视图的含义,View-A、Hex View-1等
    Linux权限认识
    嵌入式实操----基于RT1170 使能展频功能(二十七)
  • 原文地址:https://blog.csdn.net/qq_41954181/article/details/132863595