【问题标题】:how to find shape of inputs tensor?如何找到输入张量的形状?
【发布时间】:2021-02-16 20:58:46
【问题描述】:
import torchvision
from torch.utils.data import Dataset, DataLoader
from torch import from_numpy, tensor
import numpy as np
trans = transforms.Compose(
    [
     transforms.Resize(128),
     transforms.ToTensor()
    ])
class UBFCDataset(Dataset):

    def __init__(self):
       

        xy = np.loadtxt('/content/drive/My Drive/Subject3hr.csv')
        self.x_data = torch.from_numpy(xy[:])
        self.len = xy.shape[0]
       

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        train_data_tensor = torchvision.datasets.ImageFolder("/content/drive/My Drive/Meta-rPPG-master/da", transform=trans)
        
        return train_data_tensor[index], self.x_data[index]

dataset = UBFCDataset()
dataset[0]
train_loader = DataLoader(dataset=dataset,batch_size=128,shuffle=True)

for epoch in range(2):
    for i, data in enumerate(train_loader, 0):
        # get the inputs
        inputs, labels = data
        print(inputs.shape)
        # Run your training process
        print(f'Epoch: {i} | Inputs {inputs} | Labels {labels}')

错误:


AttributeError                            Traceback (most recent call last)
<ipython-input-156-fecbaf453580> in <module>()
      4         inputs, labels = data
      5 
----> 6         print(inputs.shape)
      7         # Run your training process
      8         print(f'Epoch: {i} | Inputs {inputs} | Labels {labels}')

AttributeError: 'list' object has no attribute 'shape'

【问题讨论】:

  • 错误描述inputs变量是一个列表(而不是数组),

标签: python deep-learning pytorch tensor


【解决方案1】:

您假设变量类型是一个 numpy 数组,而它实际上是一个原始列表。这是固定的代码:

for epoch in range(2):
    for i, data in enumerate(train_loader, 0):
        # get the inputs
        inputs, labels = data
        inputs = np.array(inputs)
        print(inputs.shape)
        # Run your training process
        print(f'Epoch: {i} | Inputs {inputs} | Labels {labels}')

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多