gpt4 book ai didi

python - 属性错误: 'int' object has no attribute 'view' (1)

转载 作者:行者123 更新时间:2023-11-30 21:53:38 25 4
gpt4 key购买 nike

我的代码如下,我的问题出在input_gray = input_gray.view(batch_size,1,64,32)。由于我使用了 enumerate,我猜想遇到了问题,但不知道如何解决它,我需要您的帮助,谢谢。

for epoch in range(epochs):
# Train for one epoch, then validate
train(train_loader, model, criterion, optimizer, epoch)
correct=0
total=0

with torch.no_grad():
losses = validate(val_loader, model, criterion, save_images, epoch)
for data in enumerate(train_loader):
input_gray, labels = data
input_gray = input_gray.view(batch_size,1,64,32)
input_gray = input_gray.float()

if use_gpu:
input_gray, labels = input_gray.to.cuda(), labels.to.cuda()

output_ab = model(input_gray)
_, predicted = torch.max(output_ab.data,1)
total+=labels.size()
correct+=(predicted==labels).sum().item()

print("Accuracy train %d %%"%(100*correct/total))
train_acc.append(100*correct/total)

# Save checkpoint and replace old best model if current model is better
if losses < best_losses:
best_losses = losses
torch.save(model.state_dict(), 'checkpoints/model-epoch-{}-losses-{:.3f}.pth'.format(epoch+1,losses))

最佳答案

如果枚举一个列表,您将获得每个项目及其索引,以元组 (index,item) 的形式返回。

class something:
def __init__(self,prop1,prop2):
self.prop1=prop1
self.prop2=prop2

l = [something(1,"a"),something(2,"b")]


for k in enumerate(l):
index, data = k # so k is a tuple of (index,item) - you can deref it

print(index)

# you can access the items properties like so:
print(data.prop1, data.prop2)

输出:

0
1 a
1
2 b

您的代码可能需要:

for data in enumerate(train_loader):
index, (input_gray, labels) = data

关于python - 属性错误: 'int' object has no attribute 'view' (1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590849/

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