import torch
import numpy as np
a = torch.randn(2,3)
print("shape:",a.shape,"\nsize:",a.size())
shape: torch.Size([2, 3])
size: torch.Size([2, 3])
print("第二个维度的信息:",a.size(1))
第二个维度的信息: 3
print("dim:",a.dim())
dim: 2
k = torch.rand(1,2,3)
print("k:",k)
k: tensor([[[0.0964, 0.8898, 0.1816],
[0.7350, 0.5357, 0.1356]]])
print("k[0]:",k[0])
k[0]: tensor([[0.0964, 0.8898, 0.1816],
[0.7350, 0.5357, 0.1356]])
print(list(k.shape))
[1, 2, 3]
print(torch.tensor(1.))
tensor(1.)
print(torch.tensor(1.3))
tensor(1.3000)
print(isinstance(a,torch.FloatTensor))
True
print(isinstance(a,torch.cuda.FloatTensor))
False
a = a.cuda()
print(isinstance(a,torch.cuda.FloatTensor))
True
b = torch.randn(2,3)
print(b)
tensor([[ 0.4233, 0.7711, -0.6087],
[ 1.4722, 1.3866, -0.4094]])
print(len(b.shape))
2
print(b.shape)
torch.Size([2, 3])
print(b.size())
torch.Size([2, 3])
print("dim:",b.dim())
dim: 2
print(len(torch.tensor(1.).shape))
0
print(torch.tensor(1.).size())
torch.Size([])
c = torch.FloatTensor(3)
print(c)
tensor([1.4013e-45, 0.0000e+00, 2.8399e-29])
print(c.shape)
torch.Size([3])
print(c.dim())
1
print(len(c.shape))
1
print(torch.tensor([1.2,2,2]))
tensor([1.2000, 2.0000, 2.0000])
print(torch.tensor([1.2,2,2]).shape)
torch.Size([3])
print(len(torch.tensor([1.2,2,2])))
3
print("torch.version:",torch.__version__)
torch.version: 1.3.1
data = np.ones(2)
print(data)
[1. 1.]
d = torch.rand(2,3,28,28)
print("size:",d.shape)
size: torch.Size([2, 3, 28, 28])
print("dim:",d.dim())
dim: 4
print("numel:",d.numel())
numel: 4704
print("numel = 2*3*28*28 = ",d.size(0)*d.size(1)*d.size(2)*d.size(3))
numel = 2*3*28*28 = 4704
print("len:",len(d.shape))
len: 4