欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch?Tensor創(chuàng)建實現(xiàn)

 更新時間:2023年06月30日 10:12:30   作者:jiang_huixin  
本文主要介紹了PyTorch?Tensor創(chuàng)建實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

使用已有數(shù)據(jù)

torch.tensor(data)

>>> import torch
>>> import numpy as np
# 標量
>>> 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ù)據(jù)本身自動推斷出類型, 整數(shù)使用 torch.int64 類型, 浮點數(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ù)據(jù)

# 不與 ndarray 共享內存數(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ù)會盡量共享內存, 當然只有 data 是 np.ndarray 或 torch.Tensor 類型時才能共享, data 是列表或元組時沒法共享內存

# 與 ndarray 共享內存數(shù)據(jù)
>>> n = np.array([1, 2])
# 底層調用 torch.from_numpy(n)
>>> t = torch.as_tensor(n)
>>> t[0] = 0
>>> t
tensor([0, 2])
>>> n
array([0, 2])

同樣可以指定數(shù)據(jù)類型和設備, 當指定的類型與 data 的數(shù)據(jù)類型不一致時不共享數(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)

相當于直接實例化一個 torch.Tensor 類型的對象, 默認是 torch.float32 的數(shù)據(jù)類型, 設備位于 CPU

# 不支持標量
>>> 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])
# 整型被自動轉為 torch.float32
>>> torch.Tensor(n)
tensor([0., 1.])

除了 torch.Tensor 還有其他的類型, 也可以這樣實例化, 例如 torch.IntTensor, torch.FloatTensor 等 CPU 張量類型, torch.cuda.IntTensor, torch.cuda.FloatTensor 等 GPU 張量類型

torch.Tensor 默認是 torch.FloatTensor 類型即默認張量類型, 這個可以被全局修改

# 使用 CPU 張量類型進行實例化
>>> torch.DoubleTensor([1, 2])
tensor([1., 2.], dtype=torch.float64)
# 使用 GPU 張量類型進行實例化
>>> torch.cuda.IntTensor([1, 2])
tensor([1, 2], device='cuda:0', dtype=torch.int32)

數(shù)據(jù)未初始化

數(shù)據(jù)未初始化, 直接使用存儲設備中的原有數(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)

除了末尾兩個函數(shù)生成的是二維張量, 其余的函數(shù)不限張量維度

代碼示例:

# 默認數(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

隨機生成

正態(tài)分布

# 標準正態(tài)分布
torch.randn(*size)
# 指定均值與標準差
torch.normal(mean, std, size)

示例

# 指定隨機數(shù)種子, 保證隨機數(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ù)的形狀推斷出結果的維度
# 輸出的兩個隨機數(shù)分別服從均值為 1.0 和 2.0 標準差為 0.1 的正態(tài)分布
# 顯然, 兩個數(shù)分別在 1.0 和 2.0 的附近(標準差故意選的很小)
>>> 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]])

隨機序列

# 0, 1, 2, ..., n-1 隨機排列
torch.randperm(n)

示例

>>> _ = torch.manual_seed(2022)
>>> torch.randperm(6)
tensor([5, 1, 3, 2, 0, 4])

隨機整數(shù)

# 隨機生成 low 到 high - 1 的整數(shù), 包括 low 和 high - 1 這兩個整數(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_*() 的方式新建一個張量, 該張量與調用者具有相同的張量類型

例如:

# 未初始化
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ù)類型以及設備類型
>>> t.new_full([1, 2], 1)
tensor([[1, 1]], device='cuda:0', dtype=torch.int32)

繼承維度以及張量類型

使用 torch.*_like(other) 的方式新建一個張量, 該張量與 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)
# 標準正態(tài)分布
torch.randn_like(other)
# 隨機整數(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)

到此這篇關于PyTorch Tensor創(chuàng)建實現(xiàn)的文章就介紹到這了,更多相關PyTorch Tensor創(chuàng)建內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 對pytorch中的梯度更新方法詳解

    對pytorch中的梯度更新方法詳解

    今天小編就為大家分享一篇對pytorch中的梯度更新方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 解決python -m pip install --upgrade pip 升級不成功問題

    解決python -m pip install --upgrade pip 升級不成功問題

    這篇文章主要介紹了python -m pip install --upgrade pip 解決升級不成功問題,需要的朋友可以參考下
    2020-03-03
  • Django Auth應用實現(xiàn)用戶身份認證

    Django Auth應用實現(xiàn)用戶身份認證

    Django Auth 應用一般用在用戶的登錄注冊上,用于判斷當前的用戶是否合法。本文將介紹Auth的另一個功能,即認證用戶身份,感興趣的同學可以關注一下
    2021-12-12
  • 手把手教你進行Python虛擬環(huán)境配置教程

    手把手教你進行Python虛擬環(huán)境配置教程

    這篇文章主要介紹了手把手教你進行Python虛擬環(huán)境配置,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧,需要的朋友可以參考下
    2020-02-02
  • Python實現(xiàn)學生成績管理系統(tǒng)

    Python實現(xiàn)學生成績管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)學生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • pygame實現(xiàn)井字棋之第三步邏輯優(yōu)化

    pygame實現(xiàn)井字棋之第三步邏輯優(yōu)化

    這篇文章主要介紹了pygame實現(xiàn)井字棋之第三步邏輯優(yōu)化,文中有非常詳細的代碼示例,對正在學習python的小伙伴們也有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • Python實現(xiàn)對數(shù)坐標系繪制與自定義映射

    Python實現(xiàn)對數(shù)坐標系繪制與自定義映射

    這篇文章主要為大家學習介紹了如何利用Python實現(xiàn)對數(shù)坐標系繪制與坐標自定義映射,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-08-08
  • Python使用Tabulate庫實現(xiàn)格式化表格數(shù)據(jù)

    Python使用Tabulate庫實現(xiàn)格式化表格數(shù)據(jù)

    在數(shù)據(jù)分析和軟件開發(fā)中,表格數(shù)據(jù)的展示是一個常見的需求,無論是簡單的數(shù)據(jù)報告,還是復雜的數(shù)據(jù)可視化,表格都是一種直觀且有效的信息展示方式,tabulate庫是一個非常實用的工具,它可以幫助我們輕松地將數(shù)據(jù)格式化為各種表格形式,本文將詳細介紹tabulate庫的使用方法
    2025-02-02
  • python實現(xiàn)將漢字保存成文本的方法

    python實現(xiàn)將漢字保存成文本的方法

    今天小編就為大家分享一篇python實現(xiàn)將漢字保存成文本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python實現(xiàn)url長短鏈接的轉換

    Python實現(xiàn)url長短鏈接的轉換

    短鏈接,通俗來說,就是將長的URL網(wǎng)址,通過程序計算等方式,轉換為簡短的網(wǎng)址字符串。本文將用Python語言實現(xiàn)這一效果,需要的可以參考一下
    2022-11-11

最新評論