I am quite new to python and pytorch. Please review my code below. I have tried everything I know but I am not able to create a MNIST data set image out of the matrix below. I expect the image should be 1. It would be great if someone can help me in it.
import torch
import torch.nn.functional as F
import torch.optim as optim
import torchquantum as tq
import torchquantum.functional as tqf
from torchquantum.datasets import MNIST
from torch.optim.lr_scheduler import CosineAnnealingLR
import random
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
dataset = MNIST(root='../Data_Manu',
train_valid_split_ratio=[0.9, 0.1],
digits_of_interest=[3, 6],
n_test_samples=75)
data_value =dataset['train'][0]
## Output is below
{'image': tensor([[[-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 0.1740, 2.5415, 2.7960, 2.7960, 1.4214, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.1668, 2.5415, 2.7833, 2.7833, 2.7833, 2.2105, -0.1696, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.2842, 0.3140, 2.3887, 2.7960, 2.7833, 2.7069, 2.3124, 1.1668, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4115, 1.5487, 2.7833, 2.7833, 2.7960, 2.2487, 0.7468, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.7523, 2.7833, 2.7833, 2.7833, 2.1978, -0.1696, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.2842, 0.5049, 2.7960, 2.7833, 2.7833, 2.2487, -0.1696, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.3577, 2.7833, 2.7960, 2.7833, 2.1851, -0.0296, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.1668, 2.3887, 2.7833, 2.7960, 2.2487, -0.0296, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 0.9759, 2.7960, 2.7960, 2.7960, 2.8215, 1.0904, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4115, 1.4723, 2.7833, 2.7833, 2.7833, 0.0213, -0.3606, -0.4242, 0.1104, -0.0169, -0.2969, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.1569, 2.7833, 2.7833, 2.7833, 1.4596, -0.4242, -0.0169, 1.3577, 2.3887, 2.2742, 1.4723, -0.0169, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.1569, 2.1978, 2.7833, 2.7833, 2.7833, 0.9504, 1.4214, 2.5924, 2.7833, 2.7833, 2.7960, 2.7833, 2.3124, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 0.0467, 2.7960, 2.7960, 2.7960, 2.7960, 2.7960, 2.8215, 2.7960, 2.7960, 2.7960, 2.8215, 2.7960, 2.5287, 0.1740, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 0.0467, 2.7833, 2.7833, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.6433, 0.5559, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.3577, 2.7833, 2.7833, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.3124, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.8796, 2.7833, 2.7833, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.7833, 2.7833, 2.7960, 2.2487, 0.7468, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.8923, 2.7960, 2.7960, 2.7960, 2.7960, 2.7960, 2.8215, 2.7960, 2.7960, 2.7960, 1.4214, -0.1696, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 1.3450, 2.7833, 2.7833, 2.7833, 2.7833, 2.7833, 2.7960, 2.7833, 2.1214, 0.8104, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, 0.0467, 2.7833, 2.7833, 2.7833, 2.4524, 2.3124, 0.4922, 0.4795, -0.1696, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.2206, 1.9942, 2.7833, 2.2487, -0.0296, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242], [-0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242, -0.4242]]]), 'digit': 1}
plt.imshow(data_value.numpy()[0], cmap='gray')
AttributeError Traceback (most recent call last)
<ipython-input-7-498b4257facf> in <module>
----> 1 plt.imshow(data_value.numpy()[0], cmap='gray')
AttributeError: 'dict' object has no attribute 'numpy'
Thank you for the great help.
Try change this plt.imshow(data_value.numpy()[0], cmap='gray')
to plt.imshow(data_value['image'].numpy()[0], cmap='gray')
.
Your output is not a torch.Tensor is dict than contains two labels "image"
(Tensor
) and "digit"
(int
).
Is for that reason you have this error AttributeError: 'dict' object has no attribute 'numpy'