Ejemplo n.º 1
0
def get_representations_for_joint_layer(yaml_file_path, save_path, batch_size):
    """
    The purpose of doing this is to test the compatibility of DBM with StackBlocks and TransformerDatasets
    Of course one can instead take use the "get_represenations.py" for data preparation for the next step.
    """
    
    hyper_params = {'save_path':save_path}
    yaml = open("{0}/stacked_image_unimodaliy.yaml".format(yaml_file_path), 'r').read()
    yaml = yaml % (hyper_params)
    image_stacked_blocks = yaml_parse.load(yaml)
    yaml = open("{0}/stacked_text_unimodaliy.yaml".format(yaml_file_path), 'r').read()
    yaml = yaml % (hyper_params)
    text_stacked_blocks = yaml_parse.load(yaml)
      
    image_raw = Flickr_Image_Toronto(which_cat = 'unlabelled',which_sub='nnz', using_statisfile = True)
    image_rep = TransformerDataset( raw = image_raw, transformer = image_stacked_blocks )
    m, n = image_raw.get_data().shape()
    dw = data_writer.DataWriter(['image_h2_rep'], save_path + 'image/', '10G', [n], m)
    image_iterator = image_rep.iterator(batch_size= batch_size)
    
    for data in image_iterator:
        dw.Submit(data)
    dw.Commit()
    text_raw = Flickr_Text_Toronto(which_cat='unlabelled')
    text_rep = TransformerDataset( raw = text_raw, transformer = text_stacked_blocks )
    m, n = text_raw.get_data().shape()
    dw = data_writer.DataWriter(['text_h2_rep'], save_path + 'text/', '10G', [n], m)
    text_iterator = text_rep.iterator(batch_size= batch_size)
    
    for data in text_iterator:
        dw.Submit(data)
    dw.Commit()
Ejemplo n.º 2
0
def test_duplicate_keywords():
    """
    Tests whether there are doublicate keywords in the yaml
    """
    initialize()
    yamlfile = """{
            "model": !obj:pylearn2.models.mlp.MLP {
            "layers": [
                     !obj:pylearn2.models.mlp.Sigmoid {
                         "layer_name": 'h0',
                         "dim": 20,
                         "sparse_init": 15,
                     }],
            "nvis": 784,
            "nvis": 384,
        }
    }"""

    try:
        load(yamlfile)
    except yaml.constructor.ConstructorError as e:
        message = str(e)
        assert message.endswith("found duplicate key (nvis)")
        pass
    except Exception as e:
        error_msg = "Got the unexpected error: %s" % (e)
        raise TypeError(error_msg)
Ejemplo n.º 3
0
def main(args=None):
    """
    Execute the main body of the script.

    Parameters
    ----------
    args : list, optional
        Command-line arguments. If unspecified, `sys.argv[1:]` is used.
    """
    parser = argparse.ArgumentParser(description='Load a YAML file without '
                                                 'performing any training.')
    parser.add_argument('yaml_file', type=argparse.FileType('r'),
                        help='The YAML file to load.')
    parser.add_argument('-N', '--no-instantiate',
                        action='store_const', default=False, const=True,
                        help='Only verify that the YAML parses correctly '
                             'but do not attempt to instantiate the objects. '
                             'This might be used as a quick sanity check if '
                             'checking a file that requires a GPU in an '
                             'environment that lacks one (e.g. a cluster '
                             'head node)')
    args = parser.parse_args(args=args)
    name = args.yaml_file.name
    initialize()
    if args.no_instantiate:
        yaml_load(args.yaml_file)
        print("Successfully parsed %s (but objects not instantiated)." % name)
    else:
        load(args.yaml_file)
        print("Successfully parsed and loaded %s." % name)
def test_which_set():
    """Test which_set selector."""
    skip_if_no_sklearn()

    # one label
    this_yaml = test_yaml_which_set % {'which_set': 'train'}
    trainer = yaml_parse.load(this_yaml)
    trainer.main_loop()

    # multiple labels
    this_yaml = test_yaml_which_set % {'which_set': ['train', 'test']}
    trainer = yaml_parse.load(this_yaml)
    trainer.main_loop()

    # improper label (iterator only returns 'train' and 'test' subsets)
    this_yaml = test_yaml_which_set % {'which_set': 'valid'}
    try:
        trainer = yaml_parse.load(this_yaml)
        trainer.main_loop()
        raise AssertionError
    except ValueError:
        pass

    # bogus label (not in approved list)
    this_yaml = test_yaml_which_set % {'which_set': 'bogus'}
    try:
        yaml_parse.load(this_yaml)
        raise AssertionError
    except ValueError:
        pass
Ejemplo n.º 5
0
def main(argv):
  
  try:
    opts, args = getopt.getopt(argv, '')
    student_yaml = args[0]
  except getopt.GetoptError:
    usage()
    sys.exit(2) 
  
  #
  # TRAIN WITH TARGETS
  #

  # Load student
  with open(student_yaml, "r") as sty:
    student = yaml_parse.load(sty)
    
  # Remove teacher decay over epoch if there is one
  for ext in range(len(student.extensions)):
    if isinstance(student.extensions[ext],TeacherDecayOverEpoch):
      del student.extensions[ext]
  
  student.algorithm.cost = MethodCost(method='cost_from_X')

  # Change save paths
  for ext in range(len(student.extensions)):
    if isinstance(student.extensions[ext],MonitorBasedSaveBest):
      student.extensions[ext].save_path = student.save_path[0:-4] + "_noteacher_best.pkl"
  student.save_path = student.save_path[0:-4] + "_noteacher.pkl" 
  
  student.main_loop()
  
  #
  # TRAIN WITH TEACHER (TOP LAYER)
  #

  # Load student
  with open(student_yaml, "r") as sty:
    student = yaml_parse.load(sty)
    
  # Change save paths
  for ext in range(len(student.extensions)):
    if isinstance(student.extensions[ext],MonitorBasedSaveBest):
      student.extensions[ext].save_path = student.save_path[0:-4] + "_toplayer_best.pkl"
  student.save_path = student.save_path[0:-4] + "_toplayer.pkl" 
  
  student.main_loop()
  
  
  #
  # TRAIN WITH HINTS
  #
    
  hints.main([student_yaml, 'conv'])
Ejemplo n.º 6
0
def test_multi_constructor_obj():
    """
    Tests whether multi_constructor_obj throws an exception when
    the keys in mapping are None.
    """
    try:
        load("a: !obj:decimal.Decimal { 1 }")
    except TypeError as e:
        assert str(e) == "Received non string object (1) as key in mapping."
        pass
    except Exception as e:
        error_msg = "Got the unexpected error: %s" % (e)
        reraise_as(ValueError(error_msg))
def get_dataset_timitVowels20ms9Frames_MFCC():
    print('loading timitVowels20ms9Frames_MFCC dataset...')

    template = \
        """!obj:pylearn2.datasets.timitVowels20ms9Frames_MFCC.timit.TIMIT {
classes_number: 32,
which_set: %s,
}"""
    trainset = yaml_parse.load(template % "train")
    validset = yaml_parse.load(template % "valid")
    # testset = yaml_parse.load(template % "test")

    print('...done loading timitVowels20ms9Frames_MFCC.')

    return trainset, validset
Ejemplo n.º 8
0
def test_parse_null_as_none():
    """
    Tests whether None may be passed via yaml kwarg null.
    """
    initialize()
    yamlfile = """{
             "model": !obj:pylearn2.models.autoencoder.Autoencoder {

                 "nvis" : 1024,
                 "nhid" : 64,
                 "act_enc" : Null,
                 "act_dec" : null

             }
    }"""
    load(yamlfile)
Ejemplo n.º 9
0
def get_dataless_dataset(model):
    """
    Loads the dataset that model was trained on, without loading data.
    This is useful if you just need the dataset's metadata, like for
    formatting views of the model's weights.

    Parameters
    ----------
    model : Model

    Returns
    -------
    dataset : Dataset
        The data-less dataset as described above.
    """

    global yaml_parse
    global control

    if yaml_parse is None:
        from pylearn2.config import yaml_parse

    if control is None:
        from pylearn2.datasets import control

    control.push_load_data(False)
    try:
        rval = yaml_parse.load(model.dataset_yaml_src)
    finally:
        control.pop_load_data()
    return rval
Ejemplo n.º 10
0
def train_mlp2(data_path,
               dim_v=1406,
               dim_h=1700):
    save_path = "mlp2-{}-{}-on-" + data_path
    save_path = save_path.format(dim_h, dim_h)
    dim_h1 = dim_h0 = dim_h
    # save_path.format(dim_h0, dim_h1)

    path = "mlp_tutorial_part_2.yaml"
    with open(path, 'r') as f:
        train_2 = f.read()

    hyper_params = {'data_path': data_path,
                    'nvis': dim_v,
                    'dim_h0': dim_h0,
                    'dim_h1': dim_h1,
                    #'sparse_init_h1': 15,
                    'max_epochs': MAX_EPOCHS,
                    'save_path': save_path}
    train_2 = train_2 % (hyper_params)
    # print train_2

    train_2 = yaml_parse.load(train_2)
    print "save to {}".format(save_path)
    train_2.main_loop()
    return save_path
Ejemplo n.º 11
0
 def test_maxout_conv_c01b_cifar10(self):
     if cuda.cuda_available is False:
         raise SkipTest('Optional package cuda disabled')
     if not hasattr(cuda, 'unuse'):
         raise Exception("Theano version too old to run this test!")
     # Tests that we can run a small convolutional model on GPU,
     assert cuda.cuda_enabled is False
     # Even if there is a GPU, but the user didn't specify device=gpu
     # we want to run this test.
     try:
         old_floatX = config.floatX
         cuda.use('gpu')
         config.floatX = 'float32'
         try:
             train = yaml_parse.load(yaml_string_maxout_conv_c01b_cifar10)
         except NoDataPathError:
             raise SkipTest("PYLEARN2_DATA_PATH environment variable "
                            "not defined")
         train.main_loop()
         # Check that the performance is close to the expected one:
         # test_y_misclass: 0.3777000308036804
         misclass_chan = train.algorithm.monitor.channels['test_y_misclass']
         assert misclass_chan.val_record[-1] < 0.38
         # test_y_nll: 1.0978516340255737
         nll_chan = train.algorithm.monitor.channels['test_y_nll']
         assert nll_chan.val_record[-1] < 1.1
     finally:
         config.floatX = old_floatX
         cuda.unuse()
     assert cuda.cuda_enabled is False
