{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Bala Priya C",
  "title_listed": "Useful Tensor Manipulation Functions in PyTorch",
  "keywords": "",
  "domain": "文本处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/balapriya/useful-tensor-manipulation-functions-in-pytorch-4g4c",
  "publish_date": "2021-03-14",
  "corpus_stage": "",
  "notes": "",
  "id": "2d0380555baa",
  "collected_at": "2026-10-02T14:01:48",
  "title": "Useful Tensor Manipulation Functions in PyTorch - DEV Community",
  "body_markdown": "PyTorch\nis a popular, open source, optimized tensor library widely used in deep learning and AI Research, developed by researchers at Facebook AI. The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined.\nIn this blog post, you'll learn some useful functions that the\ntorch\npackage provides for manipulating tensors. Specifically, you'll take the help of examples to understand how the different functions work, including cases where the functions do not perform as expected and throw errors. We shall look at the following tensor manipulation functions.\ntorch.cat\n: Concatenates the given sequence of tensors in the given dimension\ntorch.unbind\n: Removes a tensor dimension\ntorch.movedim\n: Moves the dimension(s) of input at the position(s) in source to the position(s) in destination\ntorch.squeeze\n: Returns a tensor with all the dimensions of input of size 1 removed.\ntorch.unsqueeze\n: Returns a new tensor with a dimension of size one inserted at the specified position.\nBefore we begin, let's import\ntorch\n.\nimport\ntorch\nEnter fullscreen mode\nExit fullscreen mode\n1.\ntorch.cat\ntorch.cat(tensors, dim=0, *, out=None)\nConcatenates the given sequence of tensors in the given dimension.\nAll tensors must either have the same shape (except in the concatenating dimension) or be empty.\nThe argument\ntensors\ndenotes the sequence of tensors to be concatenated.\ndim\nis an optional argument that specifies the dimension along which we want\ntensors\nto be concatenated. (\ndefault dim=0\n)\nout\nis an optional keyword argument.\n# Example 1\nip_tensor_1\n=\ntorch\n.\ntensor\n([[\n1\n,\n2\n,\n3\n],[\n4\n,\n5\n,\n6\n]])\nip_tensor_2\n=\ntorch\n.\ntensor\n([[\n7\n,\n8\n,\n9\n],[\n10\n,\n11\n,\n12\n]])\ntorch\n.\ncat\n((\nip_tensor_1\n,\nip_tensor_2\n),\ndim\n=\n0\n)\n# Output\ntensor\n([[\n1\n,\n2\n,\n3\n],\n[\n4\n,\n5\n,\n6\n],\n[\n7\n,\n8\n,\n9\n],\n[\n10\n,\n11\n,\n12\n]])\nEnter fullscreen mode\nExit fullscreen mode\nAs we specified\ndim=0\n, the input tensors have been concatenated along dimension 0. The input tensors each had shape (2,3) and as the tensors were concatenated along dimension 0, the output tensor is of shape (4,3)\n# Example 2\nip_tensor_1\n=\ntorch\n.\ntensor\n([[\n1\n,\n2\n,\n3\n],[\n4\n,\n5\n,\n6\n]])\nip_tensor_2\n=\ntorch\n.\ntensor\n([[\n7\n,\n8\n,\n9\n,\n10\n],[\n11\n,\n12\n,\n13\n,\n14\n]])\ntorch\n.\ncat\n((\nip_tensor_1\n,\nip_tensor_2\n),\ndim\n=\n1\n)\n# Output\ntensor\n([[\n1\n,\n2\n,\n3\n,\n7\n,\n8\n,\n9\n,\n10\n],\n[\n4\n,\n5\n,\n6\n,\n11\n,\n12\n,\n13\n,\n14\n]])\nEnter fullscreen mode\nExit fullscreen mode\nWell, this time, we chose to concatenate along the first dimension (\ndim=1\n).The\nip_tensor_1\nwas of shape (2,3) and the\nip_tensor_2\nwas of shape (2,4). As we chose to concatenate along the first dimension, the output tensor returned is of shape (2,7).\nNow, let's see what happens when we try to concatenate the above two input tensors along\ndim=0\n.\n# Example 3\nip_tensor_1\n=\ntorch\n.\ntensor\n([[\n1\n,\n2\n,\n3\n],[\n4\n,\n5\n,\n6\n]])\nip_tensor_2\n=\ntorch\n.\ntensor\n([[\n7\n,\n8\n,\n9\n,\n10\n],[\n11\n,\n12\n,\n13\n,\n14\n]])\ntorch\n.\ncat\n((\nip_tensor_1\n,\nip_tensor_2\n),\ndim\n=\n0\n)\n# Output\n---------------------------------------------------------------------------\nRuntimeError\nTraceback\n(\nmost\nrecent\ncall\nlast\n)\n<\nipython\n-\ninput\n-\n23\n-\ncb649a6e60ac\n>\nin\n<\nmodule\n>\n()\n3\nip_tensor_2\n=\ntorch\n.\ntensor\n([[\n7\n,\n8\n,\n9\n,\n10\n],[\n11\n,\n12\n,\n13\n,\n14\n]])\n4\n---->\n5\ntorch\n.\ncat\n((\nip_tensor_1\n,\nip_tensor_2\n),\ndim\n=\n0\n)\nRuntimeError\n:\nSizes\nof\ntensors\nmust\nmatch\nexcept\nin\ndimension\n0.\nGot\n3\nand\n4\nin\ndimension\n1\n(\nThe\noffending\nindex\nis\n1\n)\nEnter fullscreen mode\nExit fullscreen mode\nWe see that an error is thrown when we try to concatenate along\ndim=0\n. This is precisely because the size of the tensors should agree in all dimensions other than the one that we're concatenating along.\nHere,\nip_tensor_1\nhas size 3 along dim=1 whereas\nip_tensor_2\nhas size 4 along\ndim=1\nwhich is why we ran into an error.\nTherefore, we can use the\ntorch.cat\nfunction when we want to\nconcatenate\ntensors\nalong a valid dimension\nprovided the tensors have the\nsame size in all other dimensions\n.\n2.\ntorch.unbind\ntorch.unbind(input, dim=0)\nThis function removes the tensor dimension specified by the argument\ndim\n.(default dim=0)\nReturns a tuple of slices of the tensor along the specified\ndim\n.\n# Example 1\nip_tensor\n=\ntorch\n.\ntensor\n([[\n1\n,\n2\n,\n3\n],[\n4\n,\n5\n,\n6\n]])\ntorch\n.\nunbind\n(\nip_tensor\n,\ndim\n=\n0\n)\n# Output\n(\ntensor\n([\n1\n,\n2\n,\n3\n]),\ntensor\n([\n4\n,\n5\n,\n6\n]))\nEnter fullscreen mode\nExit fullscreen mode\nThe ip_tensor is of shape (2,3). As we specified\ndim=0\nwe can see that applying unbind along the\ndim=0\nreturns a tuple of slices of the\nip_tensor\nalong the zeroth dimension.\n# Example 2\nip_tensor\n=\ntorch\n.\ntensor\n([[\n1\n,\n2\n,\n3\n],[\n4\n,\n5\n,\n6\n]])\ntorch\n.\nunbind\n(\nip_tensor\n,\ndim\n=\n1\n)\n# Output\n(\ntensor\n([\n1\n,\n4\n]),\ntensor\n([\n2\n,\n5\n]),\ntensor\n([\n3\n,\n6\n]))\nEnter fullscreen mode\nExit fullscreen mode\nIn the above example, we see that when we choose to unbind along\ndim=1\n, we get a tuple containing three slices of the input tensor along the first dimension.\n# Example 3\nip_tensor\n=\ntorch\n.\nrandn\n(\n10\n,\n10\n)\ntorch\n.\nunbind\n(\nip_tensor\n,\ndim\n=\n2\n)\n# Output\n---------------------------------------------------------------------------\nIndexError\nTraceback\n(\nmost\nrecent\ncall\nlast\n)\n<\nipython\n-\ninput\n-\n19\n-\n0159\nbee72931\n>\nin\n<\nmodule\n>\n()\n2\nip_tensor\n=\ntorch\n.\nrandn\n(\n10\n,\n10\n)\n3\nprint\n(\nip_tensor\n)\n---->\n4\ntorch\n.\nunbind\n(\nip_tensor\n,\ndim\n=\n2\n)\nIndexError\n:\nDimension\nout\nof\nrange\n(\nexpected\nto\nbe\nin\nrange\nof\n[\n-\n2\n,\n1\n],\nbut\ngot\n2\n)\nEnter fullscreen mode\nExit fullscreen mode\nAs expected, we see that the input tensor is of shape (10,10) and when we choose to unbind along a dimension that is not valid, we run into an error.\nA clear understanding of dimensions and size along a specific dimension is necessary; Even though our input tensor has 100 elements and has size 10 in each of the dimensions 0 and 1 it does not have a third dimension of index 2; hence, it's important to pass in a valid dimension for the tensor manipulation operations.\nThe unbind function can be useful when we would like to\nexamine slices of a tensor\nalong a\nspecified input dimension\n.\n3.\ntorch.movedim\ntorch.movedim(input, source, destination)\nThis function moves the dimensions of input at the positions in\nsource\nto the positions specified in\ndestination\n.\nsource\nand\ndestination\ncan be either\nint\n(single dimension) or\ntuple\nof dimensions to be moved.\nOther dimensions of input that are not explicitly moved remain in their original order and appear at the positions not specified in destination.\n# Example 1\nip_tensor\n=\ntorch\n.\nrandn\n(\n4\n,\n3\n,\n2\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nmovedim\n(\nip_tensor\n,\n1\n,\n2\n)\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n4\n,\n3\n,\n2\n])\nOutput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n4\n,\n2\n,\n3\n])\nEnter fullscreen mode\nExit fullscreen mode\nIn this example, we wanted to move the dimension 1 in the input tensor to dimension 2 in the output tensor & we've done just that using the movedim function.\nip_tensor\nis of shape (4,3,2) whereas\nop_tensor\nis of shape (4,2,3) that is,\ndim1\nin input tensor has moved to\ndim2\nin the output tensor.\n# Example 2\nip_tensor\n=\ntorch\n.\nrandn\n(\n4\n,\n3\n,\n2\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nmovedim\n(\nip_tensor\n,(\n1\n,\n0\n),(\n2\n,\n1\n))\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n4\n,\n3\n,\n2\n])\nOutput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n4\n,\n3\n])\nEnter fullscreen mode\nExit fullscreen mode\nIn this example,we want to move dimensions 1 and 0 in input tensor to dimensions 2 and 1 in the output tensor. And we see that this change has been reflected by checking the shape of the respective tensors.\n# Example 3\nip_tensor\n=\ntorch\n.\nrandn\n(\n4\n,\n3\n,\n2\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nmovedim\n(\nip_tensor\n,(\n1\n,\n0\n),(\n1\n,\n1\n))\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n4\n,\n3\n,\n2\n])\n---------------------------------------------------------------------------\nRuntimeError\nTraceback\n(\nmost\nrecent\ncall\nlast\n)\n<\nipython\n-\ninput\n-\n14\n-\nf3275d938a7d\n>\nin\n<\nmodule\n>\n()\n2\nip_tensor\n=\ntorch\n.\nrandn\n(\n4\n,\n3\n,\n2\n)\n3\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\n---->\n4\nop_tensor\n=\ntorch\n.\nmovedim\n(\nip_tensor\n,(\n1\n,\n0\n),(\n1\n,\n1\n))\n5\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\nRuntimeError\n:\nmovedim\n:\nrepeated\ndim\nin\n`destination`\n([\n1\n,\n1\n])\nEnter fullscreen mode\nExit fullscreen mode\nIn this example, we get an error as we've repeated dimension 1 in the destination tuple. The entries in\nsource\nand\ndestination\ntuples\nshould all be unique\n.\nThus, the function\nmovedim\nhelps in going from a tensor of specified shape to another while retaining the\nsame underlying elements\n.\n4.\ntorch.squeeze\ntorch.squeeze(input, dim=None, *, out=None)\nThis operation returns a tensor with all the dimensions of\ninput\nof size 1 removed.\nWhen\ndim\nis specified, then squeeze operation is done only along that dimension.\n# Example 1\nip_tensor\n=\ntorch\n.\nrandn\n(\n2\n,\n1\n,\n3\n)\nprint\n(\nip_tensor\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nsqueeze\n(\nip_tensor\n)\nprint\n(\nop_tensor\n)\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\ntensor\n([[[\n0.2819\n,\n0.3406\n,\n-\n1.8031\n]],\n[[\n-\n0.9314\n,\n1.0048\n,\n-\n0.3198\n]]])\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n1\n,\n3\n])\ntensor\n([[\n0.2819\n,\n0.3406\n,\n-\n1.8031\n],\n[\n-\n0.9314\n,\n1.0048\n,\n-\n0.3198\n]])\nOutput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n3\n])\nEnter fullscreen mode\nExit fullscreen mode\nWe had\nip_tensor\nof shape (2,1,3). In the\nop_tensor\nafter squeezing operation, we have shape (2,3). In the input tensor\nip_tensor\nthe second dimension of size 1 has been dropped.\n# Example 2 a\nip_tensor\n=\ntorch\n.\nrandn\n(\n2\n,\n1\n,\n3\n,\n1\n)\nprint\n(\nip_tensor\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nsqueeze\n(\nip_tensor\n,\ndim\n=\n0\n)\nprint\n(\nop_tensor\n)\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\ntensor\n([[[[\n0.4133\n],\n[\n-\n0.6541\n],\n[\n-\n0.5506\n]]],\n[[[\n-\n1.1734\n],\n[\n-\n0.3823\n],\n[\n-\n0.8710\n]]]])\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n1\n,\n3\n,\n1\n])\ntensor\n([[[[\n0.4133\n],\n[\n-\n0.6541\n],\n[\n-\n0.5506\n]]],\n[[[\n-\n1.1734\n],\n[\n-\n0.3823\n],\n[\n-\n0.8710\n]]]])\nOutput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n1\n,\n3\n,\n1\n])\nEnter fullscreen mode\nExit fullscreen mode\nIn the above example, we set the dimension argument\ndim=0\n. The input tensor\nip_tensor\nhas\nsize=2\nalong\ndim=0\n. As we did not have\nsize=1\nalong\ndim=0\n, there's no effect of squeezing operation on the tensor and the output tensor is identical to the input tensor.\n# Example 2 b\nip_tensor\n=\ntorch\n.\nrandn\n(\n2\n,\n1\n,\n3\n,\n1\n)\nprint\n(\nip_tensor\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nshape\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nsqueeze\n(\nip_tensor\n,\ndim\n=\n1\n)\nprint\n(\nop_tensor\n)\nprint\n(\nf\n\"\nOutput Tensor shape:\n{\nop_tensor\n.\nshape\n}\n\\n\n\"\n)\n# Output\ntensor\n([[[[\n-\n1.7004\n],\n[\n-\n0.1863\n],\n[\n1.1550\n]]],\n[[[\n-\n1.1890\n],\n[\n-\n0.4821\n],\n[\n-\n0.3731\n]]]])\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n1\n,\n3\n,\n1\n])\ntensor\n([[[\n-\n1.7004\n],\n[\n-\n0.1863\n],\n[\n1.1550\n]],\n[[\n-\n1.1890\n],\n[\n-\n0.4821\n],\n[\n-\n0.3731\n]]])\nOutput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n3\n,\n1\n])\nEnter fullscreen mode\nExit fullscreen mode\nIn the above example, we set the dimension argument\ndim=1\n.\nAs the tensor had\nsize=1\nalong the first dimension, in the output tensor, that dimension was removed and the output tensor is of shape (2,3,1).\n# Example 3\nip_tensor\n=\ntorch\n.\nrandn\n(\n2\n,\n3\n)\nprint\n(\nip_tensor\n)\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nsize\n()\n}\n\\n\n\"\n)\nop_tensor\n=\ntorch\n.\nsqueeze\n(\nip_tensor\n,\ndim\n=\n2\n)\n# Output\ntensor\n([[\n-\n0.0688\n,\n-\n0.7170\n,\n-\n1.5563\n],\n[\n-\n0.2138\n,\n-\n0.5387\n,\n-\n1.0245\n]])\nInput\nTensor\nshape\n:\ntorch\n.\nSize\n([\n2\n,\n3\n])\n---------------------------------------------------------------------------\nIndexError\nTraceback\n(\nmost\nrecent\ncall\nlast\n)\n<\nipython\n-\ninput\n-\n8\n-\nd312de22f1d3\n>\nin\n<\nmodule\n>\n()\n3\nprint\n(\nip_tensor\n)\n4\nprint\n(\nf\n\"\nInput Tensor shape:\n{\nip_tensor\n.\nsize\n()\n}\n\\n\n\"\n)\n---->\n5\nop_tensor\n=\ntorch\n.\nsqueeze\n(\nip_tensor\n,\ndim\n=\n2\n)\nIndexError\n:\nDimension\nout\nof\nrange\n(\nexpected\nto\nbe\nin\nrange\nof\n[\n-\n2\n,\n1\n],\nbut\ngot\n2\n)\nEnter fullscreen mode\nExit fullscreen mode\nIn the above example, we see that\nip_tensor\nhas shape (2,3) and is a 2-D tensor with dim=0,1 defined. As we tried to squeeze along\ndim=2\nwhich does not exist in the original tensor, we get an\nIndexError\n.\nIn essence,\nsqueeze\nfunction helps remove\nall dimensions of size 1\nor\nalong a specific dimension\n.\n5.\ntorch.unsquueze\ntorch.unsqueeze(input, dim)\nHere,\ndim\ndenotes the index at which we want the dimension of size 1 to be inserted.\nThis function returns a new tensor with a dimension of size one inserted at the specified position. The returned tensor shares the same underlying data with this tensor.\n# Example 1\nip_tensor\n=\ntorch\n.\ntensor\n([\n1\n,\n2\n,\n3\n,\n4\n])\ntorch\n.\nunsqueeze\n(\nip_tensor\n,\n0\n)\n# Output\ntensor\n([[\n1\n,\n2\n,\n3\n,\n4\n]])\nEnter fullscreen mode\nExit fullscreen mode\nIn this simple example shown above, unsqueeze inserts a singleton dimension at the specified index 0.\n# Example 2\nip_tensor\n=\ntorch\n.\nrand\n(\n2\n,\n3\n)\ntorch\n.\nunsqueeze\n(\nip_tensor\n,\n2\n)\n# Output\ntensor\n([[[\n0.3670\n],\n[\n0.1786\n],\n[\n0.7115\n]],\n[[\n0.4241\n],\n[\n0.0422\n],\n[\n0.2277\n]]])\nEnter fullscreen mode\nExit fullscreen mode\nIn this simple example shown above,\nunsqueeze\ninserts a singleton dimension at the specified index 2 (the input is of dimension 2 (0,1) and we have inserted a new dimension of size 1 along dim=2).\n# Example 3\nip_tensor\n=\ntorch\n.\nrand\n(\n2\n,\n3\n)\ntorch\n.\nunsqueeze\n(\nip_tensor\n,\n3\n)\n# Output\n--------------------------------------------------------------------------\nIndexError\nTraceback\n(\nmost\nrecent\ncall\nlast\n)\n<\nipython\n-\ninput\n-\n28\n-\ne1fcd3f3f58a\n>\nin\n<\nmodule\n>\n()\n1\n# Example 3\n2\nip_tensor\n=\ntorch\n.\nrand\n(\n2\n,\n3\n)\n---->\n3\ntorch\n.\nunsqueeze\n(\nip_tensor\n,\n3\n)\nIndexError\n:\nDimension\nout\nof\nrange\n(\nexpected\nto\nbe\nin\nrange\nof\n[\n-\n3\n,\n2\n],\nbut\ngot\n3\n)\nEnter fullscreen mode\nExit fullscreen mode\nWe get an index error as expected; This is because the argument\ndim\ncan only take values upto\ninput_dim+1\n. In this case,\ndim\ncan take a maximum value of 2.\nThus\nunsqueeze\nfunction lets us insert\ndimension of size 1\nat the required index.\nIn this post, we've tried to cover some useful functions that can be used for manipulating tensors. Hope you found it useful. Happy Learning!\nReferences\n[1] Official documentation for\ntensor operations\n[2] A useful blog on basics of tensors:\nhttps://www.kdnuggets.com/2018/05/pytorch-tensor-basics.html\nCover Image:\nPhoto by\nAnnie Spratt\non\nUnsplash",
  "metadata": {
    "title": "Useful Tensor Manipulation Functions in PyTorch - DEV Community",
    "description": "PyTorch is a popular, open source, optimized tensor library widely used in deep learning and AI... Tagged with python, deeplearning, beginners, tutorial.",
    "keywords": "python, deeplearning, beginners, tutorial, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/balapriya/useful-tensor-manipulation-functions-in-pytorch-4g4c",
    "og:title": "Useful Tensor Manipulation Functions in PyTorch",
    "og:description": "PyTorch is a popular, open source, optimized tensor library widely used in deep learning and AI...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tz1zxs0nxylvolkxgss.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Useful Tensor Manipulation Functions in PyTorch",
    "twitter:description": "PyTorch is a popular, open source, optimized tensor library widely used in deep learning and AI...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2tz1zxs0nxylvolkxgss.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/2d0380555baa/code/body-000.txt",
      "chars": 13,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-001.txt",
      "chars": 246,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-002.txt",
      "chars": 240,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-003.txt",
      "chars": 603,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-004.txt",
      "chars": 133,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-005.txt",
      "chars": 143,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-006.txt",
      "chars": 463,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-007.txt",
      "chars": 274,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-008.txt",
      "chars": 283,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-009.txt",
      "chars": 709,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-010.txt",
      "chars": 456,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-011.txt",
      "chars": 586,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-012.txt",
      "chars": 574,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-013.txt",
      "chars": 683,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-014.txt",
      "chars": 114,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-015.txt",
      "chars": 195,
      "language": "text"
    },
    {
      "file": "attachments/2d0380555baa/code/body-016.txt",
      "chars": 450,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://pytorch.org/docs/stable/torch.html",
      "label": "tensor operations",
      "type": "a"
    },
    {
      "href": "https://www.kdnuggets.com/2018/05/pytorch-tensor-basics.html",
      "label": "https://www.kdnuggets.com/2018/05/pytorch-tensor-basics.html",
      "type": "a"
    },
    {
      "href": "https://unsplash.com/@anniespratt?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Annie Spratt",
      "type": "a"
    },
    {
      "href": "https://unsplash.com/s/photos/study?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText",
      "label": "Unsplash",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:13Z",
  "source_url": "https://dev.to/balapriya/useful-tensor-manipulation-functions-in-pytorch-4g4c",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 14729,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/2d0380555baa/pages/source.html",
  "local_html": "attachments/2d0380555baa/pages/article.html",
  "body_base_path": "attachments/2d0380555baa/pages/",
  "body_markdown_path": "attachments/2d0380555baa/pages/article.md"
}