AttributeError: module ‘torch’ has no attribute ‘permute’ error occurs because the “torch module does not have an attribute called permute in the older version of the PyTorch library.”
To fix the AttributeError: module ‘torch’ has no attribute ‘permute’ error, “upgrade the PyTorch library to the latest version using the pip install –upgrade torch torchvision command.”
To upgrade to a specific version of PyTorch, you can specify the version number in the command.
pip install --upgrade torch==1.7.0 torchvision==0.8.1
If you are using Anaconda, use the conda package manager to upgrade PyTorch:
conda install pytorch torchvision -c pytorch
Let’s write a simple PyTorch program in which “torch” has a “permute” attribute.
import torch
batch_size, length, dim = 32, 5, 200
inputs = torch.randn(batch_size, length, dim)
inputs = torch.permute(inputs, (1, 0, 2))
print(inputs)
Output
tensor([[[ 1.0026, -2.7712, -0.7772, ..., -0.6685, -0.4177, 0.6504],
[ 0.2645, -0.4712, -1.5182, ..., 1.4168, 0.9946, -0.8884],
[-0.6747, 0.4766, -0.3585, ..., 0.3867, 0.2957, -2.0025],
...,
[ 0.5598, -0.5110, 0.8959, ..., 0.7500, 0.3673, -1.3500],
[-1.2054, 0.4063, 0.0761, ..., 0.0068, -1.3447, -1.1213],
[ 1.1985, 0.2248, 0.5502, ..., -1.3683, 1.4266, -0.7689]],
[[-0.8764, 0.3507, -0.5025, ..., -1.1899, 1.2798, -0.1717],
[ 1.2368, -0.1195, -0.1074, ..., -1.0518, 0.9270, -0.6185],
[ 0.6959, -1.4283, 0.9314, ..., 1.2142, 2.2323, 0.2987],
...,
[-1.0119, -0.7466, -1.4464, ..., 0.6538, 1.1523, -0.2886],
[ 0.1741, 0.1881, 0.1870, ..., 0.0029, 0.7537, -0.0494],
[ 0.3870, 0.1019, 0.1006, ..., -0.8216, 0.7249, 0.9346]],
...,
[-1.2947, -0.6852, -0.6406, ..., -0.2920, -2.4572, 0.2402],
[ 1.5241, -0.0057, -0.5739, ..., -1.5625, 1.0062, -0.3826],
[-0.8617, -0.9933, 0.0583, ..., -1.8369, 0.0189, -0.6340]]])
Using an older PyTorch version, you can use the transpose() function instead of permute() to resolve this AttributeError.
If you are using the latest version of PyTorch, please check the spelling and capitalization of permute() function, and make sure that you have imported the correct module.
That’s it!
Related posts
AttributeError: module ‘torchtext.data’ has no attribute ‘Field’
ImportError: cannot import name ‘SAVE_STATE_WARNING’ from ‘torch.optim.lr_scheduler’

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.