Ejemplo n.º 12
0
def mlpwd_train_850(filename,
                    dim_v=850,
                    dim_h=1700,
                    wd=.0005,
                    foldi=1):
    save_path = save_path_tmp.format("-" + str(dim_h), "", "", wd, filename)
    dim_h1 = dim_h0 = dim_h

    yaml_path = "mlp_tutorial_part_4.yaml"
    with open(yaml_path, 'r') as f:
        train_2 = f.read()

    hyper_params = {'foldi': foldi,
                    'nvis': dim_v,
                    'dim_h0': dim_h0,
                    'dim_h1': dim_h1,
                    # 'sparse_init_h1': 15,
                    'max_epochs': MAX_EPOCHS,
                    'save_path': save_path}
    train_2 = train_2 % (hyper_params)

    train_2 = yaml_parse.load(train_2)
    print "save to {}".format(save_path)
    train_2.main_loop()
    return save_path
Ejemplo n.º 13
0
def train_autoencoder(dataset_path, nvis=2, nhid=2, act_enc=None, act_dec=None):
    yaml = open('outliers.yaml', 'r').read()
    if act_enc is None:
        act_enc = 'null'
    else:
        act_enc = "'" + act_enc + "'"

    if act_dec is None:
        act_dec = 'null'
    else:
        act_dec = "'" + act_dec + "'"

    params = {
        'dataset_path': dataset_path,
        'nvis': nvis,
        'nhid': nhid,
        'act_enc': act_enc,
        'act_dec': act_dec,
        'learning_rate': 0.05,
        'save_path': 'outliers.pkl'
    }
    
    yaml = yaml % (params)

    train = yaml_parse.load(yaml)
    train.main_loop()
    
    pkl = open('outliers.pkl')
    return cPickle.load(pkl)
Ejemplo n.º 14
0
def train_one_stage(model_yaml, dataset=None, max_epochs=500):
    """
    Train a stage of the cascade
    Return the path to the best model.
    ----------------------------------
    model_yaml : YAML file defining the model
    dataset : dataset object to train on
    model_file : target of save
    max_epochs : number of training epochs
    """
    with open(model_yaml, "r") as m_y:
        model = yaml_parse.load(m_y)
    print "Loaded YAML"

    # Define the length of training
    model.algorithm.termination_criterion._max_epochs = max_epochs

    if dataset is not None:
        # We use the specified dataset
        model.dataset = dataset

    # Train model
    model.main_loop()

    # Return the path to the best model obtained
    best_model_file = model.extensions[0].save_path
    return best_model_file
Ejemplo n.º 15
0
def Transform():
    """Test smaller version of convolutional_network.ipynb"""
    which_experiment = "S100"
    skip.skip_if_no_data()
    yaml_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    data_dir = string_utils.preprocess("${PYLEARN2_DATA_PATH}")
    save_path = os.path.join(data_dir, "cifar10", "experiment_" + string.lower(which_experiment))
    base_save_path = os.path.join(data_dir, "cifar10")
    # Escape potential backslashes in Windows filenames, since
    # they will be processed when the YAML parser will read it
    # as a string
    # save_path.replace('\\', r'\\')

    yaml = open("{0}/experiment_base_transform.yaml".format(yaml_file_path), "r").read()
    hyper_params = {
        "batch_size": 64,
        "output_channels_h1": 64,
        "output_channels_h2": 128,
        "output_channels_h3": 600,
        "max_epochs": 100,
        "save_path": save_path,
        "base_save_path": base_save_path,
    }
    yaml = yaml % (hyper_params)
    train = yaml_parse.load(yaml)
    train.main_loop()
Ejemplo n.º 16
0
def test_convolutional_network():
    """Test smaller version of convolutional_network.ipynb"""
    skip.skip_if_no_data()
    yaml_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                                  '..'))
    save_path = os.path.dirname(os.path.realpath(__file__))
    # Escape potential backslashes in Windows filenames, since
    # they will be processed when the YAML parser will read it
    # as a string
    save_path.replace('\\', r'\\')

    yaml = open("{0}/conv.yaml".format(yaml_file_path), 'r').read()
    hyper_params = {'train_stop': 50,
                    'valid_stop': 50050,
                    'test_stop': 50,
                    'batch_size': 50,
                    'output_channels_h2': 4,
                    'output_channels_h3': 4,
                    'max_epochs': 1,
                    'save_path': save_path}
    yaml = yaml % (hyper_params)
    train = yaml_parse.load(yaml)
    train.main_loop()

    try:
        os.remove("{}/convolutional_network_best.pkl".format(save_path))
    except OSError:
        pass
def train_layer3(patient_id, leave_one_out_seizure, data_path, yaml_file_path, save_model_path):
    """
    Script to pre-train the softmax layers.

    Parameter settings are specified in yaml files.

    Parameters
    ----------
    patient_id : int
        Patient ID.
    leave_one_out_seizure : int
        Index of the withheld seizure.
    data_path : string
        Path to the directory of the database.
    yaml_file_path : string
        Path to the directory of the yaml files.
    save_model_path : string
        Path to the directory to save the trained model.

    """

    yaml = open("{0}/sdae_l3.yaml".format(yaml_file_path), 'r').read()
    hyper_params = {'patient_id': patient_id,
                    'leave_one_out_seizure': leave_one_out_seizure,
                    'window_size': 256,
                    'batch_size': 20,
                    'monitoring_batches': 5,
                    'nvis': 500,
                    'n_classes': 2,
                    'max_epochs': 20,
                    'data_path': data_path,
                    'save_path': save_model_path}
    yaml = yaml % (hyper_params)
    train = yaml_parse.load(yaml)
    train.main_loop()
Ejemplo n.º 18
0
def test_train_example():
    
    # path definition
    train_path = cwd = os.getcwd()
    data_path = os.path.join(train_path, '..', '..', '..', '..', 'datasets', 'gtsrb', 'preprocessed')
    grbm_path = os.path.join(train_path, '..', 'grbm', 'grbm_gtsrb.pkl')
    grbm = serial.load(grbm_path)
    NVIS = grbm.nhid
    
    try:
        os.chdir(train_path)
        
        # load and train first layer
        if DROPOUT:
            train_yaml_path = os.path.join(train_path, 'mlp_gtsrb_dropout.yaml')
        else:
            train_yaml_path = os.path.join(train_path, 'mlp_gtsrb.yaml')
        layer1_yaml = open(train_yaml_path, 'r').read()
        hyper_params_l1 = {'batch_size': 100,
                           'nvis': NVIS,
                           'n_h0': N_HIDDEN_0,
                           'n_h1': N_HIDDEN_1,
                           'max_epochs': MAX_EPOCHS,
                           'data_path' : data_path,
                           'grbm_path' : grbm_path,
                           }
        
        layer1_yaml = layer1_yaml % (hyper_params_l1)
        train = yaml_parse.load(layer1_yaml)
        
        print '\nTraining...\n'
        train.main_loop()
        
    finally:
        os.chdir(cwd)
def main():
    parser = make_parser()
    args = parser.parse_args()
    model = cPickle.load(args.model)
    src = model.dataset_yaml_src
    test = yaml_parse.load(src)
    test = test.get_test_set()
    assert test.X.shape[0] == 10000
    test.X = test.X.astype('float32')
    test.y = test.y.astype('float32')
    X = test.X
    y = test.y
    Xb = model.get_input_space().make_batch_theano()
    Xb.name = 'Xb'
    yb = model.get_output_space().make_batch_theano()
    yb.name = 'yb'
    # W/2 network
    fn = make_error_fn(Xb, model.fprop(Xb), yb)
    mf_test_error = measure_test_error(fn, X, y, batch_size=args.batch_size)
    print "Test error: %f" % mf_test_error
    num_masks = range(args.low_samples, args.high_samples, args.step_samples)
    results = np.empty((args.repeats, len(num_masks)), dtype='float64')
    for i, n_masks in enumerate(num_masks):
        print "Gathering results for n_masks = %d..." % n_masks
        out = sampled_dropout_average(model, Xb, n_masks,
                                      per_example=args.per_example,
                                      input_include_probs={'h0': args.h0_prob},
                                      input_scales={'h0': args.h0_scale})
        f = make_error_fn(Xb, out, yb)
        for rep in xrange(args.repeats):
            print "Repeat %d" % (rep + 1)
            results[rep, i] = measure_test_error(f, X, y,
                                                 batch_size=args.batch_size)
        print "Done."
    np.save(args.outfile, results)
Ejemplo n.º 20
0
def test():
    skip_if_no_data()

    dirname = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')

    with open(os.path.join(dirname, 'sr_dataset.yaml'), 'r') as f:
        dataset = f.read()

    hyper_params = {'train_stop': 50}
    dataset = dataset % (hyper_params)

    with open(os.path.join(dirname, 'sr_model.yaml'), 'r') as f:
        model = f.read()

    with open(os.path.join(dirname, 'sr_algorithm.yaml'), 'r') as f:
        algorithm = f.read()

    hyper_params = {'batch_size': 10,
                    'valid_stop': 50050}
    algorithm = algorithm % (hyper_params)

    with open(os.path.join(dirname, 'sr_train.yaml'), 'r') as f:
        train = f.read()

    save_path = os.path.dirname(os.path.realpath(__file__))
    train = train % locals()

    train = yaml_parse.load(train)
    train.main_loop()

    try:
        os.remove("{}/softmax_regression.pkl".format(save_path))
        os.remove("{}/softmax_regression_best.pkl".format(save_path))
    except:
        pass
