{"id":22514,"library":"unfoldnd","title":"unfoldNd","description":"Unfold and fold operations for N-dimensional tensors in PyTorch, generalizing torch.nn.Unfold and torch.nn.Fold to arbitrary dimensions. Current version: 0.2.3. Released irregularly, latest in Dec 2024.","status":"active","version":"0.2.3","language":"python","source_language":"en","source_url":"https://github.com/f-dangel/unfoldNd","tags":["pytorch","unfold","fold","im2col","col2im","n-dimensional","neural-networks"],"install":[{"cmd":"pip install unfoldnd","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Core dependency; library operates on PyTorch tensors.","package":"torch","optional":false}],"imports":[{"note":"Package name is 'unfoldNd' with capital N, but pip install uses all lowercase.","wrong":"from unfoldnd import unfoldNd","symbol":"unfoldNd","correct":"from unfoldNd import unfoldNd"},{"note":"","wrong":"","symbol":"foldNd","correct":"from unfoldNd import foldNd"}],"quickstart":{"code":"import torch\nfrom unfoldNd import unfoldNd, foldNd\n\nx = torch.randn(1, 3, 10, 10, 10)  # N=3 kernel size 3x3x3\npatches = unfoldNd(x, kernel_size=3, dilation=1, padding=0, stride=1)\nprint(patches.shape)  # torch.Size([1, 27, 512])\n\n# Fold back (example assumes same parameters)\nx_reconstructed = foldNd(patches, output_size=(10,10,10), kernel_size=3, dilation=1, padding=0, stride=1)\nprint(x_reconstructed.shape)  # torch.Size([1, 3, 10, 10, 10])","lang":"python","description":"Demonstrates 3D unfolding and folding with a 3x3x3 kernel."},"warnings":[{"fix":"Use 'from unfoldNd import ...' after 'pip install unfoldnd'.","message":"The package name on PyPI is 'unfoldnd' (all lowercase), but the import uses 'unfoldNd' (capital N). This mismatch often causes ImportError.","severity":"gotcha","affected_versions":"all"},{"fix":"Always provide 'output_size' matching the spatial dimensions expected after folding.","message":"foldNd requires explicit 'output_size' argument; it cannot be inferred from the input tensor shape. Missing it raises a TypeError.","severity":"gotcha","affected_versions":"<0.2.0?"},{"fix":"Update to >=0.2.0 and adjust if using fold operations.","message":"Version 0.2.0 changed the API: 'foldNd' was added and some internal parameters may differ from older versions.","severity":"deprecated","affected_versions":"<0.2.0"},{"fix":"Ensure dilation, padding, stride are either int or tuples of length equal to the spatial dims of input.","message":"The 'dilation' parameter must be a single int or tuple matching the dimension count. Using incompatible shapes causes a RuntimeError.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run: from unfoldNd import unfoldNd","cause":"Mixing up package name (unfoldnd) with module name (unfoldNd).","error":"ImportError: cannot import name 'unfoldNd' from 'unfoldnd'"},{"fix":"Add 'output_size=(D, H, W)' (or appropriate dimensions) to the foldNd call.","cause":"foldNd requires output_size parameter; it is not optional.","error":"TypeError: foldNd() missing 1 required positional argument: 'output_size'"},{"fix":"Ensure input tensor has batch and channel dimensions, e.g., reshape to (1, 1, *spatial_dims) if needed.","cause":"unfoldNd expects at least a 4D tensor (batch, channels, spatial...).","error":"RuntimeError: Expected 4D or 5D input tensor, but got 3D"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}