def make_scorer(data, reference): return E.Scorer(data, reference=reference, depth_f=lambda e: e.depth(), value_f=lambda e: e.evaluate(10), kernel_f=lambda T1, T2=None, **kwargs : K.pairwise(T1, T2=T2, normalize=True, ignore_leaves=False, **kwargs), alphas = (1.0, 0.5), device='cuda')
def __init__(self, data, reference=None, depth_f=lambda e: e.depth(), value_f=lambda e: e.evaluate(10), kernel_f=lambda E, **kwargs: K.pairwise( E, normalize=True, ignore_leaves=True, **kwargs), alphas=(1.0, 0.5), device='cuda'): self.data = data self.alphas = alphas if reference is not None: self.reference = reference else: _, self.reference = A.generate_batch(p=1.0, decay=1.5, size=200) self.depth = torch.tensor([depth_f(e) for e in data], dtype=torch.float32, device=device) self.value = torch.tensor([value_f(e) for e in data], dtype=torch.float32, device=device) self.D_tree = { alpha: 1 - kernel_f(self.data, alpha=alpha).to(device=device) for alpha in self.alphas } self.D_depth = C.abs_diff(self.depth) self.D_value = C.abs_diff(self.value) self.tree_depth = { alpha: score(self.D_tree[alpha], self.D_depth) for alpha in self.alphas } self.tree_value = { alpha: score(self.D_tree[alpha], self.D_value) for alpha in self.alphas } self.depth_value = score(self.D_depth, self.D_value) self.emb_tree = { alpha: 1 - kernel_f(self.data, self.reference, alpha=alpha).detach().cpu().numpy() for alpha in self.alphas } self.emb_depth = C.abs_diff_( self.depth, torch.tensor([depth_f(e) for e in self.reference], dtype=torch.float32, device=device)).detach().cpu().numpy() self.emb_value = C.abs_diff_( self.value, torch.tensor([value_f(e) for e in self.reference], dtype=torch.float32, device=device)).detach().cpu().numpy()
N=1000, epochs=[10, 20, 40], decay=[2.0, 1.8, 1.5], size=128, depth=1, size_embed=64, n_classes=None) _, data = A.generate_batch(p=1.0, decay=config['decay'][-1], size=config['N']) # Eval data with target decay pickle.dump(data, open('data.pkl', 'wb')) scorer = E.Scorer( data, depth_f=lambda e: e.depth(), value_f=lambda e: e.evaluate(10), kernel_f=lambda E: K.pairwise(E, normalize=True, ignore_leaves=True), device='cuda') net = M.Net(M.Encoder(size_in=15, size=config['size'], depth=config['depth'], size_embed=config['size_embed']), n_classes=config['n_classes'], target_type='depth').cuda() for i, decay in enumerate(config['decay']): for epoch in range(0, config['epochs'][i] + 1): if epoch > 0: net.experiment(batch_size=config['batch_size'], n=config['n'], decay=decay, prefix="{} {}".format(epoch, decay)) pickle.dump(net, open('net-d{}-{:02d}.pkl'.format(decay, epoch), 'wb'))