欢迎来到黄黄自学室
pytorch基础数据类型标量dim=1dim=2dim=3dim=4
亲爱的朋友们,任何时候都要抬头挺胸收下巴,慢慢追赶,心向阳光,无暇痴想!
pytorch基础数据类型
标量
a
= torch
.tensor
(2.2)
print(a
.shape
)
print(a
.size
())
print(len(a
.shape
))
print(a
.dim
())
输出:
torch
.Size
([])
torch
.Size
([])
0
0
dim=1
print(torch
.tensor
(1.1))
print(torch
.tensor
([1.1, 2.2]))
print(torch
.FloatTensor
(1))
print(torch
.FloatTensor
(2))
data
= np
.ones
(2)
print(data
)
print(torch
.from_numpy
(data
))
输出:
tensor
(1.1000)
tensor
([1.1000, 2.2000])
tensor
([1.1000])
tensor
([1.1000, 2.2000])
[1. 1.]
tensor
([1., 1.], dtype
=torch
.float64
)
dim=2
a
=torch
.randn
(2,3)
print(a
)
print(a
.shape
)
print(a
.size
(0))
print(a
.size
(1))
print(a
.shape
[0])
输出:
tensor
([[-1.2463, -1.7079, 0.3362],
[ 0.0221, -0.3282, 2.6912]])
torch
.Size
([2, 3])
2
3
2
dim=3
a
= torch
.rand
(1,2,3)
print(a
)
print(a
.shape
)
print(a
[0])
print(list(a
.shape
))
输出:
tensor
([[[0.6837, 0.0755, 0.8848],
[0.4772, 0.6598, 0.0356]]])
torch
.Size
([1, 2, 3])
tensor
([[0.6837, 0.0755, 0.8848],
[0.4772, 0.6598, 0.0356]])
[1, 2, 3]
dim=4
a
= torch
.rand
(2, 1, 28, 28)
print(a
)
print(a
.shape
)
print(a
.numel
())
print(len(a
.shape
))
print(a
.dim
())
tensor
([[[[0.5542, 0.6195, 0.8397, ..., 0.0999, 0.5630, 0.8705],
[0.0801, 0.2280, 0.8619, ..., 0.9935, 0.2579, 0.9406],
[0.7946, 0.9227, 0.1112, ..., 0.5450, 0.5005, 0.8216],
...,
[0.5081, 0.2886, 0.3900, ..., 0.1594, 0.4824, 0.0905],
[0.2204, 0.4182, 0.2404, ..., 0.8443, 0.2388, 0.5995],
[0.9903, 0.0996, 0.7635, ..., 0.6127, 0.3190, 0.3793]]],
[[[0.4489, 0.1073, 0.7817, ..., 0.4795, 0.0331, 0.9607],
[0.9574, 0.9217, 0.4065, ..., 0.0366, 0.8986, 0.8122],
[0.0629, 0.8873, 0.2454, ..., 0.3124, 0.2565, 0.9617],
...,
[0.6944, 0.6282, 0.2614, ..., 0.8821, 0.0859, 0.7367],
[0.3204, 0.7058, 0.9221, ..., 0.5302, 0.8725, 0.5979],
[0.8618, 0.9294, 0.0506, ..., 0.6029, 0.8625, 0.3238]]]])
torch
.Size
([2, 1, 28, 28])
1568
4
4
转载请注明原文地址:https://ipadbbs.8miu.com/read-33009.html