Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid index of a 0-dim tensor #8

Open
tju-sun-lab opened this issue Apr 13, 2022 · 1 comment
Open

invalid index of a 0-dim tensor #8

tju-sun-lab opened this issue Apr 13, 2022 · 1 comment

Comments

@tju-sun-lab
Copy link

直接跑生成的script报错
indi-gen00-no00.py
Files already downloaded and verified
Files already downloaded and verified
/home/nature/anaconda3/envs/nas/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)
return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Exception occurs, file:indi-gen00-no00, pid:18314...invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a number

以下是indi-gen00-no00.py文件内部内容:
"""
2022-04-13 19:46:58
"""
from future import print_function
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torch.optim as optim
import data_loader
import os
from datetime import datetime
import multiprocessing
from utils import StatusUpdateTool

class ResNetBottleneck(nn.Module):
expansion = 1

def __init__(self, in_planes, planes, stride=1):
    super(ResNetBottleneck, self).__init__()
    self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
    self.bn1 = nn.BatchNorm2d(planes)
    self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
    self.bn2 = nn.BatchNorm2d(planes)
    self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False)
    self.bn3 = nn.BatchNorm2d(self.expansion*planes)

    self.shortcut = nn.Sequential()
    if stride != 1 or in_planes != self.expansion*planes:
        self.shortcut = nn.Sequential(
            nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm2d(self.expansion*planes)
        )

def forward(self, x):
    out = F.relu(self.bn1(self.conv1(x)))
    out = F.relu(self.bn2(self.conv2(out)))
    out = self.bn3(self.conv3(out))
    out += self.shortcut(x)
    out = F.relu(out)
    return out

class ResNetUnit(nn.Module):
def init(self, amount, in_channel, out_channel):
super(ResNetUnit, self).init()
self.in_planes = in_channel
self.layer = self._make_layer(ResNetBottleneck, out_channel, amount, stride=1)

def _make_layer(self, block, planes, num_blocks, stride):
    strides = [stride] + [1]*(num_blocks-1)
    layers = []
    for stride in strides:
        layers.append(block(self.in_planes, planes, stride))
        self.in_planes = planes * block.expansion
    return nn.Sequential(*layers)
def forward(self, x):
    out = self.layer(x)
    return out

class DenseNetBottleneck(nn.Module):
def init(self, nChannels, growthRate):
super(DenseNetBottleneck, self).init()
interChannels = 4*growthRate
self.bn1 = nn.BatchNorm2d(nChannels)
self.conv1 = nn.Conv2d(nChannels, interChannels, kernel_size=1,
bias=False)
self.bn2 = nn.BatchNorm2d(interChannels)
self.conv2 = nn.Conv2d(interChannels, growthRate, kernel_size=3,
padding=1, bias=False)

def forward(self, x):
    out = self.conv1(F.relu(self.bn1(x)))
    out = self.conv2(F.relu(self.bn2(out)))
    out = torch.cat((x, out), 1)
    return out

class DenseNetUnit(nn.Module):
def init(self, k, amount, in_channel, out_channel, max_input_channel):
super(DenseNetUnit, self).init()
self.out_channel = out_channel
if in_channel > max_input_channel:
self.need_conv = True
self.bn = nn.BatchNorm2d(in_channel)
self.conv = nn.Conv2d(in_channel, max_input_channel, kernel_size=1, bias=False)
in_channel = max_input_channel

    self.layer = self._make_dense(in_channel, k, amount)

def _make_dense(self, nChannels, growthRate, nDenseBlocks):
    layers = []
    for _ in range(int(nDenseBlocks)):
        layers.append(DenseNetBottleneck(nChannels, growthRate))
        nChannels += growthRate
    return nn.Sequential(*layers)
def forward(self, x):
    out = x
    if hasattr(self, 'need_conv'):
        out = self.conv(F.relu(self.bn(out)))
    out = self.layer(out)
    assert(out.size()[1] == self.out_channel)
    return out

class EvoCNNModel(nn.Module):
def init(self):
super(EvoCNNModel, self).init()

    #resnet and densenet unit
    self.op0 = ResNetUnit(amount=7, in_channel=3, out_channel=64)
    self.op1 = ResNetUnit(amount=7, in_channel=64, out_channel=256)
    self.op6 = DenseNetUnit(k=12, amount=10, in_channel=256, out_channel=248, max_input_channel=128)
    self.op7 = ResNetUnit(amount=7, in_channel=248, out_channel=128)
    self.op8 = ResNetUnit(amount=6, in_channel=128, out_channel=256)

    #linear unit
    self.linear = nn.Linear(1024, 10)


