CS231n Convolutional Neural Networks for Visual Recognition------Numpy Tutorial
源鏈接為:http://cs231n.github.io/python-numpy-tutorial/。
這篇指導(dǎo)書是由Justin Johnson編寫的。
在這門課程中我們將使用Python語(yǔ)言完成所有變成任務(wù)!Python本身就是一種很棒的通用編程語(yǔ)言,但是在一些流行的庫(kù)幫助下(numpy,scipy,matplotlib)它已經(jīng)成為科學(xué)計(jì)算的強(qiáng)大環(huán)境。
我們希望你們中的許多人都有一些Python和numpy的使用經(jīng)驗(yàn); 對(duì)你們其他人來(lái)說(shuō),這個(gè)section將作為Python用于科學(xué)計(jì)算和使用的快速速成課程。
你們中的一些人可能已經(jīng)掌握了Matlab的知識(shí),在這種情況下我們也推薦使用numpy。
你也可以閱讀由Volodymyr Kuleshov和Isaac Caswell(CS 228)編寫的Notebook版筆記。
本教程使用的Python版本為Python3.
目錄
Arrays
Array indexing
Datatypes
Array math
Broadcasting
原文共分為4部分,分別介紹了Python、Numpy、Scipy和Matplotlib的使用。本次翻譯為第二部分:Numpy的使用指導(dǎo)!
Numpy是Python中科學(xué)計(jì)算的核心庫(kù)。 它提供了一個(gè)高性能的多維數(shù)組對(duì)象,以及用于處理這些數(shù)組的工具。 如果您已經(jīng)熟悉MATLAB,那么您可能會(huì)發(fā)現(xiàn)本教程對(duì)Numpy入門非常有用。
Arrays
numpy數(shù)組是一個(gè)值網(wǎng)格,所有類型都相同,并且由非負(fù)整數(shù)元組索引。 數(shù)組的形狀是一個(gè)整數(shù)元組,并且給出了每個(gè)維度的數(shù)組大小。
我們可以從嵌套的Python列表初始化numpy數(shù)組,并使用方括號(hào)訪問(wèn)元素:
import numpy as npa = np.array([1, 2, 3]) # Create a rank 1 array print(type(a)) # Prints "<class 'numpy.ndarray'>" print(a.shape) # Prints "(3,)" print(a[0], a[1], a[2]) # Prints "1 2 3" a[0] = 5 # Change an element of the array print(a) # Prints "[5, 2, 3]"b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array print(b.shape) # Prints "(2, 3)" print(b[0, 0], b[0, 1], b[1, 0]) # Prints "1 2 4"Numpy還提供了創(chuàng)建數(shù)組的函數(shù):
import numpy as npa = np.zeros((2,2)) # Create an array of all zeros print(a) # Prints "[[ 0. 0.]# [ 0. 0.]]"b = np.ones((1,2)) # Create an array of all ones print(b) # Prints "[[ 1. 1.]]"c = np.full((2,2), 7) # Create a constant array print(c) # Prints "[[ 7. 7.]# [ 7. 7.]]"d = np.eye(2) # Create a 2x2 identity matrix print(d) # Prints "[[ 1. 0.]# [ 0. 1.]]"e = np.random.random((2,2)) # Create an array filled with random values print(e) # Might print "[[ 0.91940167 0.08143941]# [ 0.68744134 0.87236687]]"你可以在這篇文檔看到更多關(guān)于創(chuàng)建數(shù)組的方法。
Array indexing
Numpy提供了幾種索引數(shù)組的方法。
切片:與Python列表類似,可以切割numpy數(shù)組。 由于數(shù)組可能是多維的,因此必須為數(shù)組的每個(gè)維指定一個(gè)切片:
import numpy as np# Create the following rank 2 array with shape (3, 4) # [[ 1 2 3 4] # [ 5 6 7 8] # [ 9 10 11 12]] a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])# Use slicing to pull out the subarray consisting of the first 2 rows # and columns 1 and 2; b is the following array of shape (2, 2): # [[2 3] # [6 7]] b = a[:2, 1:3]# A slice of an array is a view into the same data, so modifying it # will modify the original array. print(a[0, 1]) # Prints "2" b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1] print(a[0, 1]) # Prints "77"#這里也對(duì)原始數(shù)組進(jìn)行了修改您還可以將整數(shù)索引與切片索引混合使用。 但是,這樣做會(huì)產(chǎn)生比原始數(shù)組更低級(jí)別的數(shù)組。 請(qǐng)注意,這與MATLAB處理數(shù)組切片的方式完全不同:
import numpy as np# Create the following rank 2 array with shape (3, 4) # [[ 1 2 3 4] # [ 5 6 7 8] # [ 9 10 11 12]] a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])# Two ways of accessing the data in the middle row of the array. # Mixing integer indexing with slices yields an array of lower rank, # while using only slices yields an array of the same rank as the # original array: row_r1 = a[1, :] # Rank 1 view of the second row of a row_r2 = a[1:2, :] # Rank 2 view of the second row of a print(row_r1, row_r1.shape) # Prints "[5 6 7 8] (4,)" print(row_r2, row_r2.shape) # Prints "[[5 6 7 8]] (1, 4)"# We can make the same distinction when accessing columns of an array: col_r1 = a[:, 1] col_r2 = a[:, 1:2] print(col_r1, col_r1.shape) # Prints "[ 2 6 10] (3,)" print(col_r2, col_r2.shape) # Prints "[[ 2]# [ 6]# [10]] (3, 1)"整數(shù)數(shù)組索引:使用切片索引到numpy數(shù)組時(shí),生成的數(shù)組視圖將始終是原始數(shù)組的子數(shù)組。 相反,整數(shù)數(shù)組索引允許您使用另一個(gè)數(shù)組中的數(shù)據(jù)構(gòu)造任意數(shù)組。 這是一個(gè)例子:
import numpy as npa = np.array([[1,2], [3, 4], [5, 6]])# An example of integer array indexing. # The returned array will have shape (3,) and print(a[[0, 1, 2], [0, 1, 0]]) # Prints "[1 4 5]"# The above example of integer array indexing is equivalent to this: print(np.array([a[0, 0], a[1, 1], a[2, 0]])) # Prints "[1 4 5]"# When using integer array indexing, you can reuse the same # element from the source array: print(a[[0, 0], [1, 1]]) # Prints "[2 2]"# Equivalent to the previous integer array indexing example print(np.array([a[0, 1], a[0, 1]])) # Prints "[2 2]"整數(shù)數(shù)組索引的一個(gè)有用技巧是從矩陣的每一行中選擇或改變一個(gè)元素:
import numpy as np# Create a new array from which we will select elements a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])print(a) # prints "array([[ 1, 2, 3],# [ 4, 5, 6],# [ 7, 8, 9],# [10, 11, 12]])"# Create an array of indices b = np.array([0, 2, 0, 1])# Select one element from each row of a using the indices in b print(a[np.arange(4), b]) # Prints "[ 1 6 7 11]"# Mutate one element from each row of a using the indices in b a[np.arange(4), b] += 10print(a) # prints "array([[11, 2, 3],# [ 4, 5, 16],# [17, 8, 9],# [10, 21, 12]])布爾數(shù)組索引:布爾數(shù)組索引允許您選擇數(shù)組的任意元素。 通常,這種類型的索引用于選擇滿足某些條件的數(shù)組元素。 這是一個(gè)例子:
import numpy as npa = np.array([[1,2], [3, 4], [5, 6]])bool_idx = (a > 2) # Find the elements of a that are bigger than 2;# this returns a numpy array of Booleans of the same# shape as a, where each slot of bool_idx tells# whether that element of a is > 2.print(bool_idx) # Prints "[[False False]# [ True True]# [ True True]]"# We use boolean array indexing to construct a rank 1 array # consisting of the elements of a corresponding to the True values # of bool_idx print(a[bool_idx]) # Prints "[3 4 5 6]"# We can do all of the above in a single concise statement: print(a[a > 2]) # Prints "[3 4 5 6]"為簡(jiǎn)潔起見,我們遺漏了很多關(guān)于numpy數(shù)組索引的細(xì)節(jié); 如果你想了解更多,你應(yīng)該閱讀文檔。
Datatypes
每個(gè)numpy數(shù)組都是相同類型的元素。 Numpy提供了一組可用于構(gòu)造數(shù)組的大量數(shù)值數(shù)據(jù)類型。 Numpy在創(chuàng)建數(shù)組時(shí)嘗試猜測(cè)數(shù)據(jù)類型,但構(gòu)造數(shù)組的函數(shù)通常還包含一個(gè)可選參數(shù)來(lái)顯式指定數(shù)據(jù)類型。 這是一個(gè)例子:
import numpy as npx = np.array([1, 2]) # Let numpy choose the datatype print(x.dtype) # Prints "int64"x = np.array([1.0, 2.0]) # Let numpy choose the datatype print(x.dtype) # Prints "float64"x = np.array([1, 2], dtype=np.int64) # Force a particular datatype print(x.dtype) # Prints "int64"你可以在這篇文檔看到更多關(guān)于數(shù)組數(shù)據(jù)類型的細(xì)節(jié)。
Array math
基本數(shù)學(xué)函數(shù)在數(shù)組上以元素方式運(yùn)行,既可以作為運(yùn)算符重載,也可以作為numpy模塊中的函數(shù):
import numpy as npx = np.array([[1,2],[3,4]], dtype=np.float64) y = np.array([[5,6],[7,8]], dtype=np.float64)# Elementwise sum; both produce the array # [[ 6.0 8.0] # [10.0 12.0]] print(x + y) print(np.add(x, y))# Elementwise difference; both produce the array # [[-4.0 -4.0] # [-4.0 -4.0]] print(x - y) print(np.subtract(x, y))# Elementwise product; both produce the array # [[ 5.0 12.0] # [21.0 32.0]] print(x * y) print(np.multiply(x, y))# Elementwise division; both produce the array # [[ 0.2 0.33333333] # [ 0.42857143 0.5 ]] print(x / y) print(np.divide(x, y))# Elementwise square root; produces the array # [[ 1. 1.41421356] # [ 1.73205081 2. ]] print(np.sqrt(x))請(qǐng)注意,與MATLAB不同,*是元素乘法,而不是矩陣乘法。 我們使用點(diǎn)函數(shù)來(lái)計(jì)算向量的內(nèi)積,將向量乘以矩陣,并乘以矩陣。 dot既可以作為numpy模塊中的函數(shù)使用,也可以作為數(shù)組對(duì)象的實(shí)例方法:
import numpy as npx = np.array([[1,2],[3,4]]) y = np.array([[5,6],[7,8]])v = np.array([9,10]) w = np.array([11, 12])# Inner product of vectors; both produce 219 print(v.dot(w)) print(np.dot(v, w))# Matrix / vector product; both produce the rank 1 array [29 67] print(x.dot(v)) print(np.dot(x, v))# Matrix / matrix product; both produce the rank 2 array # [[19 22] # [43 50]] print(x.dot(y)) print(np.dot(x, y))Numpy提供了許多用于在數(shù)組上執(zhí)行計(jì)算的有用函數(shù); 其中最有用的是sum:
import numpy as npx = np.array([[1,2],[3,4]])print(np.sum(x)) # Compute sum of all elements; prints "10" print(np.sum(x, axis=0)) # Compute sum of each column; prints "[4 6]" print(np.sum(x, axis=1)) # Compute sum of each row; prints "[3 7]"您可以在文檔中找到numpy提供的完整數(shù)學(xué)函數(shù)列表。
除了使用數(shù)組計(jì)算數(shù)學(xué)函數(shù)之外,我們經(jīng)常需要重新整形或以其他方式操縱數(shù)組中的數(shù)據(jù)。 這種操作的最簡(jiǎn)單的例子是轉(zhuǎn)置矩陣; 要轉(zhuǎn)置矩陣,只需使用數(shù)組對(duì)象的T屬性:
import numpy as npx = np.array([[1,2], [3,4]]) print(x) # Prints "[[1 2]# [3 4]]" print(x.T) # Prints "[[1 3]# [2 4]]"# Note that taking the transpose of a rank 1 array does nothing: v = np.array([1,2,3]) print(v) # Prints "[1 2 3]" print(v.T) # Prints "[1 2 3]"Numpy提供了很多操作素組的方法,可以看這篇文檔!
Broadcasting
廣播是一種強(qiáng)大的機(jī)制,允許numpy在執(zhí)行算術(shù)運(yùn)算時(shí)使用不同形狀的數(shù)組。 我們經(jīng)常有一個(gè)較小的數(shù)組和一個(gè)較大的數(shù)組,我們希望多次使用較小的數(shù)組來(lái)對(duì)較大的數(shù)組執(zhí)行某些操作。
例如,假設(shè)我們想要向矩陣的每一行添加一個(gè)常量向量。 我們可以這樣做:
import numpy as np# We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) y = np.empty_like(x) # Create an empty matrix with the same shape as x# Add the vector v to each row of the matrix x with an explicit loop for i in range(4):y[i, :] = x[i, :] + v# Now y is the following # [[ 2 2 4] # [ 5 5 7] # [ 8 8 10] # [11 11 13]] print(y)這有效; 但是當(dāng)矩陣x非常大時(shí),在Python中計(jì)算顯式循環(huán)可能會(huì)很慢。 注意,將向量v添加到矩陣x的每一行等同于通過(guò)垂直堆疊v的多個(gè)副本來(lái)形成矩陣w,然后執(zhí)行x和w的元素求和。 我們可以像這樣實(shí)現(xiàn)這種方法:
import numpy as np# We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other print(vv) # Prints "[[1 0 1]# [1 0 1]# [1 0 1]# [1 0 1]]" y = x + vv # Add x and vv elementwise print(y) # Prints "[[ 2 2 4# [ 5 5 7]# [ 8 8 10]# [11 11 13]]"Numpy廣播允許我們執(zhí)行此計(jì)算而不實(shí)際創(chuàng)建v的多個(gè)副本。考慮此版本,使用廣播:
import numpy as np# We will add the vector v to each row of the matrix x, # storing the result in the matrix y x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]]) v = np.array([1, 0, 1]) y = x + v # Add v to each row of x using broadcasting print(y) # Prints "[[ 2 2 4]# [ 5 5 7]# [ 8 8 10]# [11 11 13]]"支持廣播的功能稱為通用功能。您可以在文檔中找到所有通用功能的列表。
以下是廣播的一些應(yīng)用:
import numpy as np# Compute outer product of vectors v = np.array([1,2,3]) # v has shape (3,) w = np.array([4,5]) # w has shape (2,) # To compute an outer product, we first reshape v to be a column # vector of shape (3, 1); we can then broadcast it against w to yield # an output of shape (3, 2), which is the outer product of v and w: # [[ 4 5] # [ 8 10] # [12 15]] print(np.reshape(v, (3, 1)) * w)# Add a vector to each row of a matrix x = np.array([[1,2,3], [4,5,6]]) # x has shape (2, 3) and v has shape (3,) so they broadcast to (2, 3), # giving the following matrix: # [[2 4 6] # [5 7 9]] print(x + v)# Add a vector to each column of a matrix # x has shape (2, 3) and w has shape (2,). # If we transpose x then it has shape (3, 2) and can be broadcast # against w to yield a result of shape (3, 2); transposing this result # yields the final result of shape (2, 3) which is the matrix x with # the vector w added to each column. Gives the following matrix: # [[ 5 6 7] # [ 9 10 11]] print((x.T + w).T) # Another solution is to reshape w to be a column vector of shape (2, 1); # we can then broadcast it directly against x to produce the same # output. print(x + np.reshape(w, (2, 1)))# Multiply a matrix by a constant: # x has shape (2, 3). Numpy treats scalars as arrays of shape (); # these can be broadcast together to shape (2, 3), producing the # following array: # [[ 2 4 6] # [ 8 10 12]] print(x * 2)廣播通常會(huì)使您的代碼更簡(jiǎn)潔,更快速,因此您應(yīng)該盡可能地使用它。
Numpy文檔
這個(gè)簡(jiǎn)短的概述涉及了許多關(guān)于numpy需要了解的重要事項(xiàng),但還遠(yuǎn)未完成。 查看numpy參考資料,了解有關(guān)numpy的更多信息。
總結(jié)
以上是生活随笔為你收集整理的CS231n Convolutional Neural Networks for Visual Recognition------Numpy Tutorial的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: kentbcli.exe是什么进程 有什
- 下一篇: 直播间粉丝超2000万 新东方主播董宇辉