Пример #1
0
 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)
Пример #2
0
 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)
Пример #3
0
    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)
Пример #4
0
def __instantiate_exec(target, source, env):
    varlist = env['varlist']

    missing = [key for key in varlist if not env.has_key(key)]
    if missing:
        print >> stderr, 'error: missing variables for template instantiation:', ', '.join(
            missing)
        Exit(1)

    keywords = dict((key, env.subst('$' + key)) for key in varlist)
    instantiate(str(source[0]), str(target[0]), **keywords)
Пример #5
0
 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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
    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)
Пример #10
0
    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)
Пример #11
0
    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)
Пример #12
0
    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)
Пример #13
0
 def __init__(self, dim=256, init_fn='orthogonal', update_fn='nag'):
     self.dim = dim
     self.init_fn = utils.instantiate(inits, init_fn)
     self.update_fn = utils.instantiate(updates, update_fn)
Пример #14
0
 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)
Пример #15
0
 def __init__(self, update_fn='nag', e=1e-8):
     self.update_fn = utils.instantiate(updates, update_fn)
     self.e = e
Пример #16
0
 def __init__(self, init_fn='constant', update_fn='nag'):
     self.init_fn = utils.instantiate(inits, init_fn)
     self.update_fn = utils.instantiate(updates, update_fn)
Пример #17
0
 def __init__(self, activation, init_fn=inits.Constant(c=0.25), update_fn='nag'):
     self.activation = utils.instantiate(activations, activation)
     self.init_fn = utils.instantiate(inits, init_fn)
     self.update_fn = utils.instantiate(updates, update_fn)
Пример #18
0
 def __init__(self, dim, n_embed, init_fn='uniform', update_fn='nag'):
     self.dim = dim
     self.n_embed = n_embed
     self.init_fn = utils.instantiate(inits, init_fn)
     self.update_fn = utils.instantiate(updates, update_fn)
Пример #19
0
 def __init__(self, init_fn='normal', update_fn='nag'):
     self.init_fn = instantiate(inits, init_fn)
     self.update_fn = instantiate(updates, update_fn)
Пример #20
0
 def __init__(self, init_fn="constant", update_fn="nag"):
     self.init_fn = instantiate(inits, init_fn)
     self.update_fn = instantiate(updates, update_fn)
Пример #21
0
 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)
Пример #22
0
def run_for_single_file(path, args, config):
    logging.info('Opening: {0}'.format(path))

    # Read the time-series from file
    raw_ts = read_file(path, args)
    if raw_ts.ndim == 1:
        raw_ts = np.expand_dims(raw_ts, axis=-1)

    epochs = config['epochs']
    batch_size = config['batch_size']
    scaler = utils.instantiate(config['scaler'])

    data = processing.TimeSeries(
        raw_ts,
        window=config['window_size'],
        window_stride=config['window_stride'],
        return_sequences=config['return_sequences'],
        train_test_split=config['train_test_split'],
        num_derivatives=config['num_derivatives'],
        deepcast_derivatives=config['deepcast_derivatives'],
        scaler=scaler,
        use_time_diff=False)

    model = utils.instantiate(config['model'], data.input_shape,
                              **config['model_args'])  # type: keras.Model
    model.summary()

    all_len = batch_size * (len(data.x) // batch_size)
    train_len = batch_size * (len(data.x_train) // batch_size)
    test_len = batch_size * (len(data.x_test) // batch_size)

    early_stopping = keras.callbacks.EarlyStopping(monitor='val_loss',
                                                   min_delta=0,
                                                   patience=20,
                                                   verbose=0,
                                                   mode='auto')
    history = model.fit(data.x_train[:train_len],
                        data.y_train[:train_len],
                        batch_size=batch_size,
                        epochs=epochs,
                        shuffle=True,
                        verbose=2,
                        callbacks=[early_stopping],
                        validation_data=(data.x_test[:test_len],
                                         data.y_test[:test_len]))

    if args.weights_out:
        model.save_weights(args.weights_out)

    # Evaluation
    y_true = data.y_test[:test_len]
    y_pred = model.predict(data.x_test[:test_len], batch_size=batch_size)
    y_true = data.inverse_transform_predictions(data.t_test[:test_len],
                                                y_true)[:, 0]
    y_pred = data.inverse_transform_predictions(data.t_test[:test_len],
                                                y_pred)[:, 0]

    scores = metrics.evaluate(y_true, y_pred, metrics=config['metrics'])
    logging.info('Scores on test data: {0}'.format(scores))

    if not args.noplot:
        output_dir = os.path.dirname(args.output)
        filename = '{0}_{1}.csv'.format(
            args.run,
            os.path.splitext(os.path.basename(path))[0])
        plot_path = os.path.join(output_dir, filename)

        # Prediction
        y_true = data.y[:all_len]
        y_pred = model.predict(data.x[:all_len], batch_size=batch_size)

        y_true = data.inverse_transform_predictions(data.t[:all_len],
                                                    y_true)[:, 0]
        y_pred = data.inverse_transform_predictions(data.t[:all_len],
                                                    y_pred)[:, 0]

        data = np.vstack((y_true, y_pred)).T
        np.savetxt(plot_path,
                   data,
                   delimiter=',',
                   header='True, Predicted',
                   comments='')
    else:
        plot_path = ''

    if args.output:
        results = config.copy()
        results.update({
            'file': path,
            'run': args.run,
            'plot': plot_path,
            'finished': datetime.datetime.now(),
            'epochs_trained': history.epoch[-1],
        })
        results.update(scores)
        utils.write_row_to_csv(args.output, **results)

    keras.backend.clear_session()
Пример #23
0
 def __init__(self, update_fn='nag', e=1e-8):
     self.update_fn = instantiate(updates, update_fn)
     self.e = e
Пример #24
0
 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)
Пример #25
0
 def __init__(self, init_fn='constant', update_fn='nag'):
     self.init_fn = instantiate(inits, init_fn)
     self.update_fn = instantiate(updates, update_fn)
Пример #26
0
 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)
Пример #27
0
        help=
        "Optional, name of the file in --model_dir containing weights to reload before \
                        training")  # 'best' or 'train'
    args = parser.parse_args()

    np.random.seed(42)
    torch.random.manual_seed(42)
    yaml_path = os.path.join(args.model_dir, 'params.yml')
    assert os.path.isfile(
        yaml_path), "No yaml configuration file found at {}".format(yaml_path)
    params = utils.Params(yaml_path)

    # Instantiate algorithm
    algorithm_module, algorithm_name = params.algorithm[
        'module'], params.algorithm['name']
    algorithm = instantiate(algorithm_module, algorithm_name)
    algorithm_params = params.algorithm['params']

    # Instantiate model
    model_module, model_name = params.model['module'], params.model['name']
    model = instantiate(model_module, model_name)
    model_params = params.model['params']

    # Instantiate optimizer
    optimizer_module, optimizer_name = params.optimizer[
        'module'], params.optimizer['name']
    optimizer = instantiate(optimizer_module, optimizer_name)

    # Instantiate dataset
    dataset_module, dataset_name = params.dataset['module'], params.dataset[
        'name']