. Advertisement .
..3..
. Advertisement .
..4..
An array is a collection of related data elements that are kept near to one another in memory. It is the simplest data structure because each data element can only be directly retrieved using its index number. As a result, it is easier to specify every element’s position by simply including an offset in a base value, or the location of the array’s first element in memory (generally denoted by the name of the array). The offset is the difference between the two indices, and index 0 acts as the base value. The error “TypeError: __array__() takes 1 positional argument but 2 were given” is a common problem that can show up in many ways. In this blog, we will go through some of the ways you can fix this issue. Read on.
When Do You Get The Error: “TypeError: __array__() takes 1 positional argument but 2 were given”?
In your stack track, you’re using pytorchand but you may get the following error.
File "/transforms.py", line 26, in __call__
image, target = t(image, target)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "/transforms.py", line 50, in forward
image = F.to_tensor(image)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py", line 129, in to_tensor
np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True)
TypeError: __array__() takes 1 positional argument but 2 were given
How To Solve The Error: “TypeError: __array__() takes 1 positional argument but 2 were given”?
Approach 1: Downgrade the torch library
Torch is a platform for scientific computing that prioritizes GPUs and offers extensive support for machine learning methods. Because of the fast and simple scripting language LuaJIT and the basic C/CUDA implementation, it is simple to use and effective.
Torch aims to give you the greatest flexibility and speed when creating your scientific algorithms while keeping things as straightforward as possible. Torch is based on the Lua community and comes with a sizable ecosystem of community-driven packages in machine learning, signal processing, computer vision, parallel processing, video, image and audio processing, among other areas.
Downgrading the torch library is a great solution for you to fix the error “TypeError: __array__() takes 1 positional argument but 2 were given”. If you downgrade the torch library, you should also downgrade torchvision, according to requirements.txt. The following version of torch and torchvision are error-free.
torch==1.9.0
torchvision==0.10.0
Downgrade from version two to an older version.
torch==1.8.1
torchvision==0.9.1
Approach 2: Downgrade pillow version
Another approach for you to solve your error is downgrading the pillow version. Simply set pillow to 8.2.0 as the following:
pip install pillow==8.2.0
Conclusion
We hope you will enjoy our article about the error. With this knowledge, we know that you can fix your issue: “TypeError: __array__() takes 1 positional argument but 2 were given” quickly by following these steps! If you still have any other questions about fixing this syntax error, please leave a comment below. Thank you for reading!
Read more
→ Python TypeError: ‘str’ OBject Does Not Support Item Assignment
Leave a comment