gpt4 book ai didi

neural-network - RuntimeError : Expected object of type torch. FloatTensor 但发现参数类型为 torch.cuda.DoubleTensor #2 'weight'

转载 作者:行者123 更新时间:2023-12-02 02:53:09 24 4
gpt4 key购买 nike

我一直在尝试重新训练模型,但不幸的是,最近 2 天我不断遇到同样的错误。

你能帮忙解决一下这个问题吗?

初步工作:

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import matplotlib.pyplot as plt
import numpy as np
import time

import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import datasets, transforms
import torchvision.models as models
from collections import OrderedDict

数据集:

data_dir = 'flowers'
train_dir = data_dir + '/train'

data_dir = 'flowers'

train_transforms = transforms.Compose([transforms.Resize(224),
transforms.RandomResizedCrop(224),
transforms.RandomRotation(45),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])


train_data = datasets.ImageFolder(train_dir, transform=train_transforms)

trainloader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)

import json

with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)

尝试使用预训练模型并仅训练分类器:

# Load a pretrained model
model = models.vgg16(pretrained=True)

# Keep the parameters the same
for param in model.parameters():
param.requires_grad = False


# and final output 102, since tht we have 102 flowers.
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088, 4096)),
('relu', nn.ReLU()),
('fc3', nn.Linear(4096, 102)),
('output', nn.LogSoftmax(dim=1))
]))

# Replace model's old classifier with the new classifier
model.classifier = classifier

# Calculate the loss
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=0.001)

model.to('cuda')

epochs = 1
print_every = 40
steps = 0

for e in range(epochs):
running_loss = 0
model.train()
# model = model.double()
for images, labels in iter(trainloader):
steps += 1

images.resize_(32, 3, 224, 224)

inputs = Variable(images.to('cuda'))
targets = Variable(labels.to('cuda'))

optimizer.zero_grad()

# Forward and backward passes
output = model.forward(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()

#running_loss += loss.data[0]
running_loss += loss.item()

if steps % print_every == 0:
print("Epoch: {}/{}... ".format(e+1, epochs),
"Loss: {:.4f}".format(running_loss/print_every))

错误信息:

RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.DoubleTensor for argument #2 weight

最佳答案


you should have passed inputs to the feed forward network but you have passed images to the network


# Forward and backward passes
output = model.forward(inputs)

关于neural-network - RuntimeError : Expected object of type torch. FloatTensor 但发现参数类型为 torch.cuda.DoubleTensor #2 'weight',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51081004/

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