Ejemplo n.º 21
0
def test_stratified_dataset_shuffle_split():
    """Test StratifiedDatasetShuffleSplit."""
    skip_if_no_sklearn()
    mapping = {'dataset_iterator': 'StratifiedDatasetShuffleSplit'}
    test_yaml = test_yaml_dataset_iterator % mapping
    trainer = yaml_parse.load(test_yaml)
    trainer.main_loop()
def experiment(state, channel):
    """
    Experiment function.
    Used by jobman to run jobs. Must be loaded externally.

    Parameters
    ----------
    state: WRITEME
    channel: WRITEME
    """

    yaml_template = open(yaml_file).read()
    hyper_parameters = expand(flatten(state.hyper_parameters), dict_type=ydict)

    file_params = expand(flatten(state.file_parameters), dict_type=ydict)
    # Hack to fill in file parameter strings first
    for param in file_params:
        yaml_template = yaml_template.replace("%%(%s)s" % param, file_params[param])

    yaml = yaml_template % hyper_parameters
    train_object = yaml_parse.load(yaml)

    state.pid = os.getpid()
    channel.save()
    train_object.main_loop()

    state.results = extract_results(train_object.model)
    return channel.COMPLETE
Ejemplo n.º 23
0
def train_convolutional_network(yaml_with_hyper_params):

    skip.skip_if_no_data()

    train = yaml_parse.load(yaml_with_hyper_params)

    train.main_loop()
Ejemplo n.º 24
0
def main(args):
    dataset_name = args.dataset_name

    logger.info("Getting dataset info for %s" % dataset_name)
    data_path = serial.preprocess("${PYLEARN2_NI_PATH}/" + dataset_name)
    mask_file = path.join(data_path, "mask.npy")
    mask = np.load(mask_file)
    input_dim = (mask == 1).sum()

    user = path.expandvars("$USER")
    save_path = serial.preprocess("/export/mialab/users/%s/pylearn2_outs/%s"
                                  % (user, "rbm_simple_test"))

    # File parameters are path specific ones (not model specific).
    file_params = {"save_path": save_path,
                   }

    yaml_template = open(yaml_file).read()
    hyperparams = expand(flatten(experiment.default_hyperparams(input_dim=input_dim)),
                         dict_type=ydict)

    # Set additional hyperparams from command line args
    if args.learning_rate is not None:
        hyperparams["learning_rate"] = args.learning_rate
    if args.batch_size is not None:
        hyperparams["batch_size"] = args.batch_size

    for param in file_params:
        yaml_template = yaml_template.replace("%%(%s)s" % param, file_params[param])

    yaml = yaml_template % hyperparams

    logger.info("Training")
    train = yaml_parse.load(yaml)
    train.main_loop()
Ejemplo n.º 25
0
def test():
    skip_if_no_data()

    dirname = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')

    with open(os.path.join(dirname, 'sr_dataset.yaml'), 'r') as f:
        dataset = f.read()

    hyper_params = {'train_stop': 50}
    dataset = dataset % (hyper_params)

    with open(os.path.join(dirname, 'sr_model.yaml'), 'r') as f:
        model = f.read()

    with open(os.path.join(dirname, 'sr_algorithm.yaml'), 'r') as f:
        algorithm = f.read()

    hyper_params = {'batch_size': 10,
                    'valid_stop': 50050}
    algorithm = algorithm % (hyper_params)

    with open(os.path.join(dirname, 'sr_train.yaml'), 'r') as f:
        train = f.read()

    train = train % locals()

    train = yaml_parse.load(train)
    train.main_loop()
def train(config, config_args):

    # Load config replacing tags
    with open(config, 'r') as f:
        config = ''.join(f.readlines())
    for nam in config_args:
        config = config.replace('${' + nam + "}", config_args[nam])

    train_obj = yaml_parse.load(config)

    try:
        iter(train_obj)
        iterable = True
    except TypeError:
        iterable = False

    # # Undo our custom logging setup.
    # restore_defaults()
    # root_logger = logging.getLogger()
    # formatter = CustomFormatter(prefix='%(asctime)s ', only_from='pylearn2')
    # handler = CustomStreamHandler(formatter=formatter)
    # root_logger.addHandler(handler)
    # root_logger.setLevel(logging.INFO)

    if iterable:
        for number, subobj in enumerate(iter(train_obj)):
            # Execute this training phase.
            subobj.main_loop()
            del subobj
            gc.collect()
    else:
        train_obj.main_loop()
def train_mlp():
    # Fine tuning and softmax layer training
    mlp_path = os.path.join("experiments", name, "mlp.yaml")
    mlp_file = open(mlp_path, "r")
    mlp_yaml = mlp_file.read()
    mlp_file.close()
    hyper_params_mlp = {'dataset_path' : dataset_path,
                       'label_path' : label_path,
                       'layer1_model' : layer1_model,
                       'layer2_model' : layer2_model,
                       'layer3_model' : layer3_model,
                       'layer4_model' : layer4_model,
                       'layer5_model' : layer5_model,
                       'dae_model' : dae_model,
                       'nvis' : nvis1,
                       'dim' : nhid5,
                       'batch_size' : batch_size_mlp,
                       'max_epochs' : max_epochs,
                       'save_path' : mlp_model,
                       'class_number' : class_number
                       }
    mlp_yaml = mlp_yaml % (hyper_params_mlp)
    print mlp_yaml

    # Train mlp
    train = yaml_parse.load(mlp_yaml)
    train.main_loop()
Ejemplo n.º 28
0
def test_convolutional_network():
    """Test smaller version of convolutional_network.ipynb"""
    #     skip.skip_if_no_data()
    print "hi"
    yaml_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    save_path = os.path.dirname(os.path.realpath(__file__))
    # Escape potential backslashes in Windows filenames, since
    # they will be processed when the YAML parser will read it
    # as a string
    save_path.replace("\\", r"\\")

    yaml = open("{0}/conv.yaml".format(yaml_file_path), "r").read()
    hyper_params = {
        "train_stop": 50,
        "valid_stop": 50050,
        "test_stop": 50,
        "batch_size": 50,
        "output_channels_h2": 4,
        "output_channels_h3": 4,
        "max_epochs": 1,
        "save_path": save_path,
    }
    yaml = yaml % (hyper_params)
    train = yaml_parse.load(yaml)
    train.main_loop()

    try:
        os.remove("{}/convolutional_network_best.pkl".format(save_path))
    except OSError:
        pass
Ejemplo n.º 29
0
def test_stratified_dataset_k_fold():
    """Test StratifiedDatasetKFold."""
    skip_if_no_sklearn()
    mapping = {'dataset_iterator': 'StratifiedDatasetKFold'}
    test_yaml = test_yaml_dataset_iterator % mapping
    trainer = yaml_parse.load(test_yaml)
    trainer.main_loop()
Ejemplo n.º 30
0
def show_negative_chains(model_path):
    """
    Display negative chains.

    Parameters
    ----------
    model_path: str
        The path to the model pickle file
    """
    model = serial.load(model_path)

    try:
        control.push_load_data(False)
        dataset = yaml_parse.load(model.dataset_yaml_src)
    finally:
        control.pop_load_data()

    try:
        layer_to_chains = model.layer_to_chains
    except AttributeError:
        print("This model doesn't have negative chains.")
        quit(-1)

    vis_chains = get_vis_chains(layer_to_chains, model, dataset)

    m = vis_chains.shape[0]
    grid_shape = get_grid_shape(m)

    return create_patch_viewer(grid_shape, vis_chains, m)
Ejemplo n.º 31
0
    def __call__(self):
        """
        Runs the model on the monitoring dataset in order to add one data point
        to each of the channels.
        """

        # If the channels have changed at all, we need to recompile the theano
        # functions used to compute them
        if self._dirty:
            self.redo_theano()

        datasets = self._datasets

        # Set all channels' val_shared to 0
        self.begin_record_entry()
        for d, i, b, n, a, sd, ne in safe_izip(datasets, self._iteration_mode,
                                               self._batch_size,
                                               self._num_batches, self.accum,
                                               self._rng_seed,
                                               self.num_examples):
            if isinstance(d, basestring):
                d = yaml_parse.load(d)
                raise NotImplementedError()

            # need to put d back into self._datasets
            myiterator = d.iterator(mode=i,
                                    batch_size=b,
                                    num_batches=n,
                                    data_specs=self._flat_data_specs,
                                    return_tuple=True,
                                    rng=sd)

            # If self._flat_data_specs is empty, no channel needs data,
            # so we do not need to call the iterator in order to average
            # the monitored values across different batches, we only
            # have to call them once.
            if len(self._flat_data_specs[1]) == 0:
                X = ()
                self.run_prereqs(X, d)
                a(*X)

            else:
                actual_ne = 0
                for X in myiterator:
                    # X is a flat (not nested) tuple
                    self.run_prereqs(X, d)
                    a(*X)
                    actual_ne += self._flat_data_specs[0].np_batch_size(X)
                # end for X
                if actual_ne != ne:
                    raise RuntimeError("At compile time, your iterator said "
                                       "it had %d examples total, but at "
                                       "runtime it gave us %d." %
                                       (ne, actual_ne))
        # end for d

        log.info("Monitoring step:")
        log.info("\tEpochs seen: %d" % self._epochs_seen)
        log.info("\tBatches seen: %d" % self._num_batches_seen)
        log.info("\tExamples seen: %d" % self._examples_seen)
        t = time.time() - self.t0
        for channel_name in sorted(self.channels.keys(),
                                   key=number_aware_alphabetical_key):
            channel = self.channels[channel_name]
            channel.time_record.append(t)
            channel.batch_record.append(self._num_batches_seen)
            channel.example_record.append(self._examples_seen)
            channel.epoch_record.append(self._epochs_seen)
            val = channel.val_shared.get_value()
            channel.val_record.append(val)
            # TODO: use logging infrastructure so that user can configure
            # formatting
            if abs(val) < 1e4:
                val_str = str(val)
            else:
                val_str = '%.3e' % val

            log.info("\t%s: %s" % (channel_name, val_str))
Ejemplo n.º 32
0
    pv.show()
else:
    pv.save(out_prefix + "_prod.png")

print 'Sorting so largest-norm layer 2 weights are plotted at the top'
norms = np.square(W2).sum(axis=0)
idxs = [elem[1] for elem in sorted(zip(-norms, range(norms.shape[0])))]

new = W2.copy()

for i in xrange(len(idxs)):
    new[:, i] = W2[:, idxs[i]]
W2 = new

dataset_yaml_src = model.dataset_yaml_src
dataset = yaml_parse.load(dataset_yaml_src)

import numpy as np

imgs = dataset.get_weights_view(W1.T)

N1 = W1.shape[1]
N = W2.shape[1]

N = min(N, 100)

thresh = .9
max_count = 0
total_counts = 0.
for i in xrange(N):
    w = W2[:, i]
Ejemplo n.º 33
0
def test_sparse_activation():
    """Test autoencoder sparse activation cost."""
    trainer = yaml_parse.load(test_yaml)
    trainer.main_loop()
def analyze(config):
    output_path = config.get('output_path')
    #     model_file = os.path.join(output_path, 'eeg', 'conv3', 'convolutional_network.pkl');
    #     model_file = os.path.join(output_path, 'eeg', 'conv10', 'epochs', 'cnn_epoch94.pkl');
    model_file = '../../../debug/debug_run4/debug_network.pkl'
    with log_timing(log, 'loading convnet model from {}'.format(model_file)):
        model = serial.load(model_file)

    input_shape = model.get_input_space().shape

    config = config.eeg
    hyper_params = {
        'input_length': input_shape[
            0],  #25+151-1+301-1, # this should leave a single value per channel after convolution
        'hop_size': 5,  # reduce amount of data by factor 5
        'dataset_root': config.get('dataset_root'),
        'dataset_suffix': config.get('dataset_suffix'),
        'save_path': config.get('save_path'),
    }

    dataset_yaml = '''
    !obj:deepbeat.eeg.EEGDataset.EEGDataset {
                                 name : 'testset',
                                 path : %(dataset_root)s, 
                                 suffix : '_channels', # %(dataset_suffix)s,
                                 subjects : [0],
                                 resample : [400, 100],
                                 start_sample : 2500,
                                 stop_sample  : 3200,     # None (empty) = end of sequence
                  # FIXME:                
#                                  n_fft : 24,
#                                  frame_size : 10, # %(input_length)i,                                
                                 frame_size : %(input_length)i,
                                 
                                 hop_size : %(hop_size)i,           
                                 label_mode : 'rhythm_type',
#                                  save_matrix_path: '../../../debug/debug.pkl'
                            }
'''
    dataset_yaml = dataset_yaml % hyper_params
    print dataset_yaml

    with log_timing(log, 'parsing yaml'):
        testset = yaml_parse.load(dataset_yaml)


#     print testset.subject_partitions;
#     print testset.sequence_partitions;

    seq_starts = testset.sequence_partitions
    #     return;

    #     axes=['b', 0, 1, 'c']
    #     def dimshuffle(b01c):
    #         default = ('b', 0, 1, 'c')
    #         return b01c.transpose(*[default.index(axis) for axis in axes])
    #     data = dimshuffle(testset.X);

    #     design_matrix = model.get_design_matrix()

    #     view_converter = DefaultViewConverter([475, 1, 1]);
    #     data = view_converter.

    #     ## get the labels
    #     data_specs= (model.get_output_space(), "targets");
    #     it = testset.iterator(
    #                            mode='sequential',
    #                            batch_size=100,
    #                            data_specs=data_specs);
    #     labels = np.hstack([np.argmax(minibatch, axis = 1) for minibatch in it])
    #     print labels[0:1000]
    #
    #     ## get the predictions
    #     minibatch = model.get_input_space().make_theano_batch();
    #     output_fn = theano.function(inputs=[minibatch],
    #                                 outputs=T.argmax(model.fprop(minibatch), axis = 1));
    #     print "function compiled"
    # #     data_specs= (CompositeSpace((
    # #                                 model.get_input_space(),
    # #                                 model.get_output_space())),
    # #                 ("features", "targets"));
    #
    #     data_specs= (model.get_input_space(), "features");
    #     it = testset.iterator(
    #                             mode='sequential',
    #                             batch_size=100,
    #                             data_specs=data_specs);
    #     print "iterator ready"
    #
    #     y_pred = np.hstack([output_fn(minibatch) for minibatch in it])
    #
    #     print y_pred[0:1000]

    minibatch = model.get_input_space().make_theano_batch()
    output_fn = theano.function(inputs=[minibatch],
                                outputs=T.argmax(model.fprop(minibatch),
                                                 axis=1))
    print "function compiled"

    data_specs = (CompositeSpace(
        (model.get_input_space(), model.get_output_space())), ("features",
                                                               "targets"))
    it = testset.iterator('sequential', batch_size=100, data_specs=data_specs)
    print "iterator ready"

    y_pred = []
    y_real = []
    for minibatch, target in it:
        y_pred.append(output_fn(minibatch))
        y_real.append(np.argmax(target, axis=1))
    y_pred = np.hstack(y_pred)
    y_real = np.hstack(y_real)

    print y_pred[0:1000]

    print classification_report(y_real, y_pred)
    print confusion_matrix(y_real, y_pred)

    misclass = (y_real != y_pred)
    print misclass.mean()

    correct = 0
    s_real = []
    s_pred = []
    s_pred_agg = []

    n_channels = 16
    channel_scores = np.zeros(n_channels, dtype=np.int)

    for i in xrange(len(seq_starts)):

        start = seq_starts[i]
        if i < len(seq_starts) - 1:
            stop = seq_starts[i + 1]
        else:
            stop = None

        s_real.append(y_real[start])

        #         print np.bincount(y_pred[start:stop]);
        #         print np.argmax(np.bincount(y_pred[start:stop]));

        s_pred.append(np.argmax(np.bincount(y_pred[start:stop])))

        s_pred_agg.append(np.mean(y_pred[start:stop]))
        # works only for binary classification

        seq_misclass = misclass[start:stop].mean()
        #         print '{} [{}{}]: {}'.format(i, start, stop, seq_misclass);

        if seq_misclass < 0.5:  # more correct than incorrect
            correct += 1
            channel_scores[i % n_channels] += 1

    s_real = np.hstack(s_real)
    s_pred = np.hstack(s_pred)

    print s_real
    print s_pred
    print s_pred_agg

    print 'aggregated'
    print classification_report(s_real, s_pred)
    print confusion_matrix(s_real, s_pred)

    s_misclass = (s_real != s_pred)
    print s_misclass.mean()

    print channel_scores

    return

    input_shape = model.get_input_space().shape

    print input_shape

    view_converter = DefaultViewConverter((input_shape[0], input_shape[1], 1))

    data = view_converter.design_mat_to_topo_view(testset.X)
    print data.shape

    X = model.get_input_space().make_theano_batch()
    Y = model.fprop(X)
    Y = T.argmax(Y, axis=1)  # needed - otherwise not single value
    output_fn = theano.function([X], Y)

    #     y_pred = output_fn( data );

    batch_size = 1000
    y_pred = []
    batch_start = 0
    while batch_start < data.shape[0]:
        batch_stop = min(data.shape[0], batch_start + batch_size)
        y_pred.append(output_fn(data[batch_start:batch_stop]))
        #         if batch_start == 0: print y_pred;
        batch_start = batch_stop
    y_pred = np.hstack(y_pred)

    print testset.labels[0:1000]
    print y_pred[0:1000]

    print classification_report(testset.labels, y_pred)
    print confusion_matrix(testset.labels, y_pred)

    labels = np.argmax(testset.y, axis=1)
    print classification_report(labels, y_pred)
    print confusion_matrix(labels, y_pred)

    labels = np.argmax(testset.y, axis=1)
    print classification_report(labels, y_pred)
    print confusion_matrix(labels, y_pred)

    misclass = (labels != y_pred).mean()
    print misclass

    #     # alternative version from KeepBestParams
    #     minibatch = T.matrix('minibatch')
    #     output_fn = theano.function(inputs=[minibatch],outputs=T.argmax( model.fprop(minibatch), axis = 1 ));
    #     it = testset.iterator('sequential', batch_size=batch_size, targets=False);
    #     y_pred = [output_fn(mbatch) for mbatch in it];

    #             y_hat = T.argmax(state, axis=1)
    #             y = T.argmax(target, axis=1)
    #             misclass = T.neq(y, y_hat).mean()
    #             misclass = T.cast(misclass, config.floatX)
    #             rval['misclass'] = misclass
    #             rval['nll'] = self.cost(Y_hat=state, Y=target)

    log.debug('done')
Ejemplo n.º 35
0
def train_yaml(yaml_file):

    train = yaml_parse.load(yaml_file)
    train.main_loop()
Ejemplo n.º 36
0
def get_weights_report(model_path=None,
                       model=None,
                       rescale='individual',
                       border=False,
                       norm_sort=False,
                       dataset=None):
    """
        Returns a PatchViewer displaying a grid of filter weights

        Parameters:
            model_path: the filepath of the model to make the report on.
            rescale: a string specifying how to rescale the filter images
                        'individual' (default): scale each filter so that it
                            uses as much as possible of the dynamic range
                            of the display under the constraint that 0
                            is gray and no value gets clipped
                        'global' : scale the whole ensemble of weights
                        'none' :   don't rescale
            dataset: a Dataset object to do view conversion for displaying the weights.
                    if not provided one will be loaded from the model's dataset_yaml_src
    """

    if model is None:
        print 'making weights report'
        print 'loading model'
        model = serial.load(model_path)
        print 'loading done'
    else:
        assert model_path is None
    assert model is not None

    if rescale == 'none':
        global_rescale = False
        patch_rescale = False
    elif rescale == 'global':
        global_rescale = True
        patch_rescale = False
    elif rescale == 'individual':
        global_rescale = False
        patch_rescale = True
    else:
        raise ValueError('rescale=' + rescale +
                         ", must be 'none', 'global', or 'individual'")

    if isinstance(model, dict):
        #assume this was a saved matlab dictionary
        del model['__version__']
        del model['__header__']
        del model['__globals__']
        weights, = model.values()

        norms = np.sqrt(np.square(weights).sum(axis=1))
        print 'min norm: ', norms.min()
        print 'mean norm: ', norms.mean()
        print 'max norm: ', norms.max()

        return patch_viewer.make_viewer(weights,
                                        is_color=weights.shape[1] % 3 == 0)

    weights_view = None
    W = None

    try:
        weights_view = model.get_weights_topo()
        h = weights_view.shape[0]
    except Exception, e:

        if dataset is None:
            print 'loading dataset...'
            control.push_load_data(False)
            dataset = yaml_parse.load(model.dataset_yaml_src)
            control.pop_load_data()
            print '...done'

        if hasattr(model, 'get_weights'):
            W = model.get_weights()

        if 'weightsShared' in dir(model):
            W = model.weightsShared.get_value()

        if 'W' in dir(model):
            if hasattr(model.W, '__array__'):
                warnings.warn(
                    'model.W is an ndarray; I can figure out how to display this but that seems like a sign of a bad bug'
                )
                W = model.W
            else:
                W = model.W.get_value()

        has_D = False
        if 'D' in dir(model):
            has_D = True
            D = model.D

        if 'enc_weights_shared' in dir(model):
            W = model.enc_weights_shared.get_value()

        if W is None:
            raise AttributeError(
                'model does not have a variable with a name like "W", "weights", etc  that pylearn2 recognizes'
            )
Ejemplo n.º 37
0
from pylearn2.config.yaml_parse import load
import theano.tensor as T
from theano import function
import numpy as np
import matplotlib.pyplot as plt
from theano import config
#float32 seems to break David's code
config.floatX = 'float32'

print 'loading model'
model = serial.load(sys.argv[1])
model.make_pseudoparams()
model.set_dtype(config.floatX)

print 'loading dataset'
dataset = load(model.dataset_yaml_src)

print 'compiling function'
V = T.fmatrix()
obs = model.e_step.variational_inference(V)
H = obs['H_hat']
S = obs['S_hat']
HS = abs(H * S)

f = function([V], HS)

print 'running inference'
batch_size = 5000

X = dataset.get_batch_design(batch_size)
HS = f(X)
Ejemplo n.º 38
0
def train_yaml(yaml_file):

    train = yaml_parse.load(yaml_file)

    mode1 = serial.load(os.environ['MMDAErbms'] + '/laser_best.pkl')
    mode2 = serial.load(os.environ['MMDAErbms'] + '/command_best.pkl')
    deep = serial.load(os.environ['MMDAErbms'] + '/laser_command_best.pkl')

    models = [mode1, deep, deep, mode1, mode2]

    layers = list()

    f = theano.config.floatX

    for ii, layer in enumerate(train.model.layers):
        if type(layer) is FlattenerLayer:
            for l in layer.raw_layer.layers:
                layers.append(l)
        elif type(layer) is SplitterLayer:
            layers.append(layer.raw_layer)
        else:
            layers.append(layer)

    for ii, (layer, model) in enumerate(zip(layers, models)):
        if ii < len(layers) / 2:
            if type(layer) is Sigmoid:
                if layer.get_weights().shape != model.get_weights():
                    layer.set_weights(
                        model.get_weights()
                        [:layer.get_weights().shape[0], :].astype(f))
                else:
                    layer.set_weights(model.get_weights().astype(f))
                if len(model.get_param_values()) == 4:
                    layer.set_biases(model.get_param_values()[3].astype(f))
                else:
                    layer.set_biases(model.get_param_values()[2].astype(f))
        else:
            if type(layer) is Sigmoid:
                if layer.enc_layer is None:
                    layer.set_weights(
                        model.get_weights().transpose().astype(f))
                layer.set_biases(model.get_param_values()[0].astype(f))
            elif type(layer) is LinearGaussian:
                params = model.get_param_values()
                if layer.enc_layer is None:
                    layer.set_weights(params[2].transpose().astype(f))
                layer.set_biases(params[1].astype(f))
                beta = model.get_params()[0].eval()
                if isinstance(beta, N.ndarray):
                    layer.beta.set_value(
                        model.get_params()[0].eval().astype(f))
                elif isinstance(beta,
                                theano.sandbox.cuda.type.CudaNdarrayType):
                    layer.beta.set_value(model.get_params()[0].eval().dtype(f))

    del models
    del mode1
    del mode2
    del deep

    train.main_loop()
Ejemplo n.º 39
0
def get_weights_report(model_path=None,
                       model=None,
                       rescale='individual',
                       border=False,
                       norm_sort=False,
                       dataset=None,
                       channels=[0, 1, 2]):
    """
    Returns a PatchViewer displaying a grid of filter weights

    Parameters
    ----------
    model_path : str
        Filepath of the model to make the report on.
    rescale : str
        A string specifying how to rescale the filter images:
            - 'individual' (default) : scale each filter so that it
                  uses as much as possible of the dynamic range
                  of the display under the constraint that 0
                  is gray and no value gets clipped
            - 'global' : scale the whole ensemble of weights
            - 'none' :   don't rescale
    dataset : pylearn2.datasets.dataset.Dataset
        Dataset object to do view conversion for displaying the weights. If
        not provided one will be loaded from the model's dataset_yaml_src.

    Returns
    -------
    WRITEME
    """

    channels = channels.split(',')
    channels = [int(ch) for ch in channels]

    if model is None:
        logger.info('making weights report')
        logger.info('loading model')
        model = serial.load(model_path)
        logger.info('loading done')
    else:
        assert model_path is None
    assert model is not None

    if rescale == 'none':
        global_rescale = False
        patch_rescale = False
    elif rescale == 'global':
        global_rescale = True
        patch_rescale = False
    elif rescale == 'individual':
        global_rescale = False
        patch_rescale = True
    else:
        raise ValueError('rescale=' + rescale +
                         ", must be 'none', 'global', or 'individual'")

    if isinstance(model, dict):
        #assume this was a saved matlab dictionary
        del model['__version__']
        del model['__header__']
        del model['__globals__']
        keys = [key for key in model \
                if hasattr(model[key], 'ndim') and model[key].ndim == 2]
        if len(keys) > 2:
            key = None
            while key not in keys:
                logger.info('Which is the weights?')
                for key in keys:
                    logger.info('\t{0}'.format(key))
                key = raw_input()
        else:
            key, = keys
        weights = model[key]

        norms = np.sqrt(np.square(weights).sum(axis=1))
        logger.info('min norm: {0}'.format(norms.min()))
        logger.info('mean norm: {0}'.format(norms.mean()))
        logger.info('max norm: {0}'.format(norms.max()))

        return patch_viewer.make_viewer(weights,
                                        is_color=weights.shape[1] % 3 == 0)

    weights_view = None
    W = None

    try:
        weights_view = model.get_weights_topo()
        h = weights_view.shape[0]
    except NotImplementedError:

        if dataset is None:
            logger.info('loading dataset...')
            control.push_load_data(False)
            dataset = yaml_parse.load(model.dataset_yaml_src)
            control.pop_load_data()
            logger.info('...done')

        try:
            W = model.get_weights()
        except AttributeError, e:
            raise AttributeError("""
Encountered an AttributeError while trying to call get_weights on a model.
This probably means you need to implement get_weights for this model class,
but look at the original exception to be sure.
If this is an older model class, it may have weights stored as weightsShared,
etc.
Original exception: """ + str(e))
    patch_rescale = False
elif rescale == 'individual':
    global_rescale = False
    patch_rescale = True
else:
    assert False

if path.endswith('.pkl'):
    from pylearn2.utils import serial
    obj = serial.load(path)
elif path.endswith('.yaml'):
    print 'Building dataset from yaml...'
    obj = yaml_parse.load_path(path)
    print '...done'
else:
    obj = yaml_parse.load(path)

rows = options.rows
cols = options.cols

if hasattr(obj, 'get_batch_topo'):
    #obj is a Dataset
    dataset = obj

    examples = dataset.get_batch_topo(rows * cols)
else:
    #obj is a Model
    model = obj
    from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
    theano_rng = RandomStreams(42)
    design_examples_var = model.random_design_matrix(batch_size=rows * cols,
Ejemplo n.º 41
0
import sys

_, model_path = sys.argv

model = serial.load(model_path)

src = model.dataset_yaml_src
batch_size = model.force_batch_size
# handle bug in older pkl files, where set_batch_size had updated
# batch_size but not force_batch_size
if hasattr(model, 'batch_size') and model.batch_size != model.force_batch_size:
    batch_size = model.batch_size


assert src.find('train') != -1
test = yaml_parse.load(src)
x = raw_input("test acc? ")
if x == 'y':
    test = test.get_test_set()
    assert test.X.shape[0] == 10000
else:
    assert x == 'n'

if x == 'y':
    if not (test.X.shape[0] == 10000):
        print test.X.shape[0]
        assert False
else:
    # compute the train accuracy on what the model
    # was trained on, not the entire train set
    assert test.X.shape[0] in [40000,50000,60000]
Ejemplo n.º 42
0
def show_examples(path, rows, cols, rescale='global', out=None):
    """
    .. todo::

        WRITEME

    Parameters
    ----------
    path : string
        The pickle or YAML file to show examples of
    rows : int
        WRITEME
    cols : int
        WRITEME
    rescale : {'rescale', 'global', 'individual'}
        Default is 'rescale', WRITEME
    out : string, optional
        WRITEME
    """

    if rescale == 'none':
        global_rescale = False
        patch_rescale = False
    elif rescale == 'global':
        global_rescale = True
        patch_rescale = False
    elif rescale == 'individual':
        global_rescale = False
        patch_rescale = True

    if path.endswith('.pkl'):
        from pylearn2.utils import serial
        obj = serial.load(path)
    elif path.endswith('.yaml'):
        print('Building dataset from yaml...')
        obj = yaml_parse.load_path(path)
        print('...done')
    else:
        obj = yaml_parse.load(path)

    if hasattr(obj, 'get_batch_topo'):
        # obj is a Dataset
        dataset = obj

        examples = dataset.get_batch_topo(rows*cols)
    else:
        # obj is a Model
        model = obj
        from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
        theano_rng = RandomStreams(42)
        design_examples_var = model.random_design_matrix(
            batch_size=rows * cols, theano_rng=theano_rng
        )
        from theano import function
        print('compiling sampling function')
        f = function([], design_examples_var)
        print('sampling')
        design_examples = f()
        print('loading dataset')
        dataset = yaml_parse.load(model.dataset_yaml_src)
        examples = dataset.get_topological_view(design_examples)

    norms = np.asarray([np.sqrt(np.sum(np.square(examples[i, :])))
                        for i in xrange(examples.shape[0])])
    print('norms of examples: ')
    print('\tmin: ', norms.min())
    print('\tmean: ', norms.mean())
    print('\tmax: ', norms.max())

    print('range of elements of examples', (examples.min(), examples.max()))
    print('dtype: ', examples.dtype)

    examples = dataset.adjust_for_viewer(examples)

    if global_rescale:
        examples /= np.abs(examples).max()

    if len(examples.shape) != 4:
        print('sorry, view_examples.py only supports image examples for now.')
        print('this dataset has ' + str(len(examples.shape) - 2), end='')
        print('topological dimensions')
        quit(-1)

    if examples.shape[3] == 1:
        is_color = False
    elif examples.shape[3] == 3:
        is_color = True
    else:
        print('got unknown image format with', str(examples.shape[3]), end='')
        print('channels')
        print('supported formats are 1 channel greyscale or three channel RGB')
        quit(-1)

    print(examples.shape[1:3])

    pv = patch_viewer.PatchViewer((rows, cols), examples.shape[1:3],
                                  is_color=is_color)

    for i in xrange(rows*cols):
        pv.add_patch(examples[i, :, :, :], activation=0.0,
                     rescale=patch_rescale)

    if out is None:
        pv.show()
    else:
        pv.save(out)
Ejemplo n.º 43
0
                            }
                       }
                       ]
               },
               # termination_criterion: !obj:pylearn2.termination_criteria.MonitorBased
               # {
               #          channel_name: "valid_err",
               #          N: 100,
               #          prop_decrease: 0.
               # }
        },
    extensions: [
                !obj:pylearn2.training_algorithms.sgd.MomentumAdjustor {
                    start: 1,
                    saturate: 2,
                    final_momentum: 0.732716
                },
                !obj:pylearn2.training_algorithms.sgd.LinearDecayOverEpoch {
                    start: 1,
                    saturate: 868,
                    decay_factor: 0.020379
                }
        ],
    save_freq : 1
}
"""

train_obj = yaml_parse.load(yaml)

train_obj.main_loop()
Ejemplo n.º 44
0
def main(options, positional_args):
    """
    .. todo::

        WRITEME
    """
    assert len(positional_args) == 1

    path, = positional_args

    out = options.out
    rescale = options.rescale

    if rescale == 'none':
        global_rescale = False
        patch_rescale = False
    elif rescale == 'global':
        global_rescale = True
        patch_rescale = False
    elif rescale == 'individual':
        global_rescale = False
        patch_rescale = True
    else:
        assert False

    if path.endswith('.pkl'):
        from pylearn2.utils import serial
        obj = serial.load(path)
    elif path.endswith('.yaml'):
        print 'Building dataset from yaml...'
        obj = yaml_parse.load_path(path)
        print '...done'
    else:
        obj = yaml_parse.load(path)

    rows = options.rows
    cols = options.cols

    if hasattr(obj, 'get_batch_topo'):
        # obj is a Dataset
        dataset = obj

        examples = dataset.get_batch_topo(rows * cols)
    else:
        # obj is a Model
        model = obj
        from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
        theano_rng = RandomStreams(42)
        design_examples_var = model.random_design_matrix(batch_size=rows *
                                                         cols,
                                                         theano_rng=theano_rng)
        from theano import function
        print 'compiling sampling function'
        f = function([], design_examples_var)
        print 'sampling'
        design_examples = f()
        print 'loading dataset'
        dataset = yaml_parse.load(model.dataset_yaml_src)
        examples = dataset.get_topological_view(design_examples)

    norms = N.asarray([
        N.sqrt(N.sum(N.square(examples[i, :])))
        for i in xrange(examples.shape[0])
    ])
    print 'norms of examples: '
    print '\tmin: ', norms.min()
    print '\tmean: ', norms.mean()
    print '\tmax: ', norms.max()

    print 'range of elements of examples', \
            (examples.min(),examples.max())
    print 'dtype: ', examples.dtype

    examples = dataset.adjust_for_viewer(examples)

    if global_rescale:
        examples /= N.abs(examples).max()

    if len(examples.shape) != 4:
        print 'sorry, view_examples.py only supports image examples' + \
                'for now.'
        print 'this dataset has ' + \
                str(len(examples.shape)-2)+' topological dimensions'
        quit(-1)

    if examples.shape[3] == 1:
        is_color = False
    elif examples.shape[3] == 3:
        is_color = True
    else:
        print 'got unknown image format with ' + str(examples.shape[3]) + \
                ' channels'
        print 'supported formats are 1 channel greyscale or three channel RGB'
        quit(-1)

    print examples.shape[1:3]

    pv = patch_viewer.PatchViewer((rows, cols),
                                  examples.shape[1:3],
                                  is_color=is_color)

    for i in xrange(rows * cols):
        pv.add_patch(examples[i, :, :, :],
                     activation=0.0,
                     rescale=patch_rescale)

    if out is None:
        pv.show()
    else:
        pv.save(out)
Ejemplo n.º 45
0
def main_phone(model_path):
    print 'Loading model...'
    model = serial.load(model_path)
    std = numpy.sqrt(model.monitor.channels['train_objective'].val_record[-1])

    dataset_yaml_src = model.dataset_yaml_src
    dataset = yaml_parse.load(dataset_yaml_src)
    data_specs = (VectorSpace(dim=model.nvis), 'features')
    it = dataset.iterator(mode='sequential', data_specs=data_specs,
                          num_batches=1, batch_size=1)
    original_sequence = dataset.X


    X = T.vector('X')
    h = T.vector('h')
    out = T.vector('out')

    next_h, pred = model.fprop_step(X, h, out)
    fn = theano.function(inputs=[X, h, out], outputs=[next_h, pred],
                         on_unused_input='ignore')

    # Reconstruction
    numpy_h = numpy.zeros(model.nhid)
    numpy_out = numpy.zeros(1)
    x_t = numpy.copy(original_sequence[0])

    reconstruction_list = [original_sequence[0]]
    for __ in original_sequence:
        numpy_h, numpy_out = fn(x_t, numpy_h, numpy_out)
        sampled_numpy_out = numpy.random.normal(loc=numpy_out,
                                                scale=std,
                                                size=(1,))
        # reconstruction_list.append(sampled_numpy_out)
        reconstruction_list.append(numpy_out)
        x_t[:-1] = x_t[1:]
        # x_t[-1] = sampled_numpy_out
        x_t[-1] = numpy_out

    numpy_reconstruction = numpy.concatenate(reconstruction_list)
    numpy_reconstruction = (numpy_reconstruction * dataset._std + dataset._mean) * \
                           dataset._mean_norm
    numpy_reconstruction = numpy.cast['int16'](numpy_reconstruction)
    wf.write("reconstruction.wav", 16000, numpy_reconstruction)

    # One-on-one prediction
    numpy_h = numpy.zeros(model.nhid)
    numpy_out = numpy.zeros(1)

    prediction_list = [numpy.copy(original_sequence[0])]
    for x_t in original_sequence:
        numpy_h, numpy_out = fn(x_t, numpy_h, numpy_out)
        prediction_list.append(numpy_out)

    numpy_prediction = numpy.concatenate(prediction_list)
    numpy_prediction = (numpy_prediction * dataset._std + dataset._mean) * \
                           dataset._mean_norm
    numpy_prediction = numpy.cast['int16'](numpy_prediction)
    wf.write("prediction.wav", 16000, numpy_prediction)

    original= numpy.concatenate([original_sequence[0],
                                 original_sequence[1:, -1]])
    original = (original * dataset._std + dataset._mean) * dataset._mean_norm
    original= numpy.cast['int16'](original)
    wf.write("original.wav", 16000, original)

    from matplotlib import pyplot
    pyplot.figure()
    pyplot.subplot(3, 1, 1)
    pyplot.plot(original, 'r')
    pyplot.subplot(3, 1, 2)
    pyplot.plot(numpy_prediction, 'b')
    pyplot.subplot(3, 1, 3)
    pyplot.plot(numpy_reconstruction, 'g')
    pyplot.savefig('phone_audio.png')
Ejemplo n.º 46
0
def train_yaml(yaml_file):
    # Makes a Pylearn2 train object
    train = yaml_parse.load(yaml_file)

    # Trains
    train.main_loop()
Ejemplo n.º 47
0
import os

dirname = os.path.abspath(os.path.dirname('mlp.py'))
with open(os.path.join(dirname, 'mlp.yaml'), 'r') as f:
    train = f.read()

from pylearn2.config import yaml_parse
train = yaml_parse.load(train)
print train
train.main_loop()
Ejemplo n.º 48
0
# coding=utf-8
'''
Created on 2015年9月16日

@author: BFD474
'''
from pylearn2.config import yaml_parse

if __name__ == '__main__':
    fp = open('example.yaml')
    model = yaml_parse.load(fp)
    print model
    fp.close()
Ejemplo n.º 49
0
def get_weights_report(model_path=None,
                       model=None,
                       rescale='individual',
                       border=False,
                       norm_sort=False,
                       dataset=None):
    """
        Returns a PatchViewer displaying a grid of filter weights

        Parameters:
            model_path: the filepath of the model to make the report on.
            rescale: a string specifying how to rescale the filter images
                        'individual' (default): scale each filter so that it
                            uses as much as possible of the dynamic range
                            of the display under the constraint that 0
                            is gray and no value gets clipped
                        'global' : scale the whole ensemble of weights
                        'none' :   don't rescale
            dataset: a Dataset object to do view conversion for displaying the weights.
                    if not provided one will be loaded from the model's dataset_yaml_src
    """

    if model is None:
        print 'making weights report'
        print 'loading model'
        model = serial.load(model_path)
        print 'loading done'
    else:
        assert model_path is None
    assert model is not None

    if rescale == 'none':
        global_rescale = False
        patch_rescale = False
    elif rescale == 'global':
        global_rescale = True
        patch_rescale = False
    elif rescale == 'individual':
        global_rescale = False
        patch_rescale = True
    else:
        raise ValueError('rescale=' + rescale +
                         ", must be 'none', 'global', or 'individual'")

    if isinstance(model, dict):
        #assume this was a saved matlab dictionary
        del model['__version__']
        del model['__header__']
        del model['__globals__']
        weights, = model.values()

        norms = np.sqrt(np.square(weights).sum(axis=1))
        print 'min norm: ', norms.min()
        print 'mean norm: ', norms.mean()
        print 'max norm: ', norms.max()

        return patch_viewer.make_viewer(weights,
                                        is_color=weights.shape[1] % 3 == 0)

    weights_view = None
    W = None

    try:
        weights_view = model.get_weights_topo()
        h = weights_view.shape[0]
    except NotImplementedError:

        if dataset is None:
            print 'loading dataset...'
            control.push_load_data(False)
            dataset = yaml_parse.load(model.dataset_yaml_src)
            control.pop_load_data()
            print '...done'

        try:
            W = model.get_weights()
        except AttributeError, e:
            raise AttributeError("""
Encountered an AttributeError while trying to call get_weights on a model.
This probably means you need to implement get_weights for this model class,
but look at the original exception to be sure.
If this is an older model class, it may have weights stored as weightsShared,
etc.
Original exception: """ + str(e))
Ejemplo n.º 50
0
def main(job_id, requested_params, cache):
    # Fix sub directory problems
    sys.path.append(os.path.dirname(os.getcwd()))
    os.chdir(os.path.dirname(os.path.realpath(__file__)))

    # Add parameters that are not currently being tuned but could potentially be tuned.
    params = additional_args
    params.update(requested_params)

    if params.get('rate', None) is not None:
        params['log_init_learning_rate'][0] += numpy.array([params['rate']])

    train_params = {
        'train_start': params['start'],
        'train_stop': params['stop'],
        'valid_start': 72000,
        'valid_stop': 86000,
        'test_stop': 4000,
        'batch_size': 100,
        'max_epochs': 10,
        'max_batches': 20,
        'sgd_seed': seed_str,
        'dim_h1': int(params['dim_h1'][0]),
        'irange_h1': math.pow(10, params['l_ir_h1'][0]),
        'max_col_norm_h1': params['max_norm_h1'][0],
        'dim_h2': int(params['dim_h2'][0]),
        'irange_h2': math.pow(10, params['l_ir_h2'][0]),
        'max_col_norm_h2': params['max_norm_h2'][0],
        'weight_decay_y': math.pow(10, params['l_wdecay_y'][0]),
        'max_col_norm_y': params['max_norm_y'][0],
        'irange_y': math.pow(10, params['l_ir_y'][0]),
        'init_momentum': 0.5,
        'init_learning_rate': math.pow(10,
                                       params['log_init_learning_rate'][0]),
    }

    with open('mlp_fooddata.yaml', 'r') as f:
        trainer = f.read()

    yaml_string = trainer % train_params
    train_obj = yaml_parse.load(yaml_string)

    pretrained_model_path = params.get('model', None)
    if pretrained_model_path is not None:
        print 'loading pre trained model'
        pretrained_model = serial.load(pretrained_model_path)
        print 'loading done'
        train_obj.model.set_param_values(pretrained_model.get_param_values())

    if 'converge' in params:
        train_obj.algorithm.termination_criterion._criteria[
            0]._max_epochs = params.get('epochs', 100)
        # train_obj.extensions.append(MonitorBasedSaveBest('valid_y_misclass', 'best_model.pkl'))

    train_obj.setup()
    train_obj.model.monitor.on_channel_conflict = 'ignore'
    if 'converge' not in params:
        train_obj.algorithm.termination_criterion._criteria[0].initialize(
            train_obj.model)
    train_obj.main_loop(do_setup=False)

    if 'converge' not in params:
        original_misclass = read_channel(train_obj.model, misclass_channel)
    else:
        serial.save(params['save'], train_obj.model, on_overwrite='backup')
        original_misclass = float('nan')
    return float(original_misclass) * 50
Ejemplo n.º 51
0
def test_env_lhs():
    environ['TEST_VAR'] = 'a'
    loaded = load("${TEST_VAR}: 42")
    assert_(loaded['a'] == 42)
    del environ['TEST_VAR']
Ejemplo n.º 52
0
def test_env_rhs():
    environ['TEST_VAR'] = '10'
    loaded = load("a: ${TEST_VAR}")
    assert_(loaded['a'] == 10)
    del environ['TEST_VAR']
Ejemplo n.º 53
0
    import os
    from StringIO import StringIO
    from pylearn2.config import yaml_parse

    example1 = """
        !obj:yaml_tutorial.autoencoder.AutoEncoder {
           "nvis": 784,
           "nhid": 100,
           "iscale": 0.2,
        }
    """
    stream = StringIO()
    stream.write(example1)
    stream.seek(0)
    print 'Example 1: building basic auto-encoder.'
    model = yaml_parse.load(stream)
    print model
    stream.close()

    example2 = """
        !obj:yaml_tutorial.autoencoder.AutoEncoder {
           "nvis": &nvis 100,
           "nhid": *nvis,
        }
    """
    stream = StringIO()
    stream.write(example2)
    stream.seek(0)
    print 'Example 2: anchors and references.'
    model = yaml_parse.load(stream)
    print model
Ejemplo n.º 54
0
base = 'normalization'
hyperparams_list = [
    #                    OrderedDict([('track', 1), ('norm', 1), ('h0_mcn', 100.), ('y_mcn', 100.), ('wd', 0.0001), ('sr', 1.)]),
    #                    OrderedDict([('track', 1), ('norm', 1), ('h0_mcn', .4), ('y_mcn', 100.), ('wd', 0), ('sr', 1.)]),
    #                    OrderedDict([('track', 1), ('norm', 1), ('h0_mcn', .2), ('y_mcn', 100.), ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 1), ('h0_mcn', .1), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 1), ('h0_mcn', .05), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 0), ('h0_mcn', .4), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 0), ('h0_mcn', .2), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 0), ('h0_mcn', .1), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
    OrderedDict([('track', 1), ('norm', 0), ('h0_mcn', .05), ('y_mcn', 100.),
                 ('wd', 0), ('sr', 1.)]),
]

yaml_template = open(base + '_template.yaml', 'r').read()
results_path = base + '_results.txt'
for hyperparams in hyperparams_list:
    model_base = get_model_base(base, hyperparams)
    hyperparams.update({'save_base': model_base})
    yaml = yaml_template % (hyperparams)
    train = yaml_parse.load(yaml)
    train.main_loop()

    model_path = model_base + '_best.pkl'

    print_results(model_path, results_path)
Ejemplo n.º 55
0
    def __call__(self):
        """
        Runs the model on the monitoring dataset in order to add one
        data point to each of the channels.
        """

        # If the channels have changed at all, we need to recompile the theano
        # functions used to compute them
        if self._dirty:
            self.redo_theano()

        model = self.model
        datasets = self._datasets

        # Set all channels' val_shared to 0
        self.begin_record_entry()

        for d, i, b, n, a, sd, ne in safe_izip(datasets, self._iteration_mode,
                                               self._batch_size,
                                               self._num_batches, self.accum,
                                               self._rng_seed,
                                               self.num_examples):
            if isinstance(d, basestring):
                d = yaml_parse.load(d)
                raise NotImplementedError()
                # need to put d back into self._datasets
            myiterator = d.iterator(mode=i,
                                    batch_size=b,
                                    num_batches=n,
                                    topo=self.topo,
                                    targets=self.require_label,
                                    rng=sd)

            actual_ne = 0
            for X in myiterator:
                if self.require_label:
                    X, y = X
                    self.run_prereqs(X, y, d)
                    a(X, y)
                else:
                    self.run_prereqs(X, None, d)
                    a(X)
                if X.ndim == 2:
                    actual_batch_size = X.shape[0]
                else:
                    actual_batch_size = X.shape[d.get_topo_batch_axis()]
                actual_ne += actual_batch_size
            # end for X
            if actual_ne != ne:
                raise RuntimeError(
                    "At compile time, your iterator said it had " + str(ne) +
                    " examples total, but at runtime it gave us " +
                    str(actual_ne) + ".")
        # end for d

        log.info("Monitoring step:")
        log.info("\tEpochs seen: %d" % self._epochs_seen)
        log.info("\tBatches seen: %d" % self._num_batches_seen)
        log.info("\tExamples seen: %d" % self._examples_seen)
        t = time.time() - self.t0
        for channel_name in sorted(self.channels.keys(),
                                   key=number_aware_alphabetical_key):
            channel = self.channels[channel_name]
            channel.time_record.append(t)
            channel.batch_record.append(self._num_batches_seen)
            channel.example_record.append(self._examples_seen)
            channel.epoch_record.append(self._epochs_seen)
            val = channel.val_shared.get_value()
            channel.val_record.append(val)
            # TODO: use logging infrastructure so that user can configure
            # formatting
            if abs(val) < 1e4:
                val_str = str(val)
            else:
                val_str = '%.3e' % val

            log.info("\t%s: %s" % (channel_name, val_str))
Ejemplo n.º 56
0
def test_maxout_basic():

    # Tests that we can load a densely connected maxout model
    # and train it for a few epochs (without saving) on a dummy
    # dataset-- tiny model and dataset

    yaml_string = """
    !obj:pylearn2.train.Train {
        dataset: &train !obj:pylearn2.testing.datasets.random_one_hot_dense_d\
esign_matrix {
            rng: !obj:numpy.random.RandomState { seed: [2013, 3, 16] },
            num_examples: 12,
            dim: 2,
            num_classes: 10
        },
        model: !obj:pylearn2.models.mlp.MLP {
            layers: [
                     !obj:pylearn2.models.maxout.Maxout {
                         layer_name: 'h0',
                         num_units: 3,
                         num_pieces: 2,
                         irange: .005,
                         max_col_norm: 1.9365,
                     },
                     !obj:pylearn2.models.maxout.Maxout {
                         layer_name: 'h1',
                         num_units: 2,
                         num_pieces: 3,
                         irange: .005,
                         max_col_norm: 1.9365,
                     },
                     !obj:pylearn2.models.mlp.Softmax {
                         max_col_norm: 1.9365,
                         layer_name: 'y',
                         n_classes: 10,
                         irange: .005
                     }
                    ],
            nvis: 2,
        },
        algorithm: !obj:pylearn2.training_algorithms.sgd.SGD {
            batch_size: 6,
            learning_rate: .1,
            init_momentum: .5,
            monitoring_dataset:
                {
                    'train' : *train
                },
            cost: !obj:pylearn2.costs.mlp.dropout.Dropout {
                input_include_probs: { 'h0' : .8 },
                input_scales: { 'h0': 1. }
            },
            termination_criterion: !obj:pylearn2.termination_criteria.EpochCo\
unter {
                max_epochs: 3,
            },
            update_callbacks: !obj:pylearn2.training_algorithms.sgd.Exponenti\
alDecay {
                decay_factor: 1.000004,
                min_lr: .000001
            }
        },
        extensions: [
            !obj:pylearn2.training_algorithms.sgd.MomentumAdjustor {
                start: 1,
                saturate: 250,
                final_momentum: .7
            }
        ],
    }
    """

    train = yaml_parse.load(yaml_string)

    train.main_loop()
Ejemplo n.º 57
0
from pylearn2.utils import serial
import sys

_, model_path = sys.argv
model = serial.load(model_path)
from pylearn2.gui.patch_viewer import make_viewer

space = model.generator.get_output_space()
from pylearn2.space import VectorSpace
from pylearn2.config import yaml_parse
import numpy as np

match_train = True
if match_train:
    dataset = yaml_parse.load(model.dataset_yaml_src)

grid_shape = None

nvis = model.generator.mlp.input_space.get_total_dimension()
endpoints = 50
steps_per_point = 100

rng = np.random.RandomState([2014, 6, 11])


def make_Z():
    start = rng.randn(nvis)
    idx = 0
    rval = np.zeros((endpoints * steps_per_point, nvis))
    for j in xrange(endpoints):
        stop = rng.randn(nvis)
Ejemplo n.º 58
0
def train_convolutional_network():
    train = """
!obj:pylearn2.train.Train {
    dataset: &train !obj:pylearn2.datasets.mnist.MNIST {
        which_set: 'train',
        one_hot: 1,
        start: 0,
        stop: 1000
    },
    model: !obj:pylearn2.models.mlp.MLP {
        batch_size: 100,
        input_space: !obj:pylearn2.space.Conv2DSpace {
            shape: [28, 28],
            num_channels: 1
        },
        layers: [ !obj:pylearn2.models.mlp.ConvSigmoid {
                     layer_name: 'h0',
                     output_channels: 8,
                     irange: .05,
                     kernel_shape: [6, 6],
                     pool_shape: [2, 2],
                     pool_stride: [1, 1],
                     max_kernel_norm: 1.9365
                 }, !obj:pylearn2.models.mlp.ConvSigmoid {
                     layer_name: 'h1',
                     output_channels: 8,
                     irange: .05,
                     kernel_shape: [5, 5],
                     pool_shape: [2, 2],
                     pool_stride: [1, 1],
                     max_kernel_norm: 1.9365
                 }, !obj:pylearn2.models.mlp.Softmax {
                     max_col_norm: 1.9365,
                     layer_name: 'y',
                     n_classes: 10,
                     istdev: .05
                 }
                ],
    },
    algorithm: !obj:pylearn2.training_algorithms.sgd.SGD {
        batch_size: 100,
        learning_rate: .1,
        init_momentum: .5,
        monitoring_dataset:
            {
                'train' : *train,
                'valid' : !obj:pylearn2.datasets.mnist.MNIST {
                              which_set: 'train',
                              one_hot: 1,
                              start: 50000,
                              stop:  50100
                          },
                'test'  : !obj:pylearn2.datasets.mnist.MNIST {
                              which_set: 'test',
                              one_hot: 1,
                              stop: 100
                          }
            },
        cost: !obj:pylearn2.costs.cost.SumOfCosts { costs: [
            !obj:pylearn2.costs.cost.MethodCost {
                method: 'cost_from_X'
            }, !obj:pylearn2.costs.mlp.WeightDecay {
                coeffs: [ .00005, .00005, .00005 ]
            }
            ]
        },
        termination_criterion: !obj:pylearn2.termination_criteria.MonitorBased {
            channel_name: "valid_y_misclass",
            prop_decrease: 0.50,
            N: 10
        }
    },
    extensions:
        [ !obj:pylearn2.train_extensions.best_params.MonitorBasedSaveBest {
             channel_name: 'valid_y_misclass',
             save_path: "convolutional_network_best.pkl"
        }, !obj:pylearn2.training_algorithms.sgd.MomentumAdjustor {
            start: 1,
            saturate: 10,
            final_momentum: .99
        }
    ]
}
"""

    from pylearn2.config import yaml_parse
    train = yaml_parse.load(train)
    train.main_loop()
Ejemplo n.º 59
0
    iterator = dataset.iterator(batch_size=8, mode='sequential',
                                data_specs=model.cost_from_X_data_specs())
    errs = 0.0
    for np_X, np_Y in iterator:
        errs += f(np_X, np_Y)
    return errs / dataset.get_num_examples()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("model_path", help="path to the pickled model")
    args = parser.parse_args()

    model = serial.load(args.model_path)

    train_proxy = yaml_parse.load(model.dataset_yaml_src, instantiate=False)
    train_proxy.keywords['start'] = 0
    train_proxy.keywords['stop'] = 20000
    train_set = yaml_parse._instantiate(train_proxy)
    print "Train error rate is " + str(compute_error(model, train_set))

    valid_proxy = yaml_parse.load(model.dataset_yaml_src, instantiate=False)
    valid_proxy.keywords['start'] = 20000
    valid_proxy.keywords['stop'] = 22500
    valid_set = yaml_parse._instantiate(valid_proxy)
    print "Valid error rate is " + str(compute_error(model, valid_set))

    test_proxy = yaml_parse.load(model.dataset_yaml_src, instantiate=False)
    test_proxy.keywords['start'] = 22500
    test_proxy.keywords['stop'] = 25000
    test_set = yaml_parse._instantiate(test_proxy)
def main(args=None):
    """
    args is the list of arguments that will be passed to the option parser.
    The default (None) means use sys.argv[1:].
    """
    # parser = OptionParser()
    # parser.add_option("-d", "--data", dest="dataset", default="toy",
    #                   help="specify the dataset, either cifar10, mnist or toy")
    # (options, args) = parser.parse_args(args=args)
    #
    # if options.dataset == 'toy':
    #     trainset, testset = get_dataset_toy()
    #     n_output = 2
    # elif options.dataset == 'cifar10':
    #     trainset, testset, = get_dataset_cifar10()
    #     n_output = 10
    #
    # elif options.dataset == 'mnist':
    #     trainset, testset, = get_dataset_mnist()
    #     n_output = 10
    #
    # else:
    #     NotImplementedError()
    trainset = get_dataset_timitCons()
    n_output = 32

    design_matrix = trainset.get_design_matrix()
    n_input = design_matrix.shape[1]

    # build layers
    layers = []
    structure = [[n_input, 500], [500, 500], [500, 500], [500, n_output]]
    # layer 0: gaussianRBM
    layers.append(get_grbm(structure[0]))
    # # layer 1: denoising AE
    # layers.append(get_denoising_autoencoder(structure[1]))
    # # layer 2: AE
    # layers.append(get_autoencoder(structure[2]))
    # # layer 3: logistic regression used in supervised training
    # layers.append(get_logistic_regressor(structure[3]))

    # layer 1: gaussianRBM
    layers.append(get_grbm(structure[1]))
    # layer 2: gaussianRBM
    layers.append(get_grbm(structure[2]))
    # layer 3: logistic regression used in supervised training
    # layers.append(get_logistic_regressor(structure[3]))
    layers.append(get_mlp_softmax(structure[3]))

    # construct training sets for different layers
    trainset = [
        trainset,
        TransformerDataset(raw=trainset, transformer=layers[0]),
        TransformerDataset(raw=trainset,
                           transformer=StackedBlocks(layers[0:2])),
        TransformerDataset(raw=trainset,
                           transformer=StackedBlocks(layers[0:3]))
    ]

    # construct layer trainers
    layer_trainers = []
    layer_trainers.append(get_layer_trainer_sgd_rbm0(layers[0], trainset[0]))
    # layer_trainers.append(get_layer_trainer_sgd_autoencoder(layers[1], trainset[1]))
    # layer_trainers.append(get_layer_trainer_sgd_autoencoder(layers[2], trainset[2]))
    layer_trainers.append(get_layer_trainer_sgd_rbm1(layers[1], trainset[1]))
    layer_trainers.append(get_layer_trainer_sgd_rbm2(layers[2], trainset[2]))
    # layer_trainers.append(get_layer_trainer_logistic(layers[3], trainset[3]))
    layer_trainers.append(get_layer_trainer_softmax(layers[3], trainset[3]))

    # unsupervised pretraining
    for i, layer_trainer in enumerate(layer_trainers[0:3]):
        print('-----------------------------------')
        print(' Unsupervised training layer %d, %s' % (i, layers[i].__class__))
        print('-----------------------------------')
        layer_trainer.main_loop()

    print('\n')
    print('------------------------------------------------------')
    print(' Unsupervised training done! Start supervised training...')
    print('------------------------------------------------------')
    print('\n')

    # supervised training
    # layer_trainers[-1].main_loop()
    layer1_yaml = open('MachineLearning.yaml', 'r').read()
    train = yaml_parse.load(layer1_yaml)
    train.main_loop()