def __init__(self, model, iterator, alpha=0.2, verbose=2): self.model = init(model) self.iterator = iterator self.alpha = alpha self.verbose = verbose y_tr = self.model[-1].op({'dropout':True, 'bn_active':True, 'infer':False}) y_te = self.model[-1].op({'dropout':False, 'bn_active':False, 'infer':False}) y_inf = self.model[-1].op({'dropout':False, 'bn_active':True, 'infer':True}) self.X = self.model[0].X anchor = y_tr[::3] pos = y_tr[1::3] neg = y_tr[2::3] dpos = pair_euclidean(anchor, pos) dneg = pair_euclidean(anchor, neg) d = dneg - dpos cost = T.maximum((1. - self.alpha) - d, 0.).mean() self.updates = collect_updates(self.model, cost) self.infer_updates = collect_infer_updates(self.model) self.reset_updates = collect_reset_updates(self.model) self._train = theano.function([self.X], cost, updates=self.updates) self._transform = theano.function([self.X], y_te) self._infer = theano.function([self.X], y_inf, updates=self.infer_updates) self._reset = theano.function([], updates=self.reset_updates)
def __init__(self, model, iterator, alpha=0.2, verbose=2): self.model = init(model) self.iterator = iterator self.alpha = alpha self.verbose = verbose y_tr = self.model[-1].op({ 'dropout': True, 'bn_active': True, 'infer': False }) y_te = self.model[-1].op({ 'dropout': False, 'bn_active': False, 'infer': False }) y_inf = self.model[-1].op({ 'dropout': False, 'bn_active': True, 'infer': True }) self.X = self.model[0].X anchor = y_tr[::3] pos = y_tr[1::3] neg = y_tr[2::3] dpos = pair_euclidean(anchor, pos) dneg = pair_euclidean(anchor, neg) d = dneg - dpos cost = T.maximum((1. - self.alpha) - d, 0.).mean() self.updates = collect_updates(self.model, cost) self.infer_updates = collect_infer_updates(self.model) self.reset_updates = collect_reset_updates(self.model) self._train = theano.function([self.X], cost, updates=self.updates) self._transform = theano.function([self.X], y_te) self._infer = theano.function([self.X], y_inf, updates=self.infer_updates) self._reset = theano.function([], updates=self.reset_updates)
def PairEuclidean(y_true, y_pred): return pair_euclidean(y_pred, y_true).mean()