コード例 #1
0
def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    A -- the output of the activation function, also called the post-activation value
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """
    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev,W,b)
        A, activation_cache = sigmoid(Z)

    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev,W,b)
        A, activation_cache = relu(Z)
    else:
        Z, linear_cache = linear_forward(A_prev,W,b)
        A, activation_cache = relu(Z)

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)
    return A, cache
def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of the current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    A -- the output of the activation function, also called the post-activation value 
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """
    
    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    
    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
    
    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache
コード例 #3
0
def linear_activation_forward(A_prev,W,b,activation):
    """
    implement the forward propagaton for the LINEAR-ACTIVATION layer
    # it contains: Z=WA+b; A=g(Z)
    :param A_prev: activations from previous layer (or input data): (size of previous layer, number of examples)
    :param W:weights matrix:numpy array of shape (size of current layer,size of previous layer)
    :param b:bias vector, numpy array of shape (size of the current layer,1)
    :param activation: the activation to be used in this layer, stored as a text string: sigmoid or relu

    :return:
    A- the output of the activation function, also called the post-activation value
    cache- a python dictionary containing "linear_cache" and "activation_cache", stored for computing the BP
    """
    if activation=="sigmoid":
        Z,linear_cache=linear_forward(A_prev,W,b)
        A,activation_cache=sigmoid(Z)

    elif activation=="relu":
        Z,linear_cache=linear_forward(A_prev,W,b)
        A,activation_cache=relu(Z)

    assert (A.shape==(W.shape[0],A_prev.shape[1]))
    cache=(linear_cache,activation_cache)

    return A, cache
def linear_activation_forward(A_prev, W, b, activation):
    '''
    Implement the forward propgation for the LINEAR -> ACTIVATION layer

    Arguments:
    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)
    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)
    b -- bias vector, numpy array of shape (size of current layer, 1)
    activation -- the activation to be used in this layer, stored as a text string: 'sigmoid' or 'relu'

    Returns:
    A -- the output of the activation function, also called the post-activation value
    cache -- a python dictionary containing 'linear_cache' and 'activation_cache';
             stored for computing the backward pass effienciently
    '''

    if activation == 'sigmoid':
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    elif activation == 'relu':
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #5
0
def linear_activation_forward(A_prev, W, b, activation):
    """Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
        A_prev {np.array} -- activations from previous layer
        W {np.array} -- weight matrix
        b {np.array} -- bias vector
        activation {str} -- the activation name

    Returns:
        A {np.array} -- the output of the activation function
        cache {tuple} -- ("linear_cache", "activation_cache")
    """

    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)

    if activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    assert(A.shape == (W.shape[0], A_prev.shape[1]))

    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #6
0
ファイル: DNN.py プロジェクト: Geophysics-Liu/Deep_Learning
 def forward(self, i, A, activation):
     Z = np.dot(self.W[i], A) + self.B[i]
     if activation == 'relu':
         A, cache = relu(Z)
     else:
         A, cache = sigmoid(Z)
     return A, cache
コード例 #7
0
def linear_activation_forward(A_prev, W, b, activation):
    """
	实现LINEAR-> ACTIVATION 这一层的前向传播
	参数:
		A_prev - 来自上一层(或输入层)的激活,维度为(上一层的节点数量,示例数)
		W - 权重矩阵,numpy数组,维度为(当前层的节点数量,前一层的大小)
		b - 偏向量,numpy阵列,维度为(当前层的节点数量,1)
		activation - 选择在此层中使用的激活函数名,字符串类型,【"sigmoid" | "relu"】

	返回:
		A - 激活函数的输出,也称为激活后的值
		cache - 一个包含“linear_cache”和“activation_cache”的字典,我们需要存储它以有效地计算后向传递
	"""
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    elif activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    #返回本层的A值, 及((上一层的A值, 本层的W,b), 本层的A值)
    return A, cache
def linear_activation_forward(A_prev, W, b, activation):
    Z, linear_cache = linear_forward(A_prev, W, b)
    if activation == "sigmoid":
        A, activation_cache = sigmoid(Z)
    elif activation == "relu":
        A, activation_cache = relu(Z)
    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #9
0
def linear_activation_forward(A_prev, W, b, activation):
    Z, linear_cache = linear_forward(A_prev, W, b)
    if activation == "sigmoid":
        A, activation_cache = sigmoid(Z)  # activation_cache = Z
    elif activation == "relu":
        A, activation_cache = relu(Z)
    cache = (linear_cache, activation_cache)  #把Z也加到了cache当中,cache = [A, W, b, Z]

    return A, cache
