【问题标题】:Write a code to calculate Backward Propagation, Deep Learning course by Andrew NG编写代码计算反向传播,Andrew NG 的深度学习课程
【发布时间】:2021-07-16 09:04:34
【问题描述】:

所以我参加了 Andrew NG 在 coursera 上的深度学习 AI 课程。 我目前正在完成第 2 周的最后一项任务。 我到了必须编写前向和后向传播函数的部分。 我设法编写了相当简单的 fwd_propagate 函数。 这是下面的代码:

def fwd_propagate(w,b,X,y):
    m = X.shape[1]
    A = sigmoid(np.dot(w.T,X)+b)
    J = (-1/m)*np.sum(y * np.log(A) + (1-y) * np.log(1-A))
    return J

现在我必须编写 bwd_propagation 函数,但我不知道从哪里开始以及如何开始。 有人可以帮我解释一下我应该写什么。

这是迄今为止我编写的所有测试内容。

import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage


%matplotlib inline

def load_dataset():
    train_dataset = h5py.File('C:/Users/Univ/Desktop/ML Intern/Logistic-Regression-with-a-Neural-Network-mindset-master/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('C:/Users/Univ/Desktop/ML Intern/Logistic-Regression-with-a-Neural-Network-mindset-master/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

index = 25
plt.imshow(train_set_x_orig[index])
print ("y = " + str(train_set_y[:,index]) + ", it's a '" + classes[np.squeeze(train_set_y[:,index])].decode("utf-8") +  "' picture.")

print(str(train_set_y.shape[1]) + " This is the amount of elements in the training set")
print(str(test_set_y.shape[1]) + " This is the amount of elements in the test set")
print(str(train_set_x_orig.shape[1]) + " This is the Num_px")
print(f"{train_set_x_orig.shape[1]} This is the Num_px")
print(train_set_x_orig.shape)

X_flatten1 = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
X_flatten2 = train_set_y.reshape(train_set_y.shape[0], -1).T 
X_flatten3 = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T 
X_flatten4 = test_set_y.reshape(test_set_y.shape[0], -1).T 

print(X_flatten1)
print(X_flatten2)
print(X_flatten3)
print(X_flatten4)
print(X_flatten1.shape)
print(X_flatten2.shape)
print(X_flatten3.shape)
print(X_flatten4.shape)

print(" Let's standardize our date")
train_set_x = X_flatten1/256
test_set_x = X_flatten3/256
print(train_set_x)
print(test_set_x)

def sigmoid(x):
    s = 1/(1+np.exp(-x))
    return s

print ("sigmoid(0) = " + str(sigmoid(0)))
print ("sigmoid(9.2) = " + str(sigmoid(9.2)))

def initialize_with_zeros(dim):
    shp=(dim,1)
    w = np.zeros(shp)
    b = 0
    assert(w.shape == (dim, 1))
    assert(isinstance(b, float) or isinstance(b, int))
    return w,b

dim = 2
w, b = initialize_with_zeros(dim)
print ("w = " + str(w))
print ("b = " + str(b))

def fwd_propagate(w,b,X,y):
    m = X.shape[1]
    A = sigmoid(np.dot(w.T,X)+b)
    J = (-1/m)*np.sum(y * np.log(A) + (1-y) * np.log(1-A))
    return J

【问题讨论】:

  • 请更具体并添加更多代码。
  • 不要在 cmets 中发布代码 - 改为编辑和更新您的帖子!

标签: machine-learning deep-learning backpropagation


【解决方案1】:

下一步是计算反向传播的导数:

dw = 1/m*(np.dot(X, ((A-Y).T)))
db = 1/m*(np.sum(A-Y))

【讨论】:

  • 是的,你能解释一下我们为什么写这个吗?我们是怎么知道的。 db 是作为作业中的提示编写的。但是我们是怎么得到 dw 的呢??
  • w 是一个权重,我们必须计算它的导数 dw 以便在反向传播期间更新它。
  • 请将此答案标记为正确,如果它解决了您的问题,
猜你喜欢
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 2020-08-13
  • 2023-03-27
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 2018-04-02
相关资源
最近更新 更多