行业网站设计师招聘,沧州网站建设开发服务,电商seo是什么意思啊,荆州网站建设费用简单记录以便查阅
张量
一、创建张量
x torch.empty(5,3) # 创建未初始化矩阵张量
x torch.rand(5,3) # 创建初始化随机矩阵张量
x torch.zeros(5,3,dtypetorch.long) # 创建0填充矩阵张量
x torch.tensor([5.5,3]) …简单记录以便查阅
张量
一、创建张量
x torch.empty(5,3) # 创建未初始化矩阵张量
x torch.rand(5,3) # 创建初始化随机矩阵张量
x torch.zeros(5,3,dtypetorch.long) # 创建0填充矩阵张量
x torch.tensor([5.5,3]) # 使用现有数据初始化张量
x x.new_ones(5,3, dtypetorch.double) # 使用new_*来创建对象
x torch.randn_like(x, dtypetorch.float) # 根据现有张量创建张量torch.arange(start, end, step1, dtypetorch.int32)
torch.full(size, fill_value) # 张量填充
torch.normal(mean, std, outNone) # 正态分布二、张量尺寸
x.size()
x.shape三、张量维度
x torch.squeeze(x) # 去掉大小为1的维度
x torch.unsqueeze(x,3) # 在第三维增加1个维度
x torch.transpose(x, 1, 2) # 交换维度1和维度2
x torch.permute(1,2,3,0) # 交换多个维度/维度重组四、张量操作
x y / torch.add(x, y) / y.add_(x) # 四则运算
x[:, 1] # 切片操作
x x.view(-1, 8) # 改变维度(-1维度自动推断)
x x.reshape(8,-1) # 改变维度
x.item() # 取张量的数值前提张量只有一个元素
torch.cat((x,y), dim0) # 张量拼接在原有的某一维度上进行连接
torch.stack((x,y), dim0) # 张量拼接创建一个新的维度将原有维度在这个维度上进行顺序排列[详细](https://blog.csdn.net/TH_NUM/article/details/83088915)
torch.chunk(a, chunk_num, dim0) # 张量拆分在指定维度上将a变成chunk_num个大小相等的chunk返回一个tuple。如果最后一个不够chunk_num就返回剩下的
torch.split(a, chunk_size, dim0) # 张量拆分同上五、张量类型转换
1.Torch Tensor与NumPy数组共享底层内存地址
b a.numpy() # tensor 转 numpy
b torch.from_numpy(a) # numpy 转 tensorb a.long() # torch.int64
c a.half() # torch.float16
d a.int() # torch.int32
e a.double() # torch.float64
f a.float() # torch.float32
g a.char() # torch.int8
h a.byte() # torch.uint8
j a.short() # torch.int16
c a.type_as(c) # 转化 a 的数据格式与 c 相同六、CUDA 张量
# is_available 函数判断是否有cuda可以使用
# torch.device将张量移动到指定的设备中
if torch.cuda.is_available():device torch.device(cuda) # a CUDA 设备对象y torch.ones_like(x, devicedevice) # 直接从GPU创建张量x x.to(device) # 或者直接使用.to(cuda)将张量移动到cuda中z x yprint(z)print(z.to(cpu, torch.double)) # .to 也会对变量的类型做更改