コード例 #10
0
def linear_activation_forward(A_prev, W, b, activation):
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    elif activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #11
0
def linear_activation_forward(A_prev, W, b, activation):
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)  #得到Z、A_prev、W、b
        A, activation_cache = sigmoid(Z)  #得到当前层的L和Z
    elif activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)  #cache中的值依次为A_prev, W, b, Z
    return A, cache
コード例 #12
0
def linearActivationForward(A_prev, W, b, activation):
    if activation == "sigmoid":
        Z, linearCache = linearForward(A_prev, W, b)
        outputActivationFunc, activationCache = sigmoid(Z)
    elif activation == "relu":
        Z, linearCache = linearForward(A_prev, W, b)
        outputActivationFunc, activationCache = relu(Z)
    cache = (linearCache, activationCache)
    # outputActivationFunc = sigmoid/relu(Z) = A
    # cache = (linearCache = (A_prev,W,b) , activationCache = Z)
    return outputActivationFunc, cache
コード例 #13
0
def linear_activation_forward(A_prev, W, b, activation):

    if activation == 'sigmoid':
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    elif activation == 'relu':
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    cache = (linear_cache, activation_cache)

    return A, cache
コード例 #14
0
def linear_activation_forward(A_prev, W, b, activation):
    # assume using RELU fx in hidden layer and SIGMOID in output layer
    if activation == 'sigmoid':
        Z, linear_cache = linear_forward(A=A_prev, W=W, b=b)
        A, activation_cache = sigmoid(Z)
    elif activation == 'relu':
        Z, linear_cache = linear_forward(A=A_prev, W=W, b=b)
        A, activation_cache = relu(Z)

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #15
0
def linear_activation_forward(A_prev,W,b,activation):
    
    if activation == "sigmoid":
        Z,linear_cache = linear_forward(A_prev,W,b)
        A,activation_cache = sigmoid(Z)
        #sigoid 和 relu的返回值是计算出来的 A 和 原始数据 Z
    elif activation =="relu":
        Z,linear_cache = linear_forward(A_prev,W,b)
        A,activation_cache = relu(Z)
        
    cache = (linear_cache,activation_cache)
    
    return A,cache
コード例 #16
0
def linear_activation_forward(A_prev, W, b, activation):
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)    #activation_cache used for backpropagation

    elif activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)       #activation_cache used for backpropagation

    assert(A.shape == (W.shape[0], A_prev.shape[1]))
    #Used in backpropagation
    cache = (linear_cache, activation_cache)

    return A, cache
