Last updated by Yarco on Sat, 24 Aug 2019 10:08:44 +0000
IntTensor, LongTensor,
FloatTensor, DoubleTensor
obj.type() 或 isinstance(obj, torch.FloatTensor)
obj.shape
obj.size()
obj.dim()
torch.from_numpy(…)
torch.tensor([…])
torch.empty(shape)
torch.Tensor(shape)
torch.set_default_tensor_type(torch.DoubleTensor) // Reinforce Learning 增强学习中通常用DoubleTensor
torch.rand(shape): 随机均匀初始化, 介于[0,1]
torch.randint(min, max, shape)
torch.randn(shape): 正态分布
torch.normal(mean=, std=)
*_like(tensor): 同shape
torch.full(shape, value)
torch.full([], value) – scalar
torch.arange(left, right, step) – right不包含
torch.linspace(0, 10, steps=4) – 包含0, 10等分切割4份
torch.logspace(0, -1, steps=10) – 最后一个值是 0.1
torch.randperm(10) – 生成随机打散的[0, 10)数列
a[:2] first 2 a[:] 所有 a[1:] first 1 to end a[-1:] last 1 to end
a[::2]
obj.index_select(dim, index)
Ex.: a[:, …, :2]
flatten之后的index
两个是一样的, reshape是为了与numpy api一致
view_as(obj)
unsqueeze – 增加维度, 数值为 [-a.dim()-1, a.dim()+1)
squeeze() – 尽可能缩减维度
squeeze(idx) – 根据idx缩减维度
expand: broadcasting, expand(shape) 只能1->N, 例子: b.expand(4, 32, 14, 14) (b.shape = 1, 32, 14, 14) 如果写-1, 则表示不变
repeat: memory copied (占用空间) repeat(…拷贝的次数…)