柚子快報(bào)邀請(qǐng)碼778899分享:學(xué)習(xí)Numpy的奇思妙想
柚子快報(bào)邀請(qǐng)碼778899分享:學(xué)習(xí)Numpy的奇思妙想
學(xué)習(xí)Numpy的奇思妙想
本文主要想記錄一下,學(xué)習(xí) numpy 過(guò)程中的偶然的靈感,并記錄一下知識(shí)框架。 推薦資源:https://numpy.org/doc/stable/user/absolute_beginners.html
?靈感
為什么 numpy 數(shù)組的 shape 和 pytorch 是 tensor 是反著的??
在讀入一個(gè) RGB 圖像的時(shí)候,pytorch 的張量通常是(batch, channel, height, width),但是 numpy 的數(shù)組形狀通常是(height, width, channel)把數(shù)組轉(zhuǎn)換成張量直接用 transform.ToTensor(),但是在把 tensor 轉(zhuǎn)換成張量并用matplotlib 顯示前要注意轉(zhuǎn)換維度。from torchvision import transforms, datasets
from torch.utils.data import DataLoader
from PIL import Image
# 定義轉(zhuǎn)換操作,將圖片轉(zhuǎn)換為 tensor
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.ToTensor(),
])
# 加載單個(gè)圖片
image_pil = Image.open('path_to_image.jpg')
image_tensor = transform(image_pil)
# 顯示圖片形狀
print(image_tensor.shape) # 輸出可能是 (channels, height, width)
# 注意:PyTorch 的 tensor 需要先轉(zhuǎn)置維度,然后才能用 matplotlib 顯示
plt.imshow(image_tensor.permute(1, 2, 0))
plt.show()
?猜想一下 numpy 是如何計(jì)算數(shù)組形狀的,可能是numpy得到一個(gè)輸入的列表,會(huì)先查看他的 len,得到一個(gè)數(shù),這個(gè)就是第 0 維度,然后查看數(shù)組中第一個(gè)元素的 len,這個(gè)就是第 1 維度,以此類推。就像剝洋蔥一樣,一層一層的剝開(kāi)他的心。import numpy as np
a = [[[1,2,3],[4,5,6]]]
print(len(a)) # 1
print(len(a[0])) # 2
print(len(a[0][0])) # 3
print(np.array(a).shape) # (1,2,3)
圖片保存的時(shí)候,RGB 統(tǒng)一保存成一個(gè)顏色,比如#FFFFFF,他是在一起的,所以 channel 對(duì)于 numpy 來(lái)說(shuō)在最后邊。 ?? list 的索引返回副本(深拷貝),ndarry 的索引返回視圖(淺拷貝)
這個(gè)是例子import numpy as np
a_list = [1,2,3,4,5,6]
a_array = np.array(a_list)
b_list = a_list[0:4]
b_list[0] = 100
print(b_list, a_list)
# [100, 2, 3, 4] [1, 2, 3, 4, 5, 6]
b_array = a_array[0:4]
b_array[0] = 100
print(b_array, a_array)
# [100 2 3 4] [100 2 3 4 5 6]
同樣,展平數(shù)組時(shí),.flatten() 和 .ravel()的區(qū)別也是如此, ravel() 創(chuàng)建的新數(shù)組實(shí)際上是對(duì)父數(shù)組的引用(即,“視圖”)。這意味著對(duì)新數(shù)組的任何更改也會(huì)影響父數(shù)組。由于 ravel() 不創(chuàng)建副本,因此它的內(nèi)存效率很高。 ?? empty 不是真的 empty
np.empty 創(chuàng)建的并不是0,他直接在內(nèi)存上開(kāi)辟空間,存儲(chǔ)的是“隨機(jī)”的內(nèi)容,可能全是 0,也可能不是 0。np.zeros 創(chuàng)建的才是真正的 0。np.random.rand 創(chuàng)建的才是真正的隨機(jī)。
Numpy組織結(jié)構(gòu)
https://numpy.org/doc/stable/reference/module_structure.html
【推薦使用】Main namespaces(Regular/recommended user-facing namespaces for general use)
numpynumpy.exceptionsnumpy.fftnumpy.linalgnumpy.polynomialnumpy.randomnumpy.stringsnumpy.testingnumpy.typing 【推薦使用】Special-purpose namespaces
numpy.ctypeslib - interacting with NumPy objects with ctypesnumpy.dtypes - dtype classes (typically not used directly by end users)numpy.emath - mathematical functions with automatic domainnumpy.lib - utilities & functionality which do not fit the main namespacenumpy.rec - record arrays (largely superseded by dataframe libraries)numpy.version - small module with more detailed version info 【不建議使用】Legacy namespaces(Prefer not to use these namespaces for new code. There are better alternatives and/or this code is deprecated or isn’t reliable.)
numpy.char - legacy string functionality, only for fixed-width stringsnumpy.distutils (deprecated) - build system supportnumpy.f2py - Fortran binding generation (usually used from the command line only)numpy.ma - masked arrays (not very reliable, needs an overhaul)numpy.matlib (pending deprecation) - functions supporting matrix instances
最后附上思維導(dǎo)圖,以后有機(jī)會(huì)可以探索更多的細(xì)節(jié)
柚子快報(bào)邀請(qǐng)碼778899分享:學(xué)習(xí)Numpy的奇思妙想
推薦鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。