コード例 #1
0
def train(epoch):
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    m = math.ceil(len(trainset) / batch_size)
    optimizer = optim.Adam(net.parameters(),
                           lr=cf.learning_rate(args.lr, epoch),
                           weight_decay=args.weight_decay)

    print('\n=> Training Epoch #%d, LR=%.4f' %
          (epoch, cf.learning_rate(args.lr, epoch)))
    for batch_idx, (inputs_value, targets) in enumerate(trainloader):
        # repeat samples for
        x = inputs_value.view(-1, inputs, resize,
                              resize).repeat(args.num_samples, 1, 1, 1)
        print(x.shape)
        y = targets.repeat(args.num_samples)
        if use_cuda:
            x, y = x.cuda(), y.cuda()  # GPU settings

        if args.beta_type is "Blundell":
            beta = 2**(m - (batch_idx + 1)) / (2**m - 1)
        elif args.beta_type is "Soenderby":
            beta = min(epoch / (num_epochs // 4), 1)
        elif args.beta_type is "Standard":
            beta = 1 / m
        else:
            beta = 0
        # Forward Propagation
        x, y = Variable(x), Variable(y)
        outputs, kl = net.probforward(x)
        #print(outputs.shape)
        loss = vi(outputs, y, kl, beta)  # Loss
        optimizer.zero_grad()
        loss.backward()  # Backward Propagation
        optimizer.step()  # Optimizer update
        train_loss += loss.data
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(y.data).cpu().sum()

        sys.stdout.write('\r')
        sys.stdout.write(
            '| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tLoss: %.4f Acc@1: %.3f%%' %
            (epoch, num_epochs, batch_idx + 1,
             (len(trainset) // batch_size) + 1, loss.data,
             (100 * correct / total) / args.num_samples))
        sys.stdout.flush()

    #diagnostics_to_write = {'Epoch': epoch, 'Loss': loss.data[0], 'Accuracy': (100*correct/total)/args.num_samples}
    diagnostics_to_write = {
        'Epoch': epoch,
        'Loss': loss.data,
        'Accuracy': (100 * correct / total) / args.num_samples
    }
    with open(logfile, 'a') as lf:
        lf.write(str(diagnostics_to_write))
コード例 #2
0
def train(epoch):
    net.train()   # torch.nn.Module.train:torch.nn.Module.train:  Sets the module in training mode.
    train_loss = 0
    correct = 0
    total = 0
    m = math.ceil(len(trainset) / batch_size)
    optimizer = optim.Adam(net.parameters(), lr=cf.learning_rate(args.lr, epoch), weight_decay=args.weight_decay)

    print('\n=> Training Epoch #%d, LR=%.4f' %(epoch, cf.learning_rate(args.lr, epoch)))
    for batch_idx, (inputs_value, targets) in enumerate(trainloader):
        #print(input_value)
        #print(targets)
        x = inputs_value.view(-1, inputs, resize, resize).repeat(args.num_samples, 1, 1, 1)
        # after repeat, the first dimension of x becomes args.num_samples of the original size
        #x = inputs_value.repeat(args.num_samples, 1, 1, 1)
        #breakpoint()
        y = targets.repeat(args.num_samples)
        #y = targets.repeat(args.num_samples, 1)
        if use_cuda:
            x, y = x.cuda(), y.cuda() # GPU settings

        if args.beta_type is "Blundell":
            beta = 2 ** (m - (batch_idx + 1)) / (2 ** m - 1)
        elif args.beta_type is "Soenderby":
            beta = min(epoch / (num_epochs // 4), 1)
        elif args.beta_type is "Standard":
            beta = 1 / m
        else:
            beta = 0
        # Forward Propagation
        x, y = Variable(x), Variable(y)
        outputs, kl = net.probforward(x)    # prob.forward is not from torch.nn.Module
        # torch.nn.Module.forward: Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
        print(x.shape)
        print(outputs.shape)  # here is the bug
        print(kl.shape) # scalar shape should be empty here
        loss = vi(outputs, y, kl, beta)  # Loss, equivalent to calling vi.forward(outputs, y, kl, beta)
        optimizer.zero_grad()  # Clears the gradients of all optimized torch.Tensor s.
        loss.backward()  # Backward Propagation
        optimizer.step()  # Optimizer update
        train_loss += loss.data
        _, predicted = torch.max(outputs.data, dim = 1)  # Returns the maximum value of each row of the input tensor in the given dimension dim. The second return value is the index location of each maximum value found (argmax).
        total += targets.size(0)
        correct += predicted.eq(y.data).cpu().sum()

        sys.stdout.write('\r')
        sys.stdout.write('| Epoch [%3d/%3d] Iter[%3d/%3d]\t\tLoss: %.4f Acc@1: %.3f%%' %(epoch, num_epochs, batch_idx+1,
                    (len(trainset)//batch_size)+1, loss.data, (100*correct.to(dtype=torch.float)/float(total))/args.num_samples))
        sys.stdout.flush()

    #diagnostics_to_write = {'Epoch': epoch, 'Loss': loss.data[0], 'Accuracy': (100*correct.to(dtype=torch.float)/float(total))/args.num_samples}
    diagnostics_to_write = {'Epoch': epoch, 'Loss': loss.data, 'Accuracy': (100*correct.to(dtype=torch.float)/float(total))/args.num_samples}
    with open(logfile, 'a') as lf:
        lf.write(str(diagnostics_to_write))