张量的 pytorch 张量到张量

pytorch tensor of tensors to a tensor

当我打印火炬张量时,我得到以下输出。如果没有 [] 的内部元素,我怎么能得到那个张量呢? 我打印了第一个元素的类型,它 returns <class 'torch.Tensor'> 所以这个张量似乎是张量的张量......我怎么能把它转换成数字的张量?

tensor([[-5.6117e-01],
        [ 3.5726e-01],
        [-2.5853e-01],
        [-4.8641e-01],
        [-1.0581e-01],
        [-1.8322e-01],
        [-1.2732e+00],
        [-5.9760e-02],
        [ 1.2819e-01],
        [ 6.3894e-02],
        [-9.1817e-01],
        [-1.6539e-01],
        [-1.1471e+00],
        [ 1.9666e-01],
        [-6.3297e-01],
        [-4.0876e-01],
        [-2.4590e-02],
        [ 2.7065e-01],
        [ 3.5308e-01],
        [-4.6348e-01],
        [-4.1755e-01],
        [-1.1554e-01],
        [-4.2062e-01],
        [ 1.4067e-01],
        [-2.9788e-01],
        [-7.4582e-02],
        [-5.3751e-01],
        [ 1.1344e-01],
        [-2.6100e-01],
        [ 2.6951e-02],
        [-5.0437e-02],
        [-1.9163e-01],
        [-3.3893e-02],
        [-5.9640e-01],
        [-1.1574e-01],
        [ 1.4613e-01],
        [ 1.2263e-01],
        [-1.5566e-01],
        [ 1.4740e-01],
        [-9.9924e-01],
        [ 2.0878e-01],
        [-2.0074e-01],
        [ 7.8383e-02],
        [ 7.4679e-02],
        [-5.8065e-01],
        [ 6.7777e-01],
        [ 5.9879e-01],
        [ 6.6301e-01],
        [-4.7051e-01],
        [-2.5468e-01],
        [-2.7382e-01],
        [ 1.7585e-01],
        [ 3.6151e-01],
        [-9.2532e-01],
        [-1.6999e-01],
        [ 8.4971e-02],
        [-6.6083e-01],
        [-3.1204e-02],
        [ 6.3712e-01],
        [-5.8580e-02],
        [-7.7901e-04],
        [-4.6792e-01],
        [ 1.0796e-01],
        [ 7.8766e-01],
        [ 1.6809e-01],
        [-7.0058e-01],
        [-2.9299e-01],
        [-8.2735e-02],
        [ 2.0875e-01],
        [-2.9426e-01],
        [-7.6748e-02],
        [-1.5762e-01],
        [-5.7432e-01],
        [-5.2042e-01],
        [-1.5152e-01],
        [ 1.4119e+00],
        [-1.5752e-01],
        [-3.0565e-01],
        [-5.1378e-01],
        [-5.8924e-01],
        [-1.0163e+00],
        [-2.2021e-01],
        [ 2.9112e-02],
        [ 1.8521e-01],
        [ 6.2814e-01],
        [-6.8793e-01],
        [ 2.1395e-02],
        [ 5.7168e-01],
        [ 9.0977e-01],
        [ 3.8899e-01],
        [ 3.0209e-01],
        [ 2.4655e-01],
        [-1.1688e-01],
        [-5.9835e-02],
        [ 3.6426e-02],
        [-5.2782e-01],
        [ 1.4604e+00],
        [ 2.9685e-01],
        [-2.4077e-01],
        [ 1.0163e+00],
        [ 6.9770e-01],
        [-2.6183e-01],
        [ 3.6770e-01],
        [ 3.6535e-03],
        [ 4.2364e-01],
        [-5.4703e-01],
        [ 8.9173e-02],
        [-3.9032e-01],
        [-5.9740e-01],
        [ 3.7479e-02],
        [ 3.0257e-01],
        [ 8.2539e-02],
        [-6.0559e-01],
        [-4.3660e-01],
        [-7.0624e-01],
        [-5.0503e-01],
        [-4.0929e-01],
        [-2.3300e-01],
        [ 2.0298e-01],
        [-6.3697e-01],
        [-1.2584e-01],
        [ 5.6092e-02],
        [ 5.0150e-02],
        [-1.5358e-01],
        [ 2.9248e-02],
        [ 1.1180e-01],
        [-1.5535e-01],
        [ 1.1964e-01],
        [-6.5698e-01],
        [ 4.1923e-01],
        [ 7.4044e-02],
        [ 2.4536e-02],
        [ 3.2647e-01],
        [-7.7464e-01],
        [ 3.9898e-01],
        [-2.5777e-01],
        [ 8.5569e-02],
        [-4.0305e-01],
        [ 5.4463e-01],
        [-3.4124e-01],
        [-4.0789e-01],
        [ 4.2093e-01],
        [-3.8487e-01],
        [-4.0491e-01],
        [-2.1539e-01],
        [-1.7979e-02],
        [ 3.2492e-01],
        [-2.0894e-01],
        [ 2.5629e-01],
        [ 9.6046e-01]], device='cuda:0', grad_fn=<AddmmBackward0>)

