Tensor的引入
對(duì)于學(xué)習(xí)和使用一個(gè)開(kāi)源庫(kù)/框架馋没,開(kāi)發(fā)者最關(guān)心的2個(gè)問(wèn)題:如何傳參數(shù),如何調(diào)用API接口。 參數(shù)一般就是指數(shù)據(jù)類(lèi)型/數(shù)據(jù)結(jié)構(gòu)類(lèi)型, API接口具體指可以調(diào)用的函數(shù)齐莲、方法。
Tensor是pytorh中的數(shù)據(jù)類(lèi)型,表示一個(gè)N dims張量磷箕。在深度學(xué)習(xí)中,輸入/輸出數(shù)據(jù)阵难,網(wǎng)絡(luò)參數(shù)都用Tensor表示岳枷。
A torch.Tensor
is a multi-dimensional matrix containing elements of a single data type.
每種深度學(xué)習(xí)框架都有其數(shù)據(jù)結(jié)構(gòu):
Framework | 數(shù)據(jù)類(lèi)型 | 特點(diǎn) |
---|---|---|
Caffe | Blob | N 維矩陣 |
TensorFlow | Tensor | N 維矩陣 |
pytorch | Tensor | N 維矩陣 |
不同類(lèi)型的Tensor
pytorch中的Tensor
torch.Tensor
torch定義了7中CPU類(lèi)型的Tensor, 以及8種GPU類(lèi)型的Tensor
https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/
數(shù)據(jù)類(lèi)型 | CPU Tensor | GPU Tensor |
---|---|---|
32bit float | torch.FloatTensor |
torch.cunda.FloatTensor |
64bit float | torch.DoubleTensor |
torch.cunda.DoubleTensor |
16bit float | N/A |
torch.cuda.HalfTensor |
8bit signed int | torch.ByteTensor |
torch.cuda.ByteTensor |
8bit unsigned int | torch.CharTensor |
torch.cuda.CharTensor |
16bit signed int | torch.ShortTensor |
torch.cuda.ShortTensor |
32bit signed int | torch.IntTensor |
torch.cuda.IntTensor |
64bit signed int | torch.LongTensor |
torch.cuda.LongTensor |
開(kāi)發(fā)環(huán)境
- Ubuntu 18.04
- pytorch 1.0.0
- Anconda3 python3.6.6
- pycharm
Tensor的創(chuàng)建以及常用方法
# 導(dǎo)入pytorch
import torch
# 創(chuàng)建一個(gè)3x3全1矩陣,Tensor
x1 = torch.ones(3, 3)
print(x1)
# 5x5 全0矩陣
x2 = torch.zeros(5, 5)
print(x2)
# 與x1同維0矩陣
x4 = torch.zeros_like(x1)
print(x4)
# 與x1同維全1矩陣
x5 = torch.ones_like(x1)
print(x5)
# 對(duì)角矩陣
x6 = torch.diag(torch.from_numpy(np.array([1, 2, 3, 4, 5])))
print(x6)
# 5x5 隨機(jī)矩陣
x7 = torch.rand(5, 5)
print(x7)
# 5x5 norm分布矩陣
x8 = torch.randn(5, 5)
print(x8)
# 創(chuàng)建一個(gè)empty Tensor
x9 = torch.empty(3, 3)
print(x9)
# 創(chuàng)建一個(gè)Tensor,給定數(shù)值
x10 = torch.Tensor([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
print(x10)
# 根據(jù)已有的Tensor創(chuàng)建一個(gè)新的Tensor
x11 = torch.rand_like(x10, dtype=torch.float)
print(x11)
# 獲取Tensor的size, Tensor.Size 實(shí)際上是一個(gè)Tuple
print(x11.size())
# Tensor的in place 操作,會(huì)改變Tensor本身
x12 = torch.rand(3, 3)
print(x12.t_())
print(x12.copy_(x11))
# Tensor resize/reshape操作
x13 = torch.randn(4, 4)
x14 = x13.view(16) #16*1
print(x14)
x15 = x13.view(-1, 8) # -1, 此維度根據(jù)另一個(gè)維度計(jì)算得到
print(x15)
# 只有一個(gè)數(shù)的Tensor, 使用xxx.item() 得到python 數(shù)值
x16 = torch.rand(1)
print(x16)
print(x16.item())
# 獲取Tensor中元素的個(gè)數(shù)
x17 = torch.randn(1, 2, 3, 4, 5)
print(torch.numel(x17))
print(torch.numel(torch.zeros(4, 5)))
# 判斷一個(gè)對(duì)象是否是Tensor
print(torch.is_tensor(x17))
print(torch.is_tensor(np.array([1, 2, 3, 4])))
# 判斷一個(gè)對(duì)象是否為Pytorch storage object
print(torch.is_storage(torch.empty(3, 3))) #False???
print(torch.is_storage(np.zeros(shape=(3, 3)))) #False
# 設(shè)置Tensor的數(shù)據(jù)類(lèi)型呜叫,初始默認(rèn)為torch.float32
print(torch.Tensor([1.2, 3]).dtype) #float32
torch.set_default_dtype(torch.float64)
print(torch.Tensor([1.2, 3]).dtype) #float64
# 獲取默認(rèn)的Tensor數(shù)據(jù)類(lèi)型
print(torch.get_default_dtype()) #float64
torch.set_default_dtype(torch.float32)
print(torch.get_default_dtype()) #float32
Tensor常見(jiàn)的操作
- 加減乘除
- 矩陣運(yùn)算
- 切片
rom __future__ import print_function
import torch
'''
pytorch矩陣操作
var = torch.Tensor() 返回一個(gè)Tensor
tensor1 = torch.Tensor(3, 3)
tensor2 = torch.Tensor(3, 3)
var2 = torch.add(tensor1, tensor2) # 矩陣加
var3 = torch.sub(tensor1, tensor2) # 減
var4 = torch.mul(tensor1, tensor2) # 乘
var5 = torch.div(tensor1, tensor2) # 矩陣點(diǎn)除
var6 = torch.mm(tensor1, tensor2) # 矩陣乘
'''
print(dir(torch.Tensor))
print(help(torch.tensor))
x1 = torch.Tensor(5, 3) # 構(gòu)造一個(gè)5x3的Tensor
x2 = torch.rand(5, 3) # 構(gòu)造一個(gè)隨機(jī)初始化的Tendor
print(x1.size())
print(x2.size())
# #####################Tensor相加################################
# 2個(gè)Tensor相加
y = torch.rand(5, 3)
var1 = torch.add(x1, y)
# 2個(gè)Tensor相加
var2 = x2 + y
var3 = torch.rand(5, 3)
# 2個(gè)Tensor相加
torch.add(x1, y, out=var3)
print(var3)
# Tensor相加空繁,y.add_() 會(huì)改變y的值
y.add_(x2)
print(y)
# #####################Tensor相減###############################
x3 = torch.rand(5, 5)
x4 = torch.rand(5, 5)
y2 = x3 - x4
y3 = torch.sub(x3, x4)
print(x3.sub_(x4))
# ###################Tensor相乘################################
x5 = torch.rand(3, 3)
x6 = torch.rand(3, 3)
y4 = x5 * x6 # 矩陣元素點(diǎn)乘
y5 = torch.mul(x5, x6)
print(x5.mul_(x6))
# ###################Tensor相除################################
x7 = torch.rand(5, 5)
x8 = torch.rand(5, 5)
y6 = x7 / x8
y7 = torch.div(x7, x8)
print(x7.div_(x8))
# #################Tensor矩陣乘################################
x9 = torch.rand(3, 4)
x10 = torch.rand(4, 3)
y8 = torch.mm(x9, x10)
# print(x9.mm_(x10)) # 錯(cuò)誤用法
# ###################矩陣切片###################################
# 矩陣切片
var4 = x2[:, 1]
print(var4)
Tensor與numpy.ndarray的轉(zhuǎn)換
在python中,numpy的使用很廣泛朱庆,numpy.ndarray表示一個(gè)高維的列表/容器盛泡。在數(shù)值計(jì)算中,numpy.ndarray表示一個(gè)高維的矩陣娱颊。與numpy相比傲诵,python中的Tensor既可以利用CPU計(jì)算凯砍,也可以利用GPU計(jì)算,然而numpy只能在CPU上計(jì)算拴竹。
import numpy as np
import torch
'''
torch.Tensor與numpy.ndarray的相互轉(zhuǎn)換
'''
x1 = torch.Tensor(5, 5)
# 將Tensor轉(zhuǎn)為numpy的ndarray
var1 = x1.numpy()
print(var1)
# 將numpy的ndarray轉(zhuǎn)為T(mén)ensor
x2 = np.ndarray(shape=(5, 5), dtype=np.int32)
var2 = torch.from_numpy(x2)
print(var2)