1. 创建tensor的方法
import torch
x = torch.empty(5, 3) # 创建未初始化的矩阵
x = torch.rand(5, 3) # 创建随机初始化矩阵
x = torch.zeros(5, 3, dtype=torch.long) # 创建 0 矩阵
x = torch.tensor([5.5, 3]) # 直接从数据构造张量
x = x.new_ones(5, 3, dtype=torch.double) # he returned Tensor has the same `torch.dtype` `torch.device` as this tensor.
x = torch.randn_like(x, dtype=torch.float) # 重载 dtype!
x.size() # 获取张量形状
2. 运算
y = torch.rand(5, 3) # 加法形式 1
print(torch.add(x, y)) # 加法形式 2
result = torch.empty(5, 3)
torch.add(x, y, out=result) # 加法形式 2+
y.add_(x) # 加法形式 3
x[:, 1] # 切片操作
x = torch.randn(4, 4)
y = x.view(16) # 改变形状
z = x.view(-1, 8) # -1 自动计算维度大小
x = torch.randn(1)
x.item() # item() 方法获取 python 数值
3. 与Numpy转换
4年前老公在北京買瞭個玉的貔貅給我''就直接帶脖子上瞭!當時也不知道什麼開光,請啊什麼的就帶瞭大半年!後來懷孕瞭就給老爸保管瞭!現在想在拿出來帶!可以嗎?是不是要開光什麼的!
朋友送瞭我一個他自己戴過的貔貅 我不戴 我該怎麼處理 可不可以直接還給他 還是要怎麼辦? - 2to在線問答
誰
我有一個玉貔貅好長時間不戴瞭!不想要瞭怎麼處理? - 2to在線問答
我有一個玉貔貅好長時間不戴瞭!不想要瞭怎麼處理?
請回來的戴貔貅不適合佩戴要怎麼辦
網站簡介
a = torch.ones(5)
b = a.numpy() # tensor 转换为 numpy
a.add_(1) # a 改变,b 也会跟着变
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a) # numpy 转换为 pytorch