一切数值皆torch.Tensor类型 import torch from torch import nn from torch.nn import functional as F isinstance(torch.tensor([1,1]),torch.Tensor) # True isinstance(torch.tensor([1,1])[0],torch.Tensor) # True 对比一下numpy import numpy as np isinstance(np.array([1,1]),np.ndarray) # True isinstance(np.array([1,1])[0],np.ndarray) # False |
|
|
|
|
![]() |
|
|
|
|
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach()
torch.as_tensor(data=x).float() 替换 torch.tensor(data=x).float()
必须为浮点数,不能为整数 import torch a = torch.tensor([1, 2, 3], requires_grad=True) RuntimeError: Only Tensors of floating point and complex dtype can require gradients |
不能内部修改 import torch a = torch.tensor([1., 2., 3.], requires_grad=True) a[2]=1.0 RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation. |
|
|
|
import torch a=torch.rand(32,32,3) #每一个维度上做均值,相当于去除这个维度/做合并 a.mean(axis=-1).shape torch.Size([32, 32])