gpt4 book ai didi

python - Pytorch:如何将数据转换为张量

转载 作者:行者123 更新时间:2023-11-30 08:34:02 26 4
gpt4 key购买 nike

我是 Pytorch 的初学者。我正在尝试引用 Pytorch 教程编写 CNN 代码。下面是代码的一部分,但它显示错误“RuntimeError:变量数据必须是张量,但得到了列表”。我尝试将输入数据转换为张量,但效果不佳。如果有人知道解决方案,请帮助我......

    def read_labels(file):
dic = {}
with open(file) as f:
reader = f
for row in reader:
dic[row.split(",")[0]] = row.split(",")[1].rstrip() #rstrip(): eliminate "\n"
return dic

image_names= os.listdir("./train_mini")
label_dic = read_labels("labels.csv")


names =[]
labels = []
images =[]

for name in image_names[1:]:
images.append(cv2.imread("./train_mini/"+name))
labels.append(label_dic[os.path.splitext(name)[0]])

"""
Data distribution
"""
N = len(images)
N_train = int(N * 0.7)
N_test = int(N*0.2)

X_train, X_tmp, Y_train, Y_tmp = train_test_split(images, labels, train_size=N_train)
X_validation, X_test, Y_validation, Y_test = train_test_split(X_tmp, Y_tmp, test_size=N_test)

"""
Model Definition
"""

class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.head = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=10,
kernel_size=5, stride=1),
nn.MaxPool2d(kernel_size=2),
nn.ReLU(),
nn.Conv2d(10, 20, kernel_size=5),
nn.MaxPool2d(kernel_size=2),
nn.ReLU())
self.tail = nn.Sequential(
nn.Linear(320, 50),
nn.ReLU(),
nn.Linear(50, 10))

def forward(self, x):
x = self.head(x)
x = x.view(-1, 320)
x = self.tail(x)
return F.log_softmax(x)

CNN = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(CNN.parameters(), lr=0.001, momentum=0.9)


"""
Training
"""
batch_size = 50
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i in range(N / batch_size):
#for i, data in enumerate(trainloader, 0):
batch = batch_size * i

# get the inputs
images_batch = X_train[batch:batch + batch_size]
labels_batch = Y_train[batch:batch + batch_size]

# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)

# zero the parameter gradients
optimizer.zero_grad()

# forward + backward + optimize
outputs = CNN(images_batch)
loss = criterion(outputs, labels_batch)
loss.backward()
optimizer.step()

# print statistics
running_loss += loss.data[0]
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0

print('Finished Training')

这里发生错误

# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)

最佳答案

如果我的猜测是正确的,您可能会在以下行中收到错误。

# wrap them in Variable
images_batch, labels_batch = Variable(images_batch), Variable(labels_batch)

这意味着,images_batch和/或labels_batch是列表。您可以简单地将它们转换为 numpy 数组,然后转换为张量,如下所示。

# wrap them in Variable
images_batch = torch.from_numpy(numpy.array(images_batch))
labels_batch = torch.from_numpy(numpy.array(labels_batch))

应该能解决你的问题。

<小时/>

编辑:如果您在运行上述代码片段时遇到以下错误:

"RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8."

您可以通过指定数据类型来创建 numpy 数组。例如,

images_batch = torch.from_numpy(numpy.array(images_batch, dtype='int32'))

我假设images_batch包含图像的像素信息,所以我使用int32。欲了解更多信息,请参阅official documentation .

关于python - Pytorch:如何将数据转换为张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47272971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com