tuple: 用圆括号()表示,元组的特点是只读,保存在里面的元素不能修改,在保存不可改变的元素时,非常有优势。也有说元组是只读list。列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,然而,有时候你需要创建一系列不可修改的元素,元组可以满足这种需求。Python将不能修改的值称为不可变的,而不可变的列表被称为元组。相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组。
list:用方括号[]表示
dict:用大括号表示
元组使用的特殊方法:
In [1]: a = {'id':10065}
In [2]: b = [1, 2 , 3 , 4, 5]
In [3]: cache = (a, b)
In [4]: (d,e) = cache #取出元组中保存的变量,将元组中保存的东西赋值给d,e两个变量,结果是a赋给d,b赋给了e
In [5]: print(d)
{'id': 10065}
In [6]: print(e)
[1, 2, 3, 4, 5]
In [8]: (f) = cache #将元组中的东西赋给了f这一个变量,将全部内容赋值给了f
In [10]: print(f)
({'id': 10065}, [1, 2, 3, 4, 5])
In [11]: (h,i,j) = cache #将元组中的元素,赋值给三个元素的话,会提示值不够了。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 (h,i,j) = cache
ValueError: not enough values to unpack (expected 3, got 2)
神经网络编程中元组的使用示例:
def forward_propagation(X, parameters):
"""
Implements the forward propagation (and computes the loss) presented in Figure 2.
Arguments:
X -- input dataset, of shape (input size, number of examples)
parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3":
W1 -- weight matrix of shape ()
b1 -- bias vector of shape ()
W2 -- weight matrix of shape ()
b2 -- bias vector of shape ()
W3 -- weight matrix of shape ()
b3 -- bias vector of shape ()
Returns:
loss -- the loss function (vanilla logistic loss)
"""
# retrieve parameters
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
W3 = parameters["W3"]
b3 = parameters["b3"]
# LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
Z1 = np.dot(W1, X) + b1
A1 = relu(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = relu(Z2)
Z3 = np.dot(W3, A2) + b3
A3 = sigmoid(Z3)
cache = (Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3) //将所有前向传播过程中的参数计算出来之后,保存到元组cache中。这些值在元组中不可更改
return A3, cache
def backward_propagation(X, Y, cache):
"""
Implement the backward propagation presented in figure 2.
Arguments:
X -- input dataset, of shape (input size, number of examples)
Y -- true "label" vector (containing 0 if cat, 1 if non-cat)
cache -- cache output from forward_propagation()
Returns:
gradients -- A dictionary with the gradients with respect to each parameter, activation and pre-activation variables
"""
m = X.shape[1]
(Z1, A1, W1, b1, Z2, A2, W2, b2, Z3, A3, W3, b3) = cache #在反向传播过程中将元组中#保存的结果取出来,然后使用这些值,计算梯度。
dZ3 = A3 - Y
dW3 = 1./m * np.dot(dZ3, A2.T)
db3 = 1./m * np.sum(dZ3, axis=1, keepdims = True)
dA2 = np.dot(W3.T, dZ3)
dZ2 = np.multiply(dA2, np.int64(A2 > 0))
dW2 = 1./m * np.dot(dZ2, A1.T)
db2 = 1./m * np.sum(dZ2, axis=1, keepdims = True)
dA1 = np.dot(W2.T, dZ2)
dZ1 = np.multiply(dA1, np.int64(A1 > 0))
dW1 = 1./m * np.dot(dZ1, X.T)
db1 = 1./m * np.sum(dZ1, axis=1, keepdims = True)
gradients = {"dZ3": dZ3, "dW3": dW3, "db3": db3,
"dA2": dA2, "dZ2": dZ2, "dW2": dW2, "db2": db2,
"dA1": dA1, "dZ1": dZ1, "dW1": dW1, "db1": db1}
return gradients