pytorch 创建张量tensor
先看下面一张图
通过上图有了一个直观了解后,我们开始尝试创建一下。
先创建一个标量和一个向量
a = torch.tensor([1]) #标量
print(a)
print(a.size(),a.size(0),a.shape)
b = torch.tensor([1,2]) #向量
print(b)
print(b.size(),b.size(0),a.shape)
打印出来如下图
下面再来创建两个矩阵
a = torch.tensor([[1],[2]])
print(a)
print(a.size(),a.size(0),a.size(1),a.shape)
b = torch.tensor([[1,1],[2,2]])
print(b)
print(b.size(),b.size(0),b.size(1),b.shape)
很直观的解释就是a是一个2行1列的矩阵,b是一个2行2列的矩阵,打印结果如下
标量,向量和矩阵很好理解,下面进一步创建一个多维张量。
这里解释一下,
在同构的意义下,我们设r为张量的秩或阶,那么,第零阶张量(r = 0)为标量,第一阶张量(r = 1)为向量,第二阶张量(r = 2)为矩阵,第三阶以上(r > 2)的统称为多维张量。
当r=3时,表示张量有3个维度,想象一个长方体,有长宽高三个维度。如下图
在创建之前,先引入一个符号c,也就是通道。可以简单理解为c用来表示有几个矩阵,比如c为2时,可以理解为有2个矩阵,用长方体来解释就是c=2。创建如下张量
a = torch.tensor([[[1,1],[2,2]],[[1,1],[2,2]]])
print(a)
print(a.size(),a.size(0),a.size(1),a.shape)
可以想象一下创建了如下长方体
打印如下
再来创建一个通道数为3,矩阵行数为2,列数为2的三阶张量
b = torch.tensor([[[1,1],[2,2]],[[1,1],[2,2]],[[1,1],[2,2]]])
print(b)
print(b.size(),b.size(0),b.size(1),b.size(2),b.shape)
打印如下
现在继续创建更高维的张量。
之前创建三维张量时,我们直观理解的是用c个矩阵叠加构成一个长方体。现在我们创建多维张量时,就直观理解为构建多个长方体,如下图。
下面创建一个多维张量,直观理解为2个长方体,每个长方体的c=2,h=2,w=3
a = torch.tensor([[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]],[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]]])
print(a)
print(a.size(),a.size(0),a.size(1),a.shape)
打印如下
下面用生成函数生成一个多维张量,比如两个长方体,每个长方体c=3,h=4,w=5
a=torch.zeros(2,3,4,5)
print(a)
print(a.size(),a.size(0),a.size(1),a.size(2),a.size(3),a.shape)
打印如下
如果想创建一个更高维的张量,按照上面的思路,我们把一个4维张量看成一个整体,比如生成一个5维张量,直观理解生成为多个4维张量。假设生成一个5维张量,直观理解为有2个4维张量,每个4维张量里包括2个长方体,没个长方体c=2,h=2, w=3
a = torch.tensor([[[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]],[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]]],[[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]],[[[1,1,1],[2,2,2]],[[1,1,1],[2,2,2]]]]])
print(a)
print(a.size(),a.size(0),a.size(1),a.size(2),a.size(3),a.size(4),a.shape)
打印如下
现在用生成函数生成三个多维张量
a=torch.zeros(2,3,4)
print(a.size(),a.size(0),a.size(1),a.size(2),a.shape)
b= torch.rand(2,3,4,5)
print(b)
print(b.size(),b.size(0),b.size(1),b.size(2),b.size(3),b.shape)
c= torch.normal(0, 1, (2,3,4,5,6))
print(c)
print(c.size(),c.size(0),c.size(1),c.size(2),c.size(3),c.shape)
打印如下
输出a:
torch.Size([2, 3, 4]) 2 3 4 torch.Size([2, 3, 4])
输出b:
tensor([[[[0.5323, 0.4103, 0.8526, 0.5870, 0.7578],
[0.2337, 0.6932, 0.3904, 0.8094, 0.2979],
[0.2760, 0.0440, 0.0421, 0.4341, 0.5498],
[0.8133, 0.8797, 0.3483, 0.7298, 0.5871]],
[[0.6664, 0.8471, 0.4671, 0.2570, 0.4872],
[0.3157, 0.9262, 0.3674, 0.1793, 0.6425],
[0.7998, 0.3407, 0.9260, 0.3809, 0.2343],
[0.5310, 0.6680, 0.2157, 0.2082, 0.8272]],
[[0.1142, 0.3816, 0.8988, 0.5251, 0.6632],
[0.5523, 0.4415, 0.2441, 0.8600, 0.0320],
[0.8309, 0.0220, 0.5321, 0.4891, 0.1545],
[0.2807, 0.1089, 0.4760, 0.0214, 0.7516]]],
[[[0.4279, 0.5537, 0.3929, 0.5693, 0.0763],
[0.9715, 0.9282, 0.0291, 0.5497, 0.1410],
[0.8224, 0.7231, 0.1167, 0.3934, 0.5008],
[0.9964, 0.1797, 0.7816, 0.3529, 0.8618]],
[[0.1492, 0.6342, 0.4635, 0.7068, 0.7053],
[0.3321, 0.5471, 0.1980, 0.7277, 0.9319],
[0.5332, 0.9777, 0.9855, 0.1361, 0.2420],
[0.4200, 0.8882, 0.1350, 0.9316, 0.7393]],
[[0.6464, 0.6867, 0.5322, 0.5308, 0.3515],
[0.7434, 0.5409, 0.3824, 0.6021, 0.3077],
[0.0220, 0.1010, 0.4882, 0.8322, 0.2234],
[0.4806, 0.7065, 0.9659, 0.4051, 0.3015]]]])
torch.Size([2, 3, 4, 5]) 2 3 4 5 torch.Size([2, 3, 4, 5])
输出c:
tensor([[[[[ 8.4570e-01, 8.8545e-01, 6.4706e-01, 2.3371e-01, 6.3365e-01,
-1.2818e-01],
[ 2.8157e-01, -5.9976e-01, -6.1350e-02, -2.9606e-01, -5.6313e-01,
-1.7975e+00],
[ 1.2712e+00, 4.1075e-01, 4.3793e-01, -6.2723e-01, -2.1180e-01,
-1.0748e+00],
[-1.0389e-01, -2.6530e-01, -1.9572e+00, 4.6628e-01, 2.2925e-01,
6.0041e-01],
[ 1.9245e+00, 2.9442e-01, 5.4111e-02, 2.1890e-02, 1.2795e+00,
3.9643e-01]],
[[ 1.5745e-01, 6.3411e-01, 7.9718e-01, 4.3876e-01, 1.0079e+00,
1.1973e+00],
[ 9.3584e-02, -2.7799e-01, -9.0217e-01, -1.9702e+00, 2.0778e+00,
-1.0505e+00],
[-4.9708e-01, -9.9813e-01, 1.7837e-01, -1.5216e+00, -1.8763e-01,
7.2541e-01],
[-1.0032e+00, 2.0914e+00, -2.1229e-01, -6.6474e-01, -2.3550e-01,
7.1369e-01],
[-1.0660e-01, -4.9528e-02, -1.3572e+00, -1.0231e+00, 9.9876e-01,
7.0753e-01]],
[[-2.2400e-01, -9.8229e-01, 6.8908e-01, -1.4827e+00, -7.3015e-01,
3.5123e-01],
[ 4.6155e-01, -1.0476e+00, -1.1763e+00, -1.2353e+00, -9.5628e-02,
-1.4597e+00],
[ 3.6695e-01, -2.1101e+00, 1.9671e+00, -2.3592e-01, 8.1444e-01,
-1.1084e+00],
[ 9.1046e-01, -7.7845e-01, -9.8645e-01, 1.2489e+00, -5.2317e-01,
3.2823e-01],
[ 6.3078e-01, 1.1090e-01, -5.5749e-01, 3.9375e-01, -1.0509e+00,
-1.0708e+00]],
[[ 2.6164e+00, 3.7516e-01, 1.6906e-01, -1.5052e+00, -2.4603e-01,
-2.8490e-01],
[ 1.1448e+00, 4.4030e-01, -1.1084e+00, -2.0204e-01, 5.0056e-01,
-7.9787e-01],
[ 3.2020e-01, -2.4338e-01, 1.6701e-01, 4.1868e-01, -7.5737e-01,
9.7790e-01],
[ 6.7062e-01, 1.8829e-01, -5.1939e-01, -4.8424e-01, -5.8767e-03,
2.5235e+00],
[-6.2654e-01, -1.9254e-01, -1.5091e+00, -6.3911e-02, -1.9896e+00,
-7.0351e-01]]],
[[[-1.4144e-01, -7.5987e-01, 1.6558e-01, -1.6157e+00, 1.3157e+00,
5.1730e-02],
[-8.2810e-01, -9.7590e-01, -6.0546e-01, -1.8625e+00, 4.7709e-02,
-2.2081e-01],
[-1.6527e+00, -1.3708e+00, -8.0298e-01, -2.5250e-01, 1.1301e+00,
2.2961e-01],
[-1.6014e+00, 5.1685e-01, 1.4383e+00, -2.1821e+00, -4.2359e-01,
-2.2537e-03],
[ 2.1337e-01, 4.4000e-01, 1.1744e+00, -4.9803e-01, -5.3193e-02,
1.7091e+00]],
[[-4.2063e-02, 2.8442e-01, 5.4094e-01, 2.1361e+00, 1.7057e-01,
2.2426e+00],
[ 2.5142e-01, -1.4382e+00, -3.2952e-01, -1.5949e+00, -4.0295e-01,
-6.0637e-02],
[ 2.0762e+00, 1.9389e+00, -1.9458e+00, 1.9113e+00, 2.2634e+00,
-4.5660e-01],
[ 4.6860e-01, -2.7597e-01, -1.5863e+00, -3.8444e-01, 3.2137e-01,
3.3837e-01],
[-1.5908e+00, 3.5689e-01, 7.7673e-01, 3.3594e-01, 3.2439e-01,
-8.5800e-01]],
[[-2.4817e-02, -1.2275e+00, -1.4821e+00, 7.7409e-01, -1.8095e+00,
-9.0291e-01],
[ 1.4445e+00, 1.3184e-01, -5.2523e-01, 1.9557e-01, -6.6541e-01,
-1.3286e-01],
[-9.7191e-02, -1.0623e-02, 1.6907e-01, -1.3444e-01, -9.9286e-01,
-9.0524e-01],
[ 8.9544e-01, 1.3233e+00, -2.4735e+00, 6.8221e-01, -1.2045e+00,
-1.4853e-01],
[-2.2915e+00, -1.1359e+00, 4.0743e-01, 8.5094e-01, -1.6482e+00,
3.3259e-01]],
[[ 1.9010e+00, 2.1039e+00, -8.2259e-01, 2.3197e+00, 1.6197e-01,
-5.6633e-01],
[-2.0824e+00, -1.8159e+00, -2.6840e+00, 6.8026e-01, 8.5818e-01,
5.5827e-01],
[-1.9431e-01, 3.2737e-01, 7.3513e-02, 1.4346e+00, -1.5601e+00,
1.0158e+00],
[ 9.2745e-02, 1.6950e+00, -1.4271e+00, 1.0351e+00, 3.3502e-01,
9.6264e-01],
[-5.2824e-01, 7.2988e-01, -7.5764e-01, -1.4038e+00, 6.7941e-01,
-8.6801e-01]]],
[[[-1.1680e+00, -1.0779e+00, 1.0772e+00, -5.7942e-01, -3.7877e-01,
-8.9438e-02],
[-1.3215e-01, -3.6088e-01, -2.6686e-01, -5.1740e-01, 7.5179e-01,
4.6317e-01],
[-2.1368e-01, 2.4409e+00, -5.8664e-01, 4.9544e-01, -6.5960e-01,
2.9775e-01],
[ 1.4571e-01, -4.8371e-01, 2.9352e-01, -1.4258e+00, 1.3468e+00,
-4.9984e-01],
[-2.5161e-01, 5.6569e-01, -8.0022e-02, 5.9544e-01, -8.3494e-02,
-5.9176e-01]],
[[-5.5313e-01, -1.0735e+00, -6.3995e-01, 3.9293e-01, -1.0596e+00,
9.5002e-01],
[ 2.8909e-01, 1.9764e+00, 7.8974e-01, -5.3437e-01, 6.1599e-02,
7.6570e-02],
[-3.7883e-01, -4.0950e-01, -1.3688e-01, -1.8612e+00, -3.2650e-02,
-1.5974e+00],
[-6.5722e-01, 4.4803e-01, 1.4384e+00, -7.3564e-01, -1.4922e+00,
9.7724e-01],
[-1.6080e-01, 8.1424e-01, -1.8807e-01, -6.4085e-01, 1.5716e-01,
-1.4818e-03]],
[[ 1.4165e-01, 1.9828e-01, -1.6206e+00, 9.5897e-01, -4.6161e-01,
-8.2331e-01],
[ 4.2246e-01, 5.2396e-01, -2.0933e+00, -9.8151e-01, -1.8276e+00,
1.1227e+00],
[-2.0200e+00, -8.2249e-01, 1.3074e+00, 1.2358e-01, -1.1941e+00,
1.0237e+00],
[-1.3299e-01, 2.0454e+00, -1.4204e-01, -1.1167e-01, -1.3196e+00,
-4.0144e-01],
[ 1.6024e+00, 9.4263e-01, 1.5464e-01, -1.2322e+00, -1.0539e+00,
-1.2796e+00]],
[[ 8.3959e-01, 2.9687e-02, 2.8890e-01, 1.6029e-01, 3.0695e-01,
-9.3486e-01],
[-1.7404e+00, -3.6490e-02, -1.2800e+00, 8.3157e-01, -6.4245e-01,
-1.5773e-01],
[-6.0091e-01, 7.6323e-01, -1.8301e+00, -5.3179e-02, -6.6559e-01,
-8.2690e-01],
[ 6.4527e-01, 4.5691e-01, -3.3110e-01, 1.1825e+00, -8.9335e-01,
2.6908e-01],
[ 2.6085e-01, -7.7397e-01, -1.4054e-01, 8.8399e-01, -6.1615e-01,
1.9790e-01]]]],
[[[[-4.7784e-01, -1.4543e+00, -1.0164e+00, 1.7553e-01, -1.2813e+00,
5.2880e-01],
[-6.3678e-01, 8.9314e-01, -1.5196e+00, -7.2509e-01, 1.3289e+00,
-1.6003e+00],
[-3.7771e-01, 1.0371e+00, 8.3983e-01, 1.8590e-01, 8.8691e-01,
-3.9374e-01],
[-6.8203e-01, -6.2969e-03, -9.3931e-01, 2.0965e-01, -5.6640e-01,
1.1072e-01],
[ 7.4534e-01, 4.8388e-01, -1.1912e+00, -6.1915e-01, -7.4436e-01,
5.6890e-01]],
[[-3.5332e-01, 8.6817e-01, -3.6723e-01, -1.0254e+00, -1.3804e+00,
-1.5567e+00],
[-7.1884e-01, -1.0605e-01, -1.2250e-01, 1.6420e+00, 1.0611e-01,
2.2328e-01],
[ 6.9605e-01, 1.0212e+00, 1.1955e+00, 1.3524e-01, 4.0578e-01,
5.5533e-01],
[ 1.4146e+00, -9.7452e-01, -2.3702e+00, -3.9358e-01, -2.8402e-01,
3.5829e-01],
[-2.4883e-01, -2.5408e-01, 1.1583e+00, -4.8976e-01, -8.9417e-01,
-3.1495e-01]],
[[ 6.8242e-01, -1.8079e+00, 1.5373e+00, -2.8739e+00, -5.2587e-01,
1.8828e+00],
[-8.4203e-01, 1.6073e+00, 3.0128e-02, -1.2699e+00, 9.1670e-02,
-7.0801e-01],
[-6.4134e-01, -2.1483e-01, 4.6702e-02, 2.1242e+00, -5.4502e-02,
-8.3308e-01],
[-3.6094e-01, 3.5267e-01, -2.0881e+00, -1.1466e-01, 1.9785e-01,
3.3630e-01],
[-9.4763e-02, 4.2234e-01, 9.0930e-01, -3.8061e-01, -8.7369e-01,
-1.1014e-01]],
[[ 4.0586e-01, -1.2549e+00, 9.9356e-01, 7.9593e-01, -6.9189e-02,
-6.8338e-01],
[-4.1695e-01, -7.9827e-01, -1.2609e-01, -2.5128e-01, -1.8518e+00,
-9.0563e-01],
[-8.1115e-01, -8.0023e-01, 9.5610e-01, 5.8341e-01, -2.8728e-01,
-4.7274e-01],
[-2.0525e-01, 2.8722e-01, -1.5245e+00, -3.2057e-02, -9.5722e-01,
2.9027e-01],
[ 1.3324e+00, -5.2395e-01, -3.1407e-01, -1.0267e+00, -2.1564e-01,
1.0621e-01]]],
[[[-1.6414e+00, 5.6669e-01, 1.0247e+00, -6.2411e-01, -9.1539e-01,
1.3125e+00],
[ 1.5814e-01, -1.1946e+00, -1.8930e+00, -1.2071e+00, -1.8788e+00,
-1.1891e+00],
[ 5.9682e-01, 4.2213e-01, -3.7164e-01, -5.3018e-01, -1.3403e-02,
8.1985e-01],
[ 9.4628e-02, 9.4829e-01, 4.9370e-01, -6.3984e-01, 3.9836e-01,
-8.9849e-01],
[-4.7721e-01, -8.3642e-01, 1.6831e-01, -6.3005e-01, 5.3191e-01,
-1.0278e+00]],
[[ 9.9119e-01, -3.6613e-01, -5.6046e-01, -6.4593e-01, -6.9760e-01,
-1.1387e+00],
[-1.0797e+00, 4.2900e-01, -6.0828e-01, -1.1628e+00, 9.4538e-01,
-4.9542e-01],
[-7.9521e-01, -1.8895e-01, 3.0855e-01, -1.4398e+00, 9.1582e-01,
1.0060e+00],
[-7.6462e-02, 7.9941e-01, 4.0099e-01, 4.7461e-01, -4.9371e-01,
2.3619e-01],
[-1.3185e-01, -2.1660e-01, -5.4233e-01, -2.9485e-01, -1.0026e+00,
-1.0412e+00]],
[[-1.3497e+00, 2.8947e-01, -2.0374e-01, 1.2474e+00, 1.5418e+00,
-4.3479e-01],
[-2.1779e+00, 1.6220e+00, 7.1444e-01, 2.6374e-01, -1.0998e+00,
-1.8698e+00],
[-5.5768e-01, -1.0170e-01, -1.1235e-01, 8.0741e-01, -9.9641e-01,
8.2046e-01],
[-4.4686e-01, 7.0509e-01, -2.1951e+00, -2.8891e-01, -4.3040e-01,
1.0912e-01],
[-5.6099e-01, -5.8205e-01, 2.0154e-01, -2.3465e+00, 1.2146e+00,
2.4922e-01]],
[[ 8.3546e-01, 1.0258e+00, 7.2581e-01, 1.1249e+00, -1.5064e+00,
6.7624e-01],
[ 3.3112e-01, -1.1315e+00, -8.0334e-01, -1.2405e+00, 9.8623e-01,
1.0246e+00],
[ 1.4091e-01, 1.0004e+00, -4.8125e-01, -6.0611e-01, 8.7497e-01,
3.9099e-01],
[ 6.2967e-02, 9.8573e-01, 5.7534e-01, 1.0903e+00, -1.0972e+00,
-4.7787e-01],
[ 5.6745e-01, -3.4242e-01, 5.5169e-01, 2.2326e+00, -1.0485e+00,
1.8625e+00]]],
[[[ 1.1514e+00, -4.3170e-01, 1.2154e+00, -6.1182e-01, -1.6654e-01,
-5.8629e-02],
[ 9.5037e-01, 1.1467e-01, 1.4225e+00, 1.4325e+00, -4.8513e-01,
-2.9276e-01],
[-5.6832e-01, -1.9349e-01, -3.2615e-01, -6.8582e-01, -9.6867e-01,
-1.2156e+00],
[ 1.4024e+00, 8.7353e-02, -7.7799e-01, 1.0185e+00, 2.2627e-02,
-1.0047e+00],
[ 5.7386e-01, 1.2875e+00, -5.9975e-01, -1.7639e+00, 1.3135e+00,
1.6564e+00]],
[[ 3.9898e-01, 1.7042e+00, -6.0132e-02, -5.2001e-01, 1.0704e+00,
1.6258e-01],
[-9.5167e-01, 4.2955e-01, -1.8101e+00, -5.0860e-02, -4.8489e-01,
-8.1433e-01],
[-1.5941e+00, -1.3387e-01, -1.5383e-01, 4.3477e-01, 2.3845e-01,
-2.5789e-01],
[-1.4393e+00, -3.5759e-01, 1.7216e-01, -1.2913e+00, 4.5153e-01,
-1.0457e+00],
[-8.8329e-01, 1.5661e+00, 5.4105e-01, -1.1196e-01, -1.4932e+00,
5.4801e-01]],
[[-2.0640e+00, 1.4359e+00, -2.3919e-01, -5.0450e-01, 9.7866e-01,
-1.5596e+00],
[-8.5198e-01, -2.4388e+00, 1.3987e+00, -3.4502e-01, -7.4318e-03,
2.1564e+00],
[-4.2215e-01, -1.8626e+00, -7.3731e-01, -5.2587e-01, 6.0198e-01,
8.2447e-01],
[-1.2318e-01, -4.5281e-01, 3.6226e-01, 1.1130e+00, -8.6672e-01,
3.2549e-01],
[-1.0706e+00, 9.4219e-01, -1.0616e+00, -6.3033e-01, -2.5272e+00,
-1.3189e+00]],
[[-2.5891e-01, 2.0627e-01, 6.5381e-01, 8.5259e-01, -1.0944e+00,
-3.1494e-01],
[ 3.9371e-01, -6.4014e-01, -2.5121e-01, -6.8216e-01, 1.3034e+00,
4.9772e-01],
[-9.1848e-01, 3.0806e-01, 7.0758e-02, 1.2814e+00, -1.5876e-01,
-9.3685e-01],
[ 1.5276e+00, 9.8460e-01, 7.3852e-01, 5.5396e-02, 1.6140e+00,
5.5823e-01],
[-5.8798e-01, 4.2220e-01, 1.5134e-01, 4.5249e-01, -1.0482e-01,
-1.8100e+00]]]]])
torch.Size([2, 3, 4, 5, 6]) 2 3 4 5 torch.Size([2, 3, 4, 5, 6])