def linear_activation_forward(A_prev, W, b, activation):

    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)

    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        ### START CODE HERE ### (≈ 2 lines of code)
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache
コード例 #18
0
def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    A_prev  : np.ndarray
        Activations from previous layer (or input data)
        (size of previous layer, number of examples)
    W : np.ndarray
        weights matrix
        (size of current layer, size of previous layer)
    b : np.ndarray
        bias vector
        (size of the current layer, 1)-- activations from previous layer (or input data): (size of previous layer, number of examples)
    activation : string
                 The activation to be used in this layer, 
                 stored as a text string: "sigmoid" or "relu"

    Returns:
    A, cache : np.ndarray, tuple
        A : np.ndarray
            The output of the activation function, 
            also called the post-activation value 
    cache : linear_cache, activation_cache
             stored for computing the backward pass efficiently
            linear_cache : tuple
                           a python tuple containing "A", "W" and "b"
                           stored for computing the backward pass efficiently
            activation_cache: np.ndarray
                              Z
                              (size of current layer, number of examples)                                       
    """
    
    if activation == "sigmoid":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)
    
    elif activation == "relu":
        # Inputs: "A_prev, W, b". Outputs: "A, activation_cache".
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)
    
    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache
コード例 #19
0
def L_model_forward(X, parameters):
    caches = []
    A = X
    L = len(parameters)     # number of layers in the neural network
    
    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    for l in range(1, L):
        A_prev = A 
        A, cache = relu(np.dot(parameters["W"+str(l)],A_prev) + parameters["b"+str(l)])
        caches.append(cache)
    
    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    AL, cache = sigmoid(np.dot(parameters["W"+str(L)],A) + parameters["b"+str(L)])
    caches.append(cache)
    assert(AL.shape == (1,X.shape[1]))
            
    return AL, caches
コード例 #20
0
ファイル: dnn_demo.py プロジェクト: Natelu/dnn_learn
def liner_activate_forward(A_prev,W,b,activation):
    '''
    :param A_prev: 前一层的输出值
    :param W: 本层的权重
    :param b: 本层偏置项
    :param activation: 激活函数类型 sifmod ReLu
    :return: 
    '''
    # Z=np.dot(W,A_prev)+b
    if activation=='sigmod':
        Z,liner_cache=liner_forward(A_prev,W,b)
        A,actication_cache=dnn_utils_v2.sigmoid(Z)
    elif activation=='relu':
        Z,liner_cache=liner_forward(A_prev,W,b)
        A,actication_cache=dnn_utils_v2.relu(Z)
    assert (A.shape==(W.shape[0],A_prev.shape[1]))
    cache=(liner_cache,actication_cache)    #本层的输入 权重w+b; 本层的z
    return A,cache
コード例 #21
0
def linear_activation_forward(A_prev, W, b, activation):
    """
    Implement the forward propagation for the LINEAR->ACTIVATION layer

    Arguments:
    activation -- the activation to be used in this layer, stored as a text string: "sigmoid" or "relu"

    Returns:
    A -- the output of the activation function, also called the post-activation value 
    cache -- a python dictionary containing "linear_cache" and "activation_cache";
             stored for computing the backward pass efficiently
    """
    Z, linear_cache = linear_forward(A_prev, W, b)
    if activation == "sigmoid":
        A, activation_cache = sigmoid(Z)
    elif activation == "relu":
        A, activation_cache = relu(Z)

    cache = (linear_cache, activation_cache)
    return A, cache
コード例 #22
0
def linear_activation_forward(A, W, b, activation):
    """
    :param A: activation from previous layers
    :param W: weight values
    :param b: bias values
    :param activation: activation function
    :return:
    A: output of activation function
    cache: tuple containing linear and activation cache
    """
    if activation == "sigmoid":
        Z, linear_cache = linear_forward(A, W, b)
        A, activation_cache = sigmoid(Z)

    else:
        Z, linear_cache = linear_forward(A, W, b)
        A, activation_cache = relu(Z)

    cache = (linear_cache, activation_cache)

    return A, cache
def L_model_forward(X, parameters):
    """
    Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation
    
    Arguments:
    X -- data, numpy array of shape (input size, number of examples)
    parameters -- output of initialize_parameters_deep()
    
    Returns:
    AL -- last post-activation value
    caches -- list of caches containing:
                every cache of linear_relu_forward() (there are L-1 of them, indexed from 0 to L-2)
                the cache of linear_sigmoid_forward() (there is one, indexed L-1)
    """

    caches = []
    A = X
    L = len(parameters) // 2  # number of layers in the neural network

    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    for l in range(1, L):
        A_prev = A
        ### START CODE HERE ### (≈ 2 lines of code)
        A, cache = relu(
            np.dot(parameters["W" + str(l)], A) + parameters["b" + str(l)])
        caches.append(cache)

        ### END CODE HERE ###

    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    ### START CODE HERE ### (≈ 2 lines of code)
    AL, cache = sigmoid(
        np.dot(parameters["W" + str(L)], A) + parameters["b" + str(L)])
    caches.append(cache)

    ### END CODE HERE ###

    assert (AL.shape == (1, X.shape[1]))

    return AL, caches
コード例 #24
0
def linear_activation_forward(A_prev, W, b, activation):
    """
    神经网络一层 (LINEAR->ACTIVATION) 的前向传播
    :param A_prev: 前一层的激活值
    :param W: 当前层的权重
    :param b: 当前层的偏置
    :param activation: 当前层的激活函数,string: "sigmoid" or "relu"
    
    :return A: 当前层激活函数的输出值
    :return cache: 元组,包含 "linear_cache" and "activation_cache (Z)", 存储用来高效的计算后向传播
    """
    # 激活函数为 sigmoid()
    if activation == 'sigmoid':
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = sigmoid(Z)  # activation_cache 存储的为 Z
    # 激活函数为 relu()
    elif activation == "relu":
        Z, linear_cache = linear_forward(A_prev, W, b)
        A, activation_cache = relu(Z)  # activation_cache 存储的为 Z

    assert (A.shape == (W.shape[0], A_prev.shape[1]))
    cache = (linear_cache, activation_cache)

    return A, cache
コード例 #25
0
def linear_relu(A_previous, W, b):
    #A_previous is the outoupt of previous layer, W,b is the parameter of current layer
    Z = np.dot(W, A_previous) + b
    A, _ = relu(Z)
    return A, Z