很多命令用的也不是很多,但是要用想不起来就很花时间,下面整理一些:
实现的功能是,1 tensor翻转 2.判断是否为torch类型
1.tensor翻转
tensor没有办法用[::-1]这样的办法
x=torch.tensor([i for i in range(10)])
#首先要把shape是(n,)的tensor换成(n,1)的
y=x.reshape(x.shape[0],1)
#然后使用torch.flip翻转
z=torch.flip(y,[0,1])
#需要再变成1维tensor的话
l=z.reshape(-1)
2.判断数据类型
使用type
a=torch.tensor([1])
type(a)
3.判断是否为torch类型
使用isinstance函数,isinstance(x,type)
a=torch.tensor([1])
isinstance(a,torch.Tensor)
这个函数还可以判断numpy,int,list等其他数据类型,记录如下
type种类 | 例子 |
str | ‘test’ |
list | [1,2,3] |
set | {'Adam', 'Kathy', 'Lisa'} |
tuple | ('Adam', 'Kathy', 'Lisa') |
dict | {'name':'Kathy'} |
int | 1 |
float | 1.2 |
numpy.ndarray | array([[0.27754372, 0.01823316]]) |
torch.Tensor | tensor(3) |