import numpy as np
import torch
print(torch.__version__)defcreat_tensor():
t1 = torch.tensor(7)#创建一个一阶张量print(t1)print(t1.shape)#形状print(t1.dtype)#它的类型(它指t1中的数据的类型)print(type(t1))defcreat_tensor2():
data = np.array([10,20,30,40])
t1 = torch.tensor(data)#创建一个一阶张量print(t1.shape)#形状print(t1.dtype)#它的类型(它指t1中的数据的类型)print(type(t1))defcreat_tensor3():
data =[10,20,30,40]
t1 = torch.tensor(data)#创建一个一阶张量print(t1.shape)#形状print(t1.dtype)#它的类型(它指t1中的数据的类型)print(type(t1))defcreat_tensor4():
t = torch.Tensor(3,4)print(t)print(t.shape)#np阶段是(3,4)defcreat_tensor5():
t = torch.Tensor([[10,20,30],[10,20,30]])print(t.shape)print(t.device)defcreat_tensor6():
t = torch.IntTensor([10,20,30])# t = torch.FloatTensor([10,20,30])# t = torch.DoubleTensor([10,20,30])# t = torch.LongTensor([10,20,30])# t = torch.HalfTensor([10,20,30])# t = torch.BoolTensor([True,False])# t = torch.tensor([10,20,257],dtype=torch.int8,device='cuda')print(t)print(t.shape)print(t.dtype)print(t.device)if __name__ =='__main__':
creat_tensor4()
import torch
import numpy as np
data = np.array([[1,2,3],[12,34,56]])
data = torch.tensor(data)
x = torch.ones_like(data)print(x)
import torch
# 创建一个 2x2 的浮点型张量
a = torch.Tensor([[1,2],[3,4]])print(a)print(a.dtype)# 默认是 torch.float32# 创建一个 2x2 的整型张量,需要指定数据类型
b = torch.tensor([[1,2],[3,4]]).type(torch.float64)print(b)print(b.dtype)# torch.int32
x = torch.tensor([1,2,3],dtype=torch.float32)print(x)
x = torch.Tensor([1,2,3]).type(torch.int32)print(x)
x = torch.Tensor([1,2,3]).to('cuda')print(x.device)
创建线性和随机张量
import torch
#关闭科学计数法的显示#torch.set_printoptions(sci_mode=False)#线性张量deftest01():
x = torch.arange(1,10,2)#从1开始到10结束(不包括10,[1,10)),步长为2,等差数列print(x)
y = torch.linspace(1,10,5)#也是等差数列,[1,10],取step个数,(end-start)/(step-1)print(y)
z = torch.logspace(1,10,3, base=2)#等比数列,base=2表示数列为2的指数,[2的1次方。。。。]step为等比数列个数print(z, z.dtype)#随机张量 deftest02():#设置随机数种子
torch.manual_seed(666)#获取当前随机数种子
r = torch.initial_seed()print(r)#生成随机张量
x = torch.rand(10,5)# np.random.rand(10,5)print(x)#生成标准正态分布随机张量
y = torch.randn(3,3)print(y)#自己配置方差和均值
z = torch.normal(2,1,(4,4))print(z)if __name__ =='__main__':
test01()
创建01张量
import numpy as np
import torch
deftest01():
x = torch.zeros(3,3)print(x)
y = torch.tensor([[10,20,30],[22,33,44]])
y = torch.rand(3,2)# 传入的数据容器只能是tensor,不能列表或者数组# y = np.array([1,2,3])# y = [12,3,4]#解决方法:
y = torch.tensor(np.array([1,2,3]))
y = torch.zeros_like(y)print(y)deftest02():
x = torch.ones(3,5)print(x)
y = torch.tensor([1,2,3])
y = torch.rand(3,2)
y = torch.ones_like(y)print(y)deftest03():
x = torch.full((3,4),999)print(x)
y = torch.full_like(x,2)print(y)deftest04():
x = torch.eye(4)print(x)if __name__ =='__main__':
test04()
常见的属性和类型转换
import torch
from numpy import dtype
deftest01():# torch.Tensor()
x = torch.tensor([1,2,3], device='cuda')#可以指定创建设备,CUDA或者cpuprint(x, x.dtype)#tensor中的数据类型print(x.device)#tensor在哪个设备上运行,默认在cpu上#[]0介张量标量#[3]1阶张量,有3个数据#[3,4]2介张量,有3行4列#[4,2,512,512]4介张量,第1层有4个,每个中有2个512*512的矩阵print(x.shape)#获取tensor形状deftest02():#不同的设备上的tensor是不能参与运算的# x = torch.tensor([1, 20, 30], device='cuda')# x2 = torch.tensor([2, 33, 44], device='cpu')# print(x)# print(x2)# c = x + x2# print(c)#设备切换方法1
x = torch.tensor([1,2,3], device='cuda')print(1, x)#设备切换方法2
y = torch.tensor([4,5,6])
z = y.to('cuda:0')#会返回一个新的z,这个新的y在cuda上,原y在cpu上print(2, z.device)#可以通过API获取当前设备中是否有cuda
res = torch.cuda.is_available()print(res)#设备上有显卡返回True否则返回False
c =1if100>10else0print(c)
x.to('cuda'if torch.cuda.is_available()else'cpu')print(x.device)#设备切换方法3#把创建的tensor移动到cuda上
x = torch.tensor([123,123])print(x.device)
x = x.cuda()#返回一个新的x,这个新的x在cuda上,原变量的值依然在cpu上print(x.device)
x = x.cuda()if torch.cuda.is_available()else x
deftest03():#类型转换1
x = torch.tensor([10,20,30,40],dtype=torch.float16)print(x)#类型转换2
x = x.type(torch.int8)#type函数不会修改原数据,会返回一个新数据print(x.dtype)#类型转换3
x = x.half()print(x.dtype)
x = x.double()print(x.dtype)
x = x.float()print(x.dtype)
x = x.long()print(x.dtype)
x = x.int()print(x.dtype)if __name__ =='__main__':
test03()