def forward(self, x):
    out_0 = self.op0(x)
    out_1 = self.op1(out_0)
    out_2 = F.avg_pool2d(out_1, 2)
    out_3 = F.avg_pool2d(out_2, 2)
    out_4 = F.max_pool2d(out_3, 2)
    out_5 = F.avg_pool2d(out_4, 2)
    out_6 = self.op6(out_5)
    out_7 = self.op7(out_6)
    out_8 = self.op8(out_7)
    out = out_8

    out = out.view(out.size(0), -1)
    out = self.linear(out)
    return out

class TrainModel(object):
def init(self):
trainloader, validate_loader = data_loader.get_train_valid_loader('../data_cifar10', batch_size=128, augment=True, valid_size=0.1, shuffle=True, random_seed=2312390, show_sample=False, num_workers=1, pin_memory=True)
#testloader = data_loader.get_test_loader('../data_cifar10', batch_size=128, shuffle=False, num_workers=1, pin_memory=True)
net = EvoCNNModel()
cudnn.benchmark = True
net = net.cuda()
criterion = nn.CrossEntropyLoss()
best_acc = 0.0
self.net = net
self.criterion = criterion
self.best_acc = best_acc
self.trainloader = trainloader
self.validate_loader = validate_loader
self.file_id = os.path.basename(file).split('.')[0]
#self.testloader = testloader
#self.log_record(net, first_time=True)
#self.log_record('+'*50, first_time=False)

def log_record(self, _str, first_time=None):
    dt = datetime.now()
    dt.strftime( '%Y-%m-%d %H:%M:%S' )
    if first_time:
        file_mode = 'w'
    else:
        file_mode = 'a+'
    f = open('./log/%s.txt'%(self.file_id), file_mode)
    f.write('[%s]-%s\n'%(dt, _str))
    f.flush()
    f.close()

def train(self, epoch):
    self.net.train()
    if epoch ==0: lr = 0.01
    if epoch > 0: lr = 0.1;
    if epoch > 148: lr = 0.01
    if epoch > 248: lr = 0.001
    optimizer = optim.SGD(self.net.parameters(), lr=lr, momentum = 0.9, weight_decay=5e-4)
    running_loss = 0.0
    total = 0
    correct = 0
    for _, data in enumerate(self.trainloader, 0):
        inputs, labels = data
        inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
        optimizer.zero_grad()
        outputs = self.net(inputs)
        loss = self.criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.data[0]*labels.size(0)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels.data).sum()
    self.log_record('Train-Epoch:%3d,  Loss: %.3f, Acc:%.3f'% (epoch+1, running_loss/total, (correct/total)))

def test(self, epoch):
    self.net.eval()
    test_loss = 0.0
    total = 0
    correct = 0
    for _, data in enumerate(self.validate_loader, 0):
        inputs, labels = data
        inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
        outputs = self.net(inputs)
        loss = self.criterion(outputs, labels)
        test_loss += loss.data[0]*labels.size(0)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels.data).sum()
    if correct/total > self.best_acc:
        self.best_acc = correct/total
        #print('*'*100, self.best_acc)
    self.log_record('Validate-Loss:%.3f, Acc:%.3f'%(test_loss/total, correct/total))


def process(self):
    total_epoch = StatusUpdateTool.get_epoch_size()
    for p in range(total_epoch):
        self.train(p)
        self.test(total_epoch)
    return self.best_acc

class RunModel(object):
@classmethod
def do_work(self, gpu_id, file_id):
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_id
best_acc = 0.0
try:
m = TrainModel()
m.log_record('Used GPU#%s, worker name:%s[%d]'%(gpu_id, multiprocessing.current_process().name, os.getpid()), first_time=True)
best_acc = m.process()
#import random
#best_acc = random.random()
except BaseException as e:
print('Exception occurs, file:%s, pid:%d...%s'%(file_id, os.getpid(), str(e)))
m.log_record('Exception occur:%s'%(str(e)))
finally:
m.log_record('Finished-Acc:%.3f'%best_acc)

        f = open('./populations/after_%s.txt'%(file_id[4:6]), 'a+')
        f.write('%s=%.5f\n'%(file_id, best_acc))
        f.flush()
        f.close()

if name == 'main':
RunModel.do_work(gpu_id='0', file_id='indi-gen00-no00')

@1234565556
Copy link

cifar.py中的loss.data[0]改成loss.item()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants