예제 #1
0
    def __init__(self, pickle_model="", datadir=None):
        self.maxlen = 100
        self.n_words = 100000
        parser = NeonArgparser(__doc__)
        self.args = parser.parse_args()
        self.args.batch_size = self.batch_size = 2048  #
        self.args.deterministic = None
        self.args.rng_seed = 0
        print extract_valid_args(self.args, gen_backend)
        self.be = gen_backend(**extract_valid_args(self.args, gen_backend))

        embedding_dim = 100
        init_emb = Uniform(-0.1 / embedding_dim, 0.1 / embedding_dim)
        init_glorot = GlorotUniform()
        self.layers = [
            LookupTable(vocab_size=self.n_words,
                        embedding_dim=embedding_dim,
                        init=init_emb,
                        pad_idx=0,
                        update=True,
                        name="LookupTable"),
            Dropout(keep=0.5),
            BiLSTM(100,
                   init=init_glorot,
                   activation=Tanh(),
                   gate_activation=Logistic(),
                   reset_cells=True,
                   split_inputs=False,
                   name="BiLSTM"),
            RecurrentMean(),
            Affine(1,
                   init_glorot,
                   bias=init_glorot,
                   activation=Identity(),
                   name="Affine")
        ]

        self.wordbatch = wordbatch.WordBatch(normalize_text,
                                             n_words=self.n_words,
                                             extractors=[(wordbatch.WordSeq, {
                                                 "seq_maxlen":
                                                 self.maxlen
                                             })])

        if datadir == None:
            self.model = Model(self.layers)
            self.model.load_params(pickle_model)
            self.wordbatch = pkl.load(gzip.open(pickle_model + ".wb", 'rb'))
        else:
            self.train(datadir, pickle_model)
예제 #2
0
    def initialize_neon():
        # for now, we don't trust the mkl backend
        if Config.options.backend == 'mkl':
            print('Resetting mkl backend to cpu')
            Config.options.backend = 'cpu'

        be = gen_backend(**extract_valid_args(Config.options, gen_backend))
        # patch a fix to stabilize the CPU version of Neon's logistic function
        fix_logistic(be)
        Config.initialized = True
예제 #3
0
def main():
    # Collect the user arguments and hyper parameters
    args, hyper_params = get_args_and_hyperparameters()

    np.set_printoptions(precision=8,
                        suppress=True,
                        edgeitems=6,
                        threshold=2048)

    # setup the CPU or GPU backend
    be = gen_backend(**extract_valid_args(args, gen_backend))

    # load the training dataset. This will download the dataset from the web and cache it
    # locally for subsequent use.
    train_set = MultiscaleSampler(
        'trainval',
        '2007',
        samples_per_img=hyper_params.samples_per_img,
        sample_height=224,
        path=args.data_dir,
        samples_per_batch=hyper_params.samples_per_batch,
        max_imgs=hyper_params.max_train_imgs,
        shuffle=hyper_params.shuffle)

    # create the model by replacing the classification layer of AlexNet with
    # new adaptation layers
    model, opt = create_model(args, hyper_params)

    # Seed the Alexnet conv layers with pre-trained weights
    if args.model_file is None and hyper_params.use_pre_trained_weights:
        load_imagenet_weights(model, args.data_dir)

    train(args, hyper_params, model, opt, train_set)

    # Load the test dataset. This will download the dataset from the web and cache it
    # locally for subsequent use.
    test_set = MultiscaleSampler(
        'test',
        '2007',
        samples_per_img=hyper_params.samples_per_img,
        sample_height=224,
        path=args.data_dir,
        samples_per_batch=hyper_params.samples_per_batch,
        max_imgs=hyper_params.max_test_imgs,
        shuffle=hyper_params.shuffle)
    test(args, hyper_params, model, test_set)

    return
예제 #4
0
def main():
    # Collect the user arguments and hyper parameters
    args, hyper_params = get_args_and_hyperparameters()
    
    np.set_printoptions( precision=8, suppress=True, edgeitems=6, threshold=2048)
    
    # setup the CPU or GPU backend
    be = gen_backend(**extract_valid_args(args, gen_backend))
    
    # load the training dataset. This will download the dataset from the web and cache it
    # locally for subsequent use.
    train_set = MultiscaleSampler('trainval', '2007', samples_per_img=hyper_params.samples_per_img, 
                                 sample_height=224, path=args.data_dir, 
                                 samples_per_batch=hyper_params.samples_per_batch,
                                 max_imgs = hyper_params.max_train_imgs,
                                 shuffle = hyper_params.shuffle)

    # create the model by replacing the classification layer of AlexNet with 
    # new adaptation layers
    model, opt = create_model( args, hyper_params)
    
    # Seed the Alexnet conv layers with pre-trained weights
    if args.model_file is None and hyper_params.use_pre_trained_weights:
        load_imagenet_weights(model, args.data_dir)
    
    train( args, hyper_params, model, opt, train_set)
    
    # Load the test dataset. This will download the dataset from the web and cache it
    # locally for subsequent use.
    test_set = MultiscaleSampler('test', '2007', samples_per_img=hyper_params.samples_per_img, 
                                 sample_height=224, path=args.data_dir, 
                                 samples_per_batch=hyper_params.samples_per_batch,
                                 max_imgs = hyper_params.max_test_imgs,
                                 shuffle = hyper_params.shuffle)
    test( args, hyper_params, model, test_set)
    
    return
예제 #5
0
args = parser.parse_args()


# hyperparameters
hidden_size = 128
embedding_dim = 128
vocab_size = 20000
sentence_length = 128
batch_size = 32
gradient_limit = 5
clip_gradients = True
num_epochs = args.epochs
embedding_update = True

# setup backend
be = gen_backend(**extract_valid_args(args, gen_backend))

# get the preprocessed and tokenized data
fname_h5, fname_vocab = build_data_train(filepath=args.review_file,
                                         vocab_file=args.vocab_file, skip_headers=True)


# play around with google-news word vectors for init
if args.use_w2v:
    w2v_file = args.w2v
    vocab, rev_vocab = cPickle.load(open(fname_vocab, 'rb'))
    init_emb, embedding_dim, _ = get_google_word2vec_W(w2v_file, vocab,
                                                       vocab_size=vocab_size, index_from=3)
    print "Done loading the Word2Vec vectors: embedding size - {}".format(embedding_dim)
    embedding_update = True
else:
예제 #6
0
    #           through the data
    return_sequences = False

    # Note that when the time series has higher or lower frequency, it requires different amounts
    # of data to learn the temporal pattern, the sequence length and the batch size for the
    # training process also makes a difference on learning performance.

    seq_len = 30
    npoints = 10
    ncycles = 100
    num_predict = 200
    seed_seq_len = 30

    # ================= Main neon script ====================

    be = gen_backend(**extract_valid_args(args, gen_backend))

    # a file to save the trained model
    if args.save_path is None:
        args.save_path = 'timeseries.pkl'

    if args.callback_args['save_path'] is None:
        args.callback_args['save_path'] = args.save_path

    if args.callback_args['serialize'] is None:
        args.callback_args['serialize'] = 1

    # create synthetic data as a whole series
    time_series = TimeSeries(npoints,
                             ncycles=ncycles,
                             curvetype=args.curvetype)
예제 #7
0
def main():
    parser = NeonArgparser(__doc__)
    args = parser.parse_args(gen_be=False)

    #mat_data = sio.loadmat('../data/timeseries/02_timeseries.mat')

    #ts = V1TimeSeries(mat_data['timeseries'], mat_data['stim'], binning=10)

    seq_len = 30
    hidden = 20

    be = gen_backend(**extract_valid_args(args, gen_backend))

    kohn = KohnV1Dataset(path='../tmp/')
    kohn.gen_iterators(seq_len)
    import pdb; pdb.set_trace()
    train_spike_set = V1IteratorSequence(ts.train, seq_len, return_sequences=False)
    valid_spike_set = V1IteratorSequence(ts.test, seq_len, return_sequences=False)

    init = GlorotUniform()

    # dataset = MNIST(path=args.data_dir)
    # (X_train, y_train), (X_test, y_test), nclass = dataset.load_data()
    # train_set = ArrayIterator([X_train, X_train], y_train, nclass=nclass, lshape=(1, 28, 28))
    # valid_set = ArrayIterator([X_test, X_test], y_test, nclass=nclass, lshape=(1, 28, 28))

    # # weight initialization
    # init_norm = Gaussian(loc=0.0, scale=0.01)

    # # initialize model
    # path1 = Sequential(layers=[Affine(nout=100, init=init_norm, activation=Rectlin()),
    #                            Affine(nout=100, init=init_norm, activation=Rectlin())])

    # path2 = Sequential(layers=[Affine(nout=100, init=init_norm, activation=Rectlin()),
    #                            Affine(nout=100, init=init_norm, activation=Rectlin())])

    # layers = [MergeMultistream(layers=[path1, path2], merge="stack"),
    #           Affine(nout=10, init=init_norm, activation=Logistic(shortcut=True))]

    spike_rnn_path = Sequential( layers = [

        LSTM(hidden, init, activation=Logistic(),
            gate_activation=Logistic(), reset_cells=False),

        Dropout(keep=0.5),

         LSTM(hidden, init, activation=Logistic(),
             gate_activation=Logistic(), reset_cells=False),

        #Dropout(keep=0.85),

        RecurrentLast(),

        Affine(train_set.nfeatures, init, bias=init, activation=Identity(), name='spike_in')])

    stim_rnn_path = Sequential( layers = [

        LSTM(hidden, init, activation=Logistic(),
            gate_activation=Logistic(), reset_cells=False),

        Dropout(keep=0.5),

        RecurrentLast(),
        Affine(1, init, bias=init, activation=Identity(), name='stim')])

    layers = [
            MergeMultiStream(
                layers = [
                    spike_rnn_path,
                    stim_rnn_path],
                merge="stack"),

            Affine(train_set.nfeatures, init, bias=init, activation=Identity(), name='spike_out'),

            Round()
            ]

    model = Model(layers=layers)

    sched = ExpSchedule(decay=0.7)

    # cost = GeneralizedCost(SumSquared())
    cost = GeneralizedCost(MeanSquared())

    optimizer_two = RMSProp(stochastic_round=args.rounding)
    optimizer_one = GradientDescentMomentum(learning_rate=0.1, momentum_coef=0.9, schedule=sched)

    opt = MultiOptimizer({'default': optimizer_one,
                          'Bias': optimizer_two,
                          'special_linear': optimizer_two})

    callbacks = Callbacks(model, eval_set=valid_set, **args.callback_args)
    callbacks.add_hist_callback(filter_key = ['W'])
    #callbacks.add_callback(MetricCallback(eval_set=valid_set, metric=FractionExplainedVariance(), epoch_freq=args.eval_freq))
    #callbacks.add_callback(MetricCallback(eval_set=valid_set,metric=Accuracy(),  epoch_freq=args.eval_freq))

    model.fit(train_set,
              optimizer=opt,
              num_epochs=args.epochs,
              cost=cost,
              callbacks=callbacks)

    train_output = model.get_outputs(
    train_set).reshape(-1, train_set.nfeatures)
    valid_output = model.get_outputs(
    valid_set).reshape(-1, valid_set.nfeatures)
    train_target = train_set.y_series
    valid_target = valid_set.y_series

    tfev = fev(train_output, train_target, train_set.mean)
    vfev = fev(valid_output, valid_target, valid_set.mean)

    neon_logger.display('Train FEV: %g, Valid FEV:  %g' % (tfev, vfev))
    # neon_logger.display('Train Mean: %g, Valid Mean:  %g' % (train_set.mean, valid_set.mean))

    plt.figure()
    plt.plot(train_output[:, 0], train_output[
        :, 1], 'bo', label='prediction')
    plt.plot(train_target[:, 0], train_target[:, 1], 'r.', label='target')
    plt.legend()
    plt.title('Neon on training set')
    plt.savefig('neon_series_training_output.png')

    plt.figure()
    plt.plot(valid_output[:, 0], valid_output[
        :, 1], 'bo', label='prediction')
    plt.plot(valid_target[:, 0], valid_target[:, 1], 'r.', label='target')
    plt.legend()
    plt.title('Neon on validation set')
    plt.savefig('neon_series_validation_output.png')
