ValueError: object array method not producing an array and TypeError: Invalid shape (1, 1, 6, 6) for image data
When attempting to run the following code, the following errors occur.
Code
import torch
import matplotlib.pyplot as plt
data = torch.tensor([[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0]]).view(-1, 1, 6, 6).float()
# Convert data to a 2D array
data_np = data[0, 0].cpu().detach().numpy()
plt.imshow(data_np, cmap='gray')
plt.axis('off')
plt.show()
Error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\tkinter__init__.py", line 1968, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1264.0_x64__qbz5n2kfra8p0\Lib\tkinter__init__.py", line 862, in callit
func(*args)
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\backends_backend_tk.py", line 271, in idle_draw
self.draw()
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 10, in draw
super().draw()
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\backends\backend_agg.py", line 387, in draw
self.figure.draw(self.renderer)
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\artist.py", line 95, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\figure.py", line 3154, in draw
self.patch.draw(renderer)
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\artist.py", line 72, in draw_wrapper
return draw(artist, renderer)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\patches.py", line 632, in draw
self._draw_paths_with_artist_properties(
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\patches.py", line 617, in _draw_paths_with_artist_properties
renderer.draw_path(gc, *draw_path_args)
File "C:\Users\mori\Desktop\ML_learning\venv\Lib\site-packages\matplotlib\backends\backend_agg.py", line 131, in draw_path
self._renderer.draw_path(gc, path, transform, rgbFace)
ValueError: object __array__ method not producing an array
My environments
- torch==2.3.1+cu121
- numpy==1.26.4
- matplotlib==3.9.0
- python==3.12.4
How can I solve these issues?
New contributor
M.M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.