创建 tensor
- torch.tensor
- torch.ones_like
- torch.zeros_like
- torch.rand_like
- torch.allclose 对比两个 tensor 是否足够接近
- torch.rand
Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)[0,1)
- torch.Tensor和torch.tensor有什么区别?,前者是转化成torch.FloatTensor,后者是根据传入的数据转化成不同类型的dtype
- torch.randn
Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
- torch.normal(mean,std,size)
- torch.zeros
- torch.arange
torch.range() is deprecated and will be removed in a future release because its behavior is inconsistent with Python’s range builtin. Instead, use torch.arange(), which produces values in [start, end).
- torch.eye
- torch.full([n,n],x)
- 移动 tensor
if torch.cuda.is_available():
tensor=tensor.to('cuda')
tensor 运算
- pytorch中的矩阵乘法:函数mul,mm,mv以及 @运算 和 *运算
- torch.is_tensor
- torch.numel,返回元素数目
- torch.bmm,批量张量相乘
tensor 操作
- torch.cat
除了dim指定的那个维度,其他维度的shape都应该一样
- torch.reshape(a,shape)
shape可以用 [] 或 () 指定
- torch.split(a,size)
按size大小均分或者可以传入list
- torch.squeeze
- troch.tile(a,dims)
扩增a的行列
- torch.transpose(a,dim0,dim1)
- torch.unbind
- torch.unsqueeze(a,dim)
- torch.nn.functional.pad
torch.nn.functional.pad使用
对 (batch_size,in_channel,h,w),pad的顺序是(h1,h2,w1,w2,in_channel1,in_channel2,batch_size1,batch_size2),因为每一个维度都有两个方向