예제 #8
0
)
parser.add_argument('--plot_log',
                    action="store_true",
                    help='Plot weights on log scale')
parser.add_argument('--plot_save_path',
                    type=str,
                    default='',
                    help='Path to save weight plots instead of displaying')

# parse the command line arguments (generates the backend)
args = parser.parse_args(gen_be=False)
print('emneon / neon options:')
print(args)

# setup backend
be_args = extract_valid_args(args, gen_backend)
# mutiple gpus accessing the cache dir for autotuning winograd was causing crashes / reboots
#be_args['cache_dir'] = tempfile.mkdtemp()  # create temp dir
be_args['deterministic'] = None  # xxx - why was this set?
be = gen_backend(**be_args)

# xxx - this doesn't work, interrupt is caught by neon for saving the model which then raises KeyboardInterrupt
#def signal_handler(signal, frame):
#    #print('You pressed Ctrl+C!')
#    shutil.rmtree(be_args['cache_dir'])  # delete directory
#signal.signal(signal.SIGINT, signal_handler)


# this function modified from cuda-convnets2 shownet.py
def make_filter_fig(filters,
                    filter_start,
예제 #9
0
파일: emneon.py 프로젝트: elhuhdron/emdrp
                    help='How many backend buffers to use, 1 for no double buffering (saves gpu memory, slower)')
parser.add_argument('--plot_weight_layer', type=int, default=-1, 
                    help='Plot weights for specified layer (must specify model_file)')
parser.add_argument('--plot_norm_per_filter', action="store_true",
                    help='With plotting weights, normalize each filter over range')
parser.add_argument('--plot_combine_chans', action="store_true",
                    help='With plotting weights, make plot that combines first three channels into colors')
parser.add_argument('--plot_log', action="store_true", help='Plot weights on log scale')
parser.add_argument('--plot_save_path', type=str, default='', help='Path to save weight plots instead of displaying')

# parse the command line arguments (generates the backend)
args = parser.parse_args(gen_be=False)
print('emneon / neon options:'); print(args)

# setup backend
be_args = extract_valid_args(args, gen_backend)
# mutiple gpus accessing the cache dir for autotuning winograd was causing crashes / reboots
#be_args['cache_dir'] = tempfile.mkdtemp()  # create temp dir
be_args['deterministic'] = None  # xxx - why was this set?
be = gen_backend(**be_args)

# xxx - this doesn't work, interrupt is caught by neon for saving the model which then raises KeyboardInterrupt
#def signal_handler(signal, frame):
#    #print('You pressed Ctrl+C!')
#    shutil.rmtree(be_args['cache_dir'])  # delete directory
#signal.signal(signal.SIGINT, signal_handler)

# this function modified from cuda-convnets2 shownet.py
def make_filter_fig(filters, filter_start, fignum, _title, num_filters, combine_chans, FILTERS_PER_ROW=None,
                    plot_border=0.0):
    MAX_ROWS = 24