该张量具有单一维度(即它的形状 [Nx1])。只需压缩该维度或选择第 0 个元素:

In [1]: import torch

In [2]: a = torch.zeros([10,1])

In [3]: a
Out[3]: 
tensor([[0.],
        [0.],
        [0.],
        [0.],
        [0.],
        [0.],
        [0.],
        [0.],
        [0.],
        [0.]])

In [4]: a[:,0]
Out[4]: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [5]: a.squeeze(1)
Out[5]: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

如果我理解你的问题,你可以使用flatten方法

输入:


tvalue=torch.tensor([[-5.6117e-01],
        [ 3.5726e-01],
        [-2.5853e-01],
        [-4.8641e-01],
        [-1.0581e-01],
        [-1.8322e-01],
        [-1.2732e+00],
        [-5.9760e-02],
        [ 1.2819e-01],
        [ 6.3894e-02],
        [-9.1817e-01],
        [-1.6539e-01],
        [-1.1471e+00],
        [ 1.9666e-01],
        [-6.3297e-01],
        [-4.0876e-01],
        [-2.4590e-02],
        [ 2.7065e-01],
        [ 3.5308e-01],
        [-4.6348e-01],
        [-4.1755e-01],
        [-1.1554e-01],
        [-4.2062e-01],
        [ 1.4067e-01],
        [-2.9788e-01],
        [-7.4582e-02],
        [-5.3751e-01],
        [ 1.1344e-01],
        [-2.6100e-01],
        [ 2.6951e-02],
        [-5.0437e-02],
        [-1.9163e-01],
        [-3.3893e-02],
        [-5.9640e-01],
        [-1.1574e-01],
        [ 1.4613e-01],
        [ 1.2263e-01],
        [-1.5566e-01],
        [ 1.4740e-01],
        [-9.9924e-01],
        [ 2.0878e-01],
        [-2.0074e-01],
        [ 7.8383e-02],
        [ 7.4679e-02],
        [-5.8065e-01],
        [ 6.7777e-01],
        [ 5.9879e-01],
        [ 6.6301e-01],
        [-4.7051e-01],
        [-2.5468e-01],
        [-2.7382e-01],
        [ 1.7585e-01],
        [ 3.6151e-01],
        [-9.2532e-01],
        [-1.6999e-01],
        [ 8.4971e-02],
        [-6.6083e-01],
        [-3.1204e-02],
        [ 6.3712e-01],
        [-5.8580e-02],
        [-7.7901e-04],
        [-4.6792e-01],
        [ 1.0796e-01],
        [ 7.8766e-01],
        [ 1.6809e-01],
        [-7.0058e-01],
        [-2.9299e-01],
        [-8.2735e-02],
        [ 2.0875e-01],
        [-2.9426e-01],
        [-7.6748e-02],
        [-1.5762e-01],
        [-5.7432e-01],
        [-5.2042e-01],
        [-1.5152e-01],
        [ 1.4119e+00],
        [-1.5752e-01],
        [-3.0565e-01],
        [-5.1378e-01],
        [-5.8924e-01],
        [-1.0163e+00],
        [-2.2021e-01],
        [ 2.9112e-02],
        [ 1.8521e-01],
        [ 6.2814e-01],
        [-6.8793e-01],
        [ 2.1395e-02],
        [ 5.7168e-01],
        [ 9.0977e-01],
        [ 3.8899e-01],
        [ 3.0209e-01],
        [ 2.4655e-01],
        [-1.1688e-01],
        [-5.9835e-02],
        [ 3.6426e-02],
        [-5.2782e-01],
        [ 1.4604e+00],
        [ 2.9685e-01],
        [-2.4077e-01],
        [ 1.0163e+00],
        [ 6.9770e-01],
        [-2.6183e-01],
        [ 3.6770e-01],
        [ 3.6535e-03],
        [ 4.2364e-01],
        [-5.4703e-01],
        [ 8.9173e-02],
        [-3.9032e-01],
        [-5.9740e-01],
        [ 3.7479e-02],
        [ 3.0257e-01],
        [ 8.2539e-02],
        [-6.0559e-01],
        [-4.3660e-01],
        [-7.0624e-01],
        [-5.0503e-01],
        [-4.0929e-01],
        [-2.3300e-01],
        [ 2.0298e-01],
        [-6.3697e-01],
        [-1.2584e-01],
        [ 5.6092e-02],
        [ 5.0150e-02],
        [-1.5358e-01],
        [ 2.9248e-02],
        [ 1.1180e-01],
        [-1.5535e-01],
        [ 1.1964e-01],
        [-6.5698e-01],
        [ 4.1923e-01],
        [ 7.4044e-02],
        [ 2.4536e-02],
        [ 3.2647e-01],
        [-7.7464e-01],
        [ 3.9898e-01],
        [-2.5777e-01],
        [ 8.5569e-02],
        [-4.0305e-01],
        [ 5.4463e-01],
        [-3.4124e-01],
        [-4.0789e-01],
        [ 4.2093e-01],
        [-3.8487e-01],
        [-4.0491e-01],
        [-2.1539e-01],
        [-1.7979e-02],
        [ 3.2492e-01],
        [-2.0894e-01],
        [ 2.5629e-01],
        [ 9.6046e-01]])

输出

tvalue.flatten()

tensor([-5.6117e-01,  3.5726e-01, -2.5853e-01, -4.8641e-01, -1.0581e-01,
        -1.8322e-01, -1.2732e+00, -5.9760e-02,  1.2819e-01,  6.3894e-02,
        -9.1817e-01, -1.6539e-01, -1.1471e+00,  1.9666e-01, -6.3297e-01,
        -4.0876e-01, -2.4590e-02,  2.7065e-01,  3.5308e-01, -4.6348e-01,
        -4.1755e-01, -1.1554e-01, -4.2062e-01,  1.4067e-01, -2.9788e-01,
        -7.4582e-02, -5.3751e-01,  1.1344e-01, -2.6100e-01,  2.6951e-02,
        -5.0437e-02, -1.9163e-01, -3.3893e-02, -5.9640e-01, -1.1574e-01,
         1.4613e-01,  1.2263e-01, -1.5566e-01,  1.4740e-01, -9.9924e-01,
         2.0878e-01, -2.0074e-01,  7.8383e-02,  7.4679e-02, -5.8065e-01,
         6.7777e-01,  5.9879e-01,  6.6301e-01, -4.7051e-01, -2.5468e-01,
        -2.7382e-01,  1.7585e-01,  3.6151e-01, -9.2532e-01, -1.6999e-01,
         8.4971e-02, -6.6083e-01, -3.1204e-02,  6.3712e-01, -5.8580e-02,
        -7.7901e-04, -4.6792e-01,  1.0796e-01,  7.8766e-01,  1.6809e-01,
        -7.0058e-01, -2.9299e-01, -8.2735e-02,  2.0875e-01, -2.9426e-01,
        -7.6748e-02, -1.5762e-01, -5.7432e-01, -5.2042e-01, -1.5152e-01,
         1.4119e+00, -1.5752e-01, -3.0565e-01, -5.1378e-01, -5.8924e-01,
        -1.0163e+00, -2.2021e-01,  2.9112e-02,  1.8521e-01,  6.2814e-01,
        -6.8793e-01,  2.1395e-02,  5.7168e-01,  9.0977e-01,  3.8899e-01,
         3.0209e-01,  2.4655e-01, -1.1688e-01, -5.9835e-02,  3.6426e-02,
        -5.2782e-01,  1.4604e+00,  2.9685e-01, -2.4077e-01,  1.0163e+00,
         6.9770e-01, -2.6183e-01,  3.6770e-01,  3.6535e-03,  4.2364e-01,
        -5.4703e-01,  8.9173e-02, -3.9032e-01, -5.9740e-01,  3.7479e-02,
         3.0257e-01,  8.2539e-02, -6.0559e-01, -4.3660e-01, -7.0624e-01,
        -5.0503e-01, -4.0929e-01, -2.3300e-01,  2.0298e-01, -6.3697e-01,
        -1.2584e-01,  5.6092e-02,  5.0150e-02, -1.5358e-01,  2.9248e-02,
         1.1180e-01, -1.5535e-01,  1.1964e-01, -6.5698e-01,  4.1923e-01,
         7.4044e-02,  2.4536e-02,  3.2647e-01, -7.7464e-01,  3.9898e-01,
        -2.5777e-01,  8.5569e-02, -4.0305e-01,  5.4463e-01, -3.4124e-01,
        -4.0789e-01,  4.2093e-01, -3.8487e-01, -4.0491e-01, -2.1539e-01,
        -1.7979e-02,  3.2492e-01, -2.0894e-01,  2.5629e-01,  9.6046e-01])