def __init__(self, n=32, shape=(3, 3), pad='same', stride=(1, 1), init_fn='orthogonal', update_fn='nag'): self.n = n if isinstance(shape, int): shape = (shape, shape) self.shape = shape if pad != 'same': raise NotImplementedError('Only same pad supported right now!') self.pad = pad if isinstance(stride, int): stride = (stride, stride) if stride != (1, 1): raise NotImplementedError( 'Only (1, 1) stride supported right now!') self.stride = stride self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, activation, init_fn=inits.Constant(c=0.25), update_fn='nag'): self.activation = instantiate(activations, activation) self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, n=32, shape=(3, 3), pad='same', stride=(1, 1), init_fn='orthogonal', update_fn='nag'): self.n = n if isinstance(shape, int): shape = (shape, shape) self.shape = shape if isinstance(pad, int): pad = (pad, pad) elif pad == 'same': pad = (same_pad(shape[0]), same_pad(shape[1])) self.pad = pad if isinstance(stride, int): stride = (stride, stride) self.stride = stride self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, dim=256, activation='rectify', proj_init_fn='orthogonal', rec_init_fn='identity', bias_init_fn='constant', update_fn='nag'): self.dim = dim self.activation = instantiate(activations, activation) self.proj_init_fn = instantiate(inits, proj_init_fn) self.rec_init_fn = instantiate(inits, rec_init_fn) self.bias_init_fn = instantiate(inits, bias_init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, model, e, a=0.5, verbose=2, iterator='linear'): self.verbose = verbose self.model = init(model) try: self.iterator = instantiate(iterators, iterator) except: self.iterator = instantiate(async_iterators, iterator) 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 self.Y = T.TensorType(theano.config.floatX, (False, ) * (len(model[-1].out_shape)))() cost = T.nnet.categorical_crossentropy(y_tr, self.Y).mean() X_adv = self.X + e * T.sgn(T.grad(cost, self.X)) self.model[0].X = X_adv y_tr_adv = self.model[-1].op({ 'dropout': True, 'bn_active': True, 'infer': False }) cost_adv = a * cost + (1. - a) * T.nnet.categorical_crossentropy( y_tr_adv, self.Y).mean() te_cost = T.nnet.categorical_crossentropy(y_te, self.Y).mean() X_te_adv = self.X + e * T.sgn(T.grad(te_cost, self.X)) self.updates = collect_updates(self.model, cost_adv) self.infer_updates = collect_infer_updates(self.model) self.reset_updates = collect_reset_updates(self.model) self._train = theano.function([self.X, self.Y], cost_adv, updates=self.updates) self._predict = theano.function([self.X], y_te) self._fast_sign = theano.function([self.X, self.Y], X_te_adv) self._infer = theano.function([self.X], y_inf, updates=self.infer_updates) self._reset = theano.function([], updates=self.reset_updates)
def __init__(self, n=32, shape=(3, 3), pad='same', stride=(1, 1), init_fn='orthogonal', update_fn='nag'): self.n = n if isinstance(shape, int): shape = (shape, shape) self.shape = shape if pad != 'same': raise NotImplementedError('Only same pad supported right now!') self.pad = pad if isinstance(stride, int): stride = (stride, stride) if stride != (1, 1): raise NotImplementedError('Only (1, 1) stride supported right now!') self.stride = stride self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, model, cost=None, verbose=2, iterator='linear'): if cost is not None: self.cost = instantiate(costs, cost) else: if isinstance(model[-1], ops.Activation): if isinstance(model[-1].activation, activations.Sigmoid): self.cost = instantiate(costs, 'bce') elif isinstance(model[-1].activation, activations.Softmax): self.cost = instantiate(costs, 'cce') else: self.cost = instantiate(costs, 'mse') else: self.cost = instantiate(costs, 'mse') self.verbose = verbose self.model = init(model) try: self.iterator = instantiate(iterators, iterator) except: self.iterator = instantiate(async_iterators, iterator) 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 self.Y = T.TensorType(theano.config.floatX, (False, ) * (len(model[-1].out_shape)))() cost = self.cost(self.Y, y_tr) 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, self.Y], cost, updates=self.updates) self._predict = 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, e, a=0.5, verbose=2, iterator='linear'): self.verbose = verbose self.model = init(model) try: self.iterator = instantiate(iterators, iterator) except: self.iterator = instantiate(async_iterators, iterator) 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 self.Y = T.TensorType(theano.config.floatX, (False,)*(len(model[-1].out_shape)))() cost = T.nnet.categorical_crossentropy(y_tr, self.Y).mean() X_adv = self.X + e*T.sgn(T.grad(cost, self.X)) self.model[0].X = X_adv y_tr_adv = self.model[-1].op({'dropout':True, 'bn_active':True, 'infer':False}) cost_adv = a*cost + (1.-a)*T.nnet.categorical_crossentropy(y_tr_adv, self.Y).mean() te_cost = T.nnet.categorical_crossentropy(y_te, self.Y).mean() X_te_adv = self.X + e*T.sgn(T.grad(te_cost, self.X)) self.updates = collect_updates(self.model, cost_adv) self.infer_updates = collect_infer_updates(self.model) self.reset_updates = collect_reset_updates(self.model) self._train = theano.function([self.X, self.Y], cost_adv, updates=self.updates) self._predict = theano.function([self.X], y_te) self._fast_sign = theano.function([self.X, self.Y], X_te_adv) self._infer = theano.function([self.X], y_inf, updates=self.infer_updates) self._reset = theano.function([], updates=self.reset_updates)
def __init__(self, model, cost=None, verbose=2, iterator='linear'): if cost is not None: self.cost = instantiate(costs, cost) else: if isinstance(model[-1], ops.Activation): if isinstance(model[-1].activation, activations.Sigmoid): self.cost = instantiate(costs, 'bce') elif isinstance(model[-1].activation, activations.Softmax): self.cost = instantiate(costs, 'cce') else: self.cost = instantiate(costs, 'mse') else: self.cost = instantiate(costs, 'mse') self.verbose = verbose self.model = init(model) try: self.iterator = instantiate(iterators, iterator) except: self.iterator = instantiate(async_iterators, iterator) 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 self.Y = T.TensorType(theano.config.floatX, (False,)*(len(model[-1].out_shape)))() cost = self.cost(self.Y, y_tr) 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, self.Y], cost, updates=self.updates) self._predict = 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, dim=256, init_fn='orthogonal', update_fn='nag'): self.dim = dim self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, init_fn='constant', update_fn='nag'): self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, dim, n_embed, init_fn='uniform', update_fn='nag'): self.dim = dim self.n_embed = n_embed self.init_fn = instantiate(inits, init_fn) self.update_fn = instantiate(updates, update_fn)
def __init__(self, update_fn='nag', e=1e-8): self.update_fn = instantiate(updates, update_fn) self.e = e