Example #1
0
import numpy as np
import torch
from meta.matching import *
import dlutil as dl

shots = 2
ways = 5

G = Embedding(1, 10)
context_embedding_network = MatchingNetwork.build_context_embedding_network(10, 64, 1)
network = MatchingNetwork(G, context_embedding_network, ways).cuda(0)

optimizer = torch.optim.Adam(network.parameters())

ckpt = dl.Checkpoint('results/matching/omniglot1', max_to_keep=10, device=0, save_best_only=True, saving_metric='test_acc')
acc = dl.MetricAccuracy(name='acc', device=0)


root = '/data/examples/omniglot'
batch_size = 32
def trans(bxs, bys):
    bx = bxs[0]
    by = bys[0]
    bx = bx.astype(np.float32) / 255.
    bx = np.expand_dims(bx, axis=1)
    by = np.squeeze(by.astype(np.int64))
    classes = sorted(list(set(by.tolist())))
    for i, c in enumerate(classes):
        by[by==c] = i
    inp_x = bx[:ways]
    sup_x = bx[ways:]
Example #2
0
def input_trans(bxs, bys):
    bx, = bxs
    by, = bys
    bx = bx.astype(np.float32).transpose([2, 0, 1])
    by = by.astype(np.int64).squeeze()
    return ((bx, ), by)


num_rounds = 5
num_ops = 11
stem = nn.Sequential(ConvOp(3, 64, kernel_size=1), nn.ReLU())
arch = ArchBuilder(stem, 10, 64, [2, 2, 2], num_rounds=num_rounds).cuda(0)
optimizer = torch.optim.Adam(arch.parameters())
loss_func = nn.CrossEntropyLoss()
ckpt = dl.Checkpoint('temp/evo/e1', max_to_keep=10, device=0)
acc = dl.MetricAccuracy(device=0, name='acc')

batch_size = 32
ds = dl.DataReader('/data/testdata/cifar10/cifar10_test.h5',
                   transform_func=input_trans)
gntr = ds.common_cls_reader(batch_size, selected_classes=['tr'])
gnte = ds.common_cls_reader(batch_size * 3,
                            selected_classes=['te'],
                            shuffle=False)
listeners = [EvoListener('test', gnte, [acc])]
emodel = EvoModel(arch, ckpt, num_ops, num_rounds, device=0)
warmup_num_epochs = 10
emodel.warm_up(gntr,
               loss_func,
               optimizer,
Example #3
0
def input_trans(bxs, bys):
    bx, = bxs
    by, = bys
    bx = bx.astype(np.float32).transpose([2, 0, 1])
    by = by.astype(np.int64).squeeze()
    return ((bx, ), by)


num_rounds = 5
num_ops = 11
stem = nn.Sequential(ConvOp(3, 64, kernel_size=1), nn.ReLU())
arch = ArchBuilder(stem, 10, 64, [2, 2, 2], num_rounds=num_rounds).cuda(0)
wp_optimizer = torch.optim.Adam(arch.parameters())
optimizer = torch.optim.Adam(arch.parameters())
loss_func = nn.CrossEntropyLoss()
ckpt = dl.Checkpoint('temp/evo/e2', device=0)
acc = dl.MetricAccuracy(device=0, name='acc')

batch_size = 32
ds = dl.DataReader('/data/testdata/cifar10/cifar10.h5',
                   transform_func=input_trans)

gntr = ds.common_cls_reader(batch_size, selected_classes=['tr'], shuffle=True)
gnte = ds.common_cls_reader(batch_size * 3,
                            selected_classes=['te'],
                            shuffle=False)
warmup_listeners = [EvoListener('wp/test', gnte, [acc])]

warmup_num_epochs = 10
num_pop = 200
evo_cycles = 200
optimizer = torch.optim.Adam(network.parameters())

class CustomSchedule(torch.optim.lr_scheduler._LRScheduler):
    def __init__(self, optimizer, d_model, warmup_steps=4000, last_epoch=-1):
        self.d_model = d_model
        self.warmup_steps = warmup_steps
        super(CustomSchedule, self).__init__(optimizer, last_epoch)

    def get_lr(self):
        arg1 = 1. / (np.sqrt(self.last_epoch) + 1e-7)
        arg2 = self.last_epoch * (self.warmup_steps ** -1.5)
        lr = 1. / np.sqrt(self.d_model) * np.minimum(arg1, arg2)
        return [lr for _ in self.base_lrs]
scheduler = CustomSchedule(optimizer, d_model, warmup_steps=100)
scheduler = None
ckpt = dl.Checkpoint('results/pt2en_transformer', max_to_keep=10, save_best_only=True, saving_metric='test_acc', device=0)
acc = dl.MetricAccuracy(name='acc', device=0)
listeners = [dl.Listener('test', gntv, [acc])]

def loss_func(predictions, labels):
    batch_size = predictions.size(0)
    loss_ = nn.CrossEntropyLoss(reduction='none')(predictions.transpose(-1, -2), labels)
    mask = (labels != 0).reshape((batch_size, -1)).type(loss_.dtype)
    loss_ *= mask
    return torch.mean(loss_)

dlmodel = dl.DlModel(network, ckpt)
dlmodel.train(gntr, loss_func, optimizer, num_epochs=100, metrics=[acc], listeners=listeners, from_scratch=True, scheduler=scheduler)

Example #5
0
    return (bx, ), by


ds = dl.DataReader('../data/mnist/mnist.h5',
                   transform_func=tran_func,
                   num_workers=0)

gntr = ds.common_cls_reader(32, selected_classes=['train'], shuffle=True)
gntv = ds.common_cls_reader(32, selected_classes=['valid'], shuffle=False)
gnte = ds.common_cls_reader(32, selected_classes=['test'], shuffle=False)

optimizer = torch.optim.Adam(builder.parameters())
loss_func = nn.CrossEntropyLoss()
ckpt = dl.Checkpoint('temp/enas_t1/builder',
                     max_to_keep=10,
                     device=0,
                     save_best_only=True,
                     saving_metric='test_acc')
acc = dl.MetricAccuracy(device=0, name='acc')
total_steps = 20000
ckpt_steps = 1000
summ_steps = 50
listeners = [EnasListener('test', gnte, [acc])]

sampler_ckpt = dl.Checkpoint('temp/enas_t1/sampler', max_to_keep=10, device=0)
sampler_optim = torch.optim.Adam(sampler.parameters())
steps_before_training_sampler = 2000
sampler_training_interval = 1000
sampler_total_steps = 100
sampler_summ_steps = 10