PyTorch?Tensor創(chuàng)建實(shí)現(xiàn)
使用已有數(shù)據(jù)
torch.tensor(data)
>>> import torch >>> import numpy as np # 標(biāo)量 >>> torch.tensor(0) tensor(0) # 列表/元組 >>> torch.tensor([[1.0]]) tensor([[1.]]) # ndarray >>> n = np.arange(3) >>> torch.tensor(n) tensor([0, 1, 2])
可以額外指定數(shù)據(jù)類型和設(shè)備, 默認(rèn)情況下由數(shù)據(jù)本身自動推斷出類型, 整數(shù)使用 torch.int64 類型, 浮點(diǎn)數(shù)使用 torch.float32 類型
>>> torch.tensor([1.0, 2.0], dtype=torch.float16, device='cuda') tensor([1., 2.], device='cuda:0', dtype=torch.float16)
使用 torch.tensor 創(chuàng)建 Tensor 時(shí), 總是完全拷貝, 不會共享底層數(shù)據(jù)
# 不與 ndarray 共享內(nèi)存數(shù)據(jù) >>> n = np.array([1, 2]) >>> t = torch.tensor(n) >>> t[0] = 0 >>> t tensor([0, 2]) >>> n array([1, 2])
torch.as_tensor(data)
與 torch.tensor 不同, 該函數(shù)會盡量共享內(nèi)存, 當(dāng)然只有 data 是 np.ndarray 或 torch.Tensor 類型時(shí)才能共享, data 是列表或元組時(shí)沒法共享內(nèi)存
# 與 ndarray 共享內(nèi)存數(shù)據(jù) >>> n = np.array([1, 2]) # 底層調(diào)用 torch.from_numpy(n) >>> t = torch.as_tensor(n) >>> t[0] = 0 >>> t tensor([0, 2]) >>> n array([0, 2])
同樣可以指定數(shù)據(jù)類型和設(shè)備, 當(dāng)指定的類型與 data 的數(shù)據(jù)類型不一致時(shí)不共享數(shù)據(jù)
>>> d = torch.arange(3) >>> t = torch.as_tensor(d, dtype=torch.int16) >>> t[0] = -1 >>> t tensor([-1, 1, 2], dtype=torch.int16) >>> d tensor([0, 1, 2])
torch.Tensor(sequence)
相當(dāng)于直接實(shí)例化一個(gè) torch.Tensor 類型的對象, 默認(rèn)是 torch.float32 的數(shù)據(jù)類型, 設(shè)備位于 CPU
# 不支持標(biāo)量 >>> torch.Tensor(1.0) ----------------------------------------------------------- TypeError ? ? ? ? ? ? ? ? Traceback (most recent call last) Input In [40], in <cell line: 1>() ----> 1 torch.Tensor(1.0) TypeError: new(): data must be a sequence (got float) >>> n = np.array([0, 1]) # 整型被自動轉(zhuǎn)為 torch.float32 >>> torch.Tensor(n) tensor([0., 1.])
除了 torch.Tensor 還有其他的類型, 也可以這樣實(shí)例化, 例如 torch.IntTensor, torch.FloatTensor 等 CPU 張量類型, torch.cuda.IntTensor, torch.cuda.FloatTensor 等 GPU 張量類型
torch.Tensor 默認(rèn)是 torch.FloatTensor 類型即默認(rèn)張量類型, 這個(gè)可以被全局修改
# 使用 CPU 張量類型進(jìn)行實(shí)例化 >>> torch.DoubleTensor([1, 2]) tensor([1., 2.], dtype=torch.float64) # 使用 GPU 張量類型進(jìn)行實(shí)例化 >>> torch.cuda.IntTensor([1, 2]) tensor([1, 2], device='cuda:0', dtype=torch.int32)
數(shù)據(jù)未初始化
數(shù)據(jù)未初始化, 直接使用存儲設(shè)備中的原有數(shù)據(jù)
torch.empty(*sizes) # 注意與 torch.Tensor(sequence) 的區(qū)別 # 這里也可以替換為 torch.IntTensor(*sizes) 等 Tensor 類型 torch.Tensor(*sizes)
示例
>>> torch.empty(1, 2) tensor([[2.9386e+29, 7.1104e-04]]) >>> torch.cuda.IntTensor(2, 3) tensor([[0, 0, 0], ? ? ? ? [0, 0, 0]], device='cuda:0', dtype=torch.int32)
特殊張量
# 全 0 torch.zeros(*size) # 全 1 torch.ones(*size) # 指定全值 torch.full(size, fill_value) # n 行 m 列單位對角矩陣 torch.eye(n, m=None) # 對角矩陣, 參數(shù) tensor 為一維張量, 指定對角線元素 torch.diag(tensor)
除了末尾兩個(gè)函數(shù)生成的是二維張量, 其余的函數(shù)不限張量維度
代碼示例:
# 默認(rèn)數(shù)據(jù)類型為 torch.float32 >>> torch.zeros(2) tensor([0., 0.]) # 2 行 1 列 >>> torch.ones(2, 1, dtype=torch.int32) tensor([[1], ? ? ? ? [1]], dtype=torch.int32) # 指定全值 >>> torch.full([2, 2], 3) tensor([[3, 3], ? ? ? ? [3, 3]]) # 主對角線元素均為 1, 其余元素為 0 >>> torch.eye(2, 3) tensor([[1., 0., 0.], ? ? ? ? [0., 1., 0.]]) >>> torch.diag(torch.tensor([1, 3, 5])) tensor([[1, 0, 0], ? ? ? ? [0, 3, 0], ? ? ? ? [0, 0, 5]])
數(shù)列
torch.arange(start=0, end, step=1)
torch.linspace(start, end, steps=100) 間隔相等的張量
torch.logspace(start, end, steps=100, base=10.0) 以對數(shù)為間隔的張量
代碼示例
>>> torch.arange(3) tensor([0, 1, 2]) >>> torch.arange(1, 3.1, 1.0) tensor([1., 2., 3.]) >>> torch.linspace(-2, 2, 5) tensor([-2., -1., ?0., ?1., ?2.]) # 從 2^(-2) 至 2^2 >>> torch.logspace(-2, 2, steps=5, base=2) tensor([0.2500, 0.5000, 1.0000, 2.0000, 4.0000])
可以看出對于相同的 start, end 和 steps 參數(shù), logspace = base ^ linspace
隨機(jī)生成
正態(tài)分布
# 標(biāo)準(zhǔn)正態(tài)分布 torch.randn(*size) # 指定均值與標(biāo)準(zhǔn)差 torch.normal(mean, std, size)
示例
# 指定隨機(jī)數(shù)種子, 保證隨機(jī)數(shù)可以重現(xiàn) >>> _ = torch.manual_seed(2022) >>> torch.randn(2, 3) tensor([[ 0.1915, 0.3306, 0.2306], [ 0.8936, -0.2044, -0.9081]]) >>> torch.normal(mean=1.0, std=0.1, size=[2, 3]) tensor([[0.7689, 1.1635, 1.2061], [0.9746, 0.8488, 0.8720]]) # 不指定size, 由 mean 和 std 參數(shù)的形狀推斷出結(jié)果的維度 # 輸出的兩個(gè)隨機(jī)數(shù)分別服從均值為 1.0 和 2.0 標(biāo)準(zhǔn)差為 0.1 的正態(tài)分布 # 顯然, 兩個(gè)數(shù)分別在 1.0 和 2.0 的附近(標(biāo)準(zhǔn)差故意選的很小) >>> torch.normal(mean=torch.Tensor([1.0, 2.0]), std=0.1) tensor([1.0111, 2.0205])
均勻分布
# [0, 1] 上的均勻分布 torch.rand(*size)
示例
# [2, 4] 上的均勻分布 >>> 2 * torch.rand(2, 2) + 2 tensor([[2.4388, 2.5786], [3.3569, 2.9994]])
隨機(jī)序列
# 0, 1, 2, ..., n-1 隨機(jī)排列 torch.randperm(n)
示例
>>> _ = torch.manual_seed(2022) >>> torch.randperm(6) tensor([5, 1, 3, 2, 0, 4])
隨機(jī)整數(shù)
# 隨機(jī)生成 low 到 high - 1 的整數(shù), 包括 low 和 high - 1 這兩個(gè)整數(shù) torch.randint(low=0, high, size)
示例
>>> torch.randint(5, [2, 3]) tensor([[1, 0, 3], [1, 4, 2]]) >>> torch.randint(3, 6, [2, 2]) tensor([[5, 4], [4, 3]])
繼承張量類型
使用 Tensor.new_*() 的方式新建一個(gè)張量, 該張量與調(diào)用者具有相同的張量類型
例如:
# 未初始化 Tensor.new(*sizes) Tensor.new_empty(size) # 全 0 Tensor.new_zeros(size) # 全 1 Tensor.new_ones(size) # 指定初始值 Tensor.new_full(size, fill_value)
示例
>>> t = torch.cuda.IntTensor([2]) >>> t tensor([2], device='cuda:0', dtype=torch.int32) # 繼承了數(shù)據(jù)類型以及設(shè)備類型 >>> t.new_full([1, 2], 1) tensor([[1, 1]], device='cuda:0', dtype=torch.int32)
繼承維度以及張量類型
使用 torch.*_like(other) 的方式新建一個(gè)張量, 該張量與 other 張量具有相同的形狀和張量類型
例如:
# 未初始化 torch.empty_like(other) # 全 0 torch.zeros_like(other) # 全 1 torch.ones_like(other) # 指定初始值 torch.full_like(other, fill_value) # 均勻分布 torch.rand_like(other) # 標(biāo)準(zhǔn)正態(tài)分布 torch.randn_like(other) # 隨機(jī)整數(shù) torch.randint_like(other, low=0, high)
示例
>>> t = torch.tensor([[1, 2]], dtype=torch.int16, device='cuda') >>> t tensor([[1, 2]], device='cuda:0', dtype=torch.int16) >>> f = torch.zeros_like(t) # 繼承了 t 的形狀以及張量類型 >>> f tensor([[0, 0]], device='cuda:0', dtype=torch.int16)
到此這篇關(guān)于PyTorch Tensor創(chuàng)建實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyTorch Tensor創(chuàng)建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決python -m pip install --upgrade pip 升級不成功問題
這篇文章主要介紹了python -m pip install --upgrade pip 解決升級不成功問題,需要的朋友可以參考下2020-03-03Django Auth應(yīng)用實(shí)現(xiàn)用戶身份認(rèn)證
Django Auth 應(yīng)用一般用在用戶的登錄注冊上,用于判斷當(dāng)前的用戶是否合法。本文將介紹Auth的另一個(gè)功能,即認(rèn)證用戶身份,感興趣的同學(xué)可以關(guān)注一下2021-12-12手把手教你進(jìn)行Python虛擬環(huán)境配置教程
這篇文章主要介紹了手把手教你進(jìn)行Python虛擬環(huán)境配置,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧,需要的朋友可以參考下2020-02-02Python實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01pygame實(shí)現(xiàn)井字棋之第三步邏輯優(yōu)化
這篇文章主要介紹了pygame實(shí)現(xiàn)井字棋之第三步邏輯優(yōu)化,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們也有非常好的幫助,需要的朋友可以參考下2021-05-05Python實(shí)現(xiàn)對數(shù)坐標(biāo)系繪制與自定義映射
這篇文章主要為大家學(xué)習(xí)介紹了如何利用Python實(shí)現(xiàn)對數(shù)坐標(biāo)系繪制與坐標(biāo)自定義映射,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-08-08Python使用Tabulate庫實(shí)現(xiàn)格式化表格數(shù)據(jù)
在數(shù)據(jù)分析和軟件開發(fā)中,表格數(shù)據(jù)的展示是一個(gè)常見的需求,無論是簡單的數(shù)據(jù)報(bào)告,還是復(fù)雜的數(shù)據(jù)可視化,表格都是一種直觀且有效的信息展示方式,tabulate庫是一個(gè)非常實(shí)用的工具,它可以幫助我們輕松地將數(shù)據(jù)格式化為各種表格形式,本文將詳細(xì)介紹tabulate庫的使用方法2025-02-02python實(shí)現(xiàn)將漢字保存成文本的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)將漢字保存成文本的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Python實(shí)現(xiàn)url長短鏈接的轉(zhuǎn)換
短鏈接,通俗來說,就是將長的URL網(wǎng)址,通過程序計(jì)算等方式,轉(zhuǎn)換為簡短的網(wǎng)址字符串。本文將用Python語言實(shí)現(xiàn)這一效果,需要的可以參考一下2022-11-11