Beispiel #1
0
def run_dae():
    ########################################
    # Initialization things with arguments #
    ########################################
    config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {
        "outdir": 'outputs/dae/mnist/',
        "input_size": 28 * 28,
        "tied_weights": True
    }
    dae = DenoisingAutoencoder(**config)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(model=dae, dataset=mnist, epochs=100)
    optimizer.train()

    # Save some reconstruction output images
    n_examples = 100
    test_xs = mnist.test_inputs[:n_examples]
    dae.create_reconstruction_image(test_xs)

    del dae, mnist
 def setUp(self):
     # configure the root logger
     config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the mnist dataset
     self.cifar = CIFAR10(one_hot=True, path='../../../datasets/cifar-10-batches-py/')
 def setUp(self):
     # configure the root logger
     config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the mnist dataset
     self.mnist = MNIST(path="../../../datasets/mnist.pkl.gz", binary=False, concat_train_valid=True)
def run_dae():
    ########################################
    # Initialization things with arguments #
    ########################################
    config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {
        "outdir": 'outputs/dae/mnist/',
        "input_size": 28*28,
        "tied_weights": True
    }
    dae = DenoisingAutoencoder(**config)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(model=dae, dataset=mnist, epochs=100)
    optimizer.train()

    # Save some reconstruction output images
    n_examples = 100
    test_xs = mnist.test_inputs[:n_examples]
    dae.create_reconstruction_image(test_xs)

    del dae, mnist
Beispiel #5
0
 def setUp(self):
     # configure the root logger
     config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the mnist dataset
     self.mnist = MNIST(path="../../../datasets/mnist.pkl.gz", binary=False, concat_train_valid=True)
Beispiel #6
0
def run_dae():
    ########################################
    # Initialization things with arguments #
    ########################################
    config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {
        "outdir": 'outputs/dae/mnist/',
        "input_size": 28*28,
        "tied_weights": True
    }
    dae = DenoisingAutoencoder(**config)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(dae, mnist)
    optimizer.train()

    # Save some reconstruction output images
    import opendeep.data.dataset as datasets
    n_examples = 100
    test_xs, _ = mnist.getSubset(subset=datasets.TEST)
    test_xs = test_xs[:n_examples].eval()
    dae.create_reconstruction_image(test_xs)
Beispiel #7
0
def run_dae():
    ########################################
    # Initialization things with arguments #
    ########################################
    config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {
        "outdir": 'outputs/dae/mnist/',
        "input_size": 28 * 28,
        "tied_weights": True
    }
    dae = DenoisingAutoencoder(**config)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(dae, mnist)
    optimizer.train()

    # Save some reconstruction output images
    import opendeep.data.dataset as datasets
    n_examples = 100
    test_xs, _ = mnist.getSubset(subset=datasets.TEST)
    test_xs = test_xs[:n_examples].eval()
    dae.create_reconstruction_image(test_xs)
Beispiel #8
0
 def setUp(self):
     # configure the root logger
     config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the mnist dataset
     self.cifar = CIFAR10(one_hot=True,
                          path='../../../datasets/cifar-10-batches-py/')
Beispiel #9
0
 def setUp(self):
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # create dataset
     train = numpy.array([[1, 2], [4, 5]])
     valid = numpy.array([[2, 3], [5, 6], [8, 9]])
     test  = numpy.array([[3, 4], [6, 7], [1, 2], [9, 0]])
     self.dataset = MemoryDataset(train_X=train, valid_X=valid, test_X=test)
Beispiel #10
0
 def setUp(self):
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # create dataset
     train = numpy.array([[1, 2], [4, 5]])
     valid = numpy.array([[2, 3], [5, 6], [8, 9]])
     test = numpy.array([[3, 4], [6, 7], [1, 2], [9, 0]])
     self.dataset = MemoryDataset(train_X=train, valid_X=valid, test_X=test)
Beispiel #11
0
 def setUp(self):
     print("setting up!")
     config_root_logger()
     # get the muse dataset
     self.muse = MuseData(path='../../../datasets/MuseData')
     # get the jsb dataset
     self.jsb = JSBChorales(path='../../../datasets/JSB Chorales')
     # get nottingham dataset
     self.nottingham = Nottingham(path='../../../datasets/Nottingham')
     # get the piano-midi-de dataset
     self.piano = PianoMidiDe(path='../../../datasets/Piano-midi.de')
Beispiel #12
0
 def setUp(self):
     print("setting up!")
     config_root_logger()
     # get the muse dataset
     self.muse = MuseData(path='../../../datasets/MuseData')
     # get the jsb dataset
     self.jsb = JSBChorales(path='../../../datasets/JSB Chorales')
     # get nottingham dataset
     self.nottingham = Nottingham(path='../../../datasets/Nottingham')
     # get the piano-midi-de dataset
     self.piano = PianoMidiDe(path='../../../datasets/Piano-midi.de')
Beispiel #13
0
 def setUp(self):
     print "setting up!"
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the muse dataset
     self.muse = MuseData()
     # get the jsb dataset
     # self.jsb = JSBChorales()
     # get nottingham dataset
     self.nottingham = Nottingham()
Beispiel #14
0
 def setUp(self):
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the muse dataset
     self.muse = MuseData()
     # get the jsb dataset
     self.jsb = JSBChorales()
     # get nottingham dataset
     self.nottingham = Nottingham()
     # get the piano-midi-de dataset
     self.piano = PianoMidiDe()
Beispiel #15
0
 def setUp(self):
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # get the mnist dataset
     self.mnist = MNIST(binary=False)
     # instantiate the sequential iterator
     self.sequentialIterator = SequentialIterator(self.mnist, dataset.TRAIN,
                                                  255, 255)
     # instantiate the random iterator
     self.randomIterator = RandomIterator(self.mnist, dataset.TRAIN, 255,
                                          255)
Beispiel #16
0
 def setUp(self):
     # change sys.stdout to StringIO for the duration of the test to test console output
     self.saved_stdout = sys.stdout
     self.out = StringIO()
     sys.stdout = self.out
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # set the paths for the logs (comes from reading logging_config.json)
     self.error_path = '../logs/error/errors.log'
     self.info_path = '../logs/info/info.log'
     self.paths = [self.error_path, self.info_path]
Beispiel #17
0
 def setUp(self):
     # change sys.stdout to StringIO for the duration of the test to test console output
     self.saved_stdout = sys.stdout
     self.out = StringIO()
     sys.stdout = self.out
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # set the paths for the logs (comes from reading logging_config.json)
     self.error_path = '../logs/error/errors.log'
     self.info_path  = '../logs/info/info.log'
     self.paths = [self.error_path, self.info_path]
Beispiel #18
0
 def setUp(self):
     self.current_dir = os.getcwd()
     # go into this file's directory
     abspath = os.path.realpath(__file__)
     dname = os.path.dirname(abspath)
     os.chdir(dname)
     # change sys.stdout to StringIO for the duration of the test to test console output
     self.saved_stdout = sys.stdout
     self.out = StringIO()
     sys.stdout = self.out
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # set the paths for the logs (comes from reading logging_config.json)
     self.error_path = '../logs/error/errors.log'
     self.info_path  = '../logs/info/info.log'
     self.paths = [self.error_path, self.info_path]
Beispiel #19
0
 def setUp(self):
     self.current_dir = os.getcwd()
     # go into this file's directory
     abspath = os.path.realpath(__file__)
     dname = os.path.dirname(abspath)
     os.chdir(dname)
     # change sys.stdout to StringIO for the duration of the test to test console output
     self.saved_stdout = sys.stdout
     self.out = StringIO()
     sys.stdout = self.out
     # configure the root logger
     logger.config_root_logger()
     # get a logger for this session
     self.log = logging.getLogger(__name__)
     # set the paths for the logs (comes from reading logging_config.json)
     self.error_path = '../logs/error/errors.log'
     self.info_path = '../logs/info/info.log'
     self.paths = [self.error_path, self.info_path]
Beispiel #20
0
def run_dae():
    ########################################
    # Initialization things with arguments #
    ########################################
    config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {"output_path": '../../../../outputs/dae/mnist/'}
    dae = DenoisingAutoencoder(config=config, dataset=mnist)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(dae, mnist)
    optimizer.train()

    # Save some reconstruction output images
    import opendeep.data.dataset as datasets
    n_examples = 100
    test_xs = mnist.getDataByIndices(indices=range(n_examples), subset=datasets.TEST)
    dae.create_reconstruction_image(test_xs)
Beispiel #21
0
def main():
    ########################################
    # Initialization things with arguments #
    ########################################
    logger.config_root_logger()
    log.info("Creating a new DAE")

    mnist = MNIST()
    config = {"output_path": '../../../outputs/dae/mnist/'}
    dae = DenoisingAutoencoder(config=config, dataset=mnist)

    # # Load initial weights and biases from file
    # params_to_load = 'dae_params.pkl'
    # dae.load_params(params_to_load)

    optimizer = AdaDelta(dae, mnist)
    optimizer.train()

    # Save some reconstruction output images
    import opendeep.data.dataset as datasets
    n_examples = 100
    test_xs = mnist.getDataByIndices(indices=range(n_examples), subset=datasets.TEST)
    dae.create_reconstruction_image(test_xs)
Beispiel #22
0
              activation='softmax',
              hidden_activation='relu',
              mrg=RNG_MRG.MRG_RandomStreams(1),
              weights_init='uniform',
              weights_interval='montreal',
              bias_init=0.0,
              r_weights_init='identity',
              r_bias_init=0.0,
              cost_function='nll',
              cost_args=None,
              noise='dropout',
              noise_level=.7,
              noise_decay='exponential',
              noise_decay_amount=.99,
              direction='forward')

    cost_monitor = Monitor("cost", rnn.get_train_cost(), train=False, valid=True, test=True)

    optimizer = RMSProp(model=rnn, dataset=data,
                        grad_clip=5., hard_clip=False,
                        learning_rate=2e-3, lr_decay='exponential', lr_decay_factor=0.97,
                        decay=0.95, batch_size=50, epochs=50)
    # optimizer = AdaDelta(model=gsn, dataset=mnist, n_epoch=200, batch_size=100, learning_rate=1e-6)
    optimizer.train(monitor_channels=cost_monitor)



if __name__ == '__main__':
    config_root_logger()
    main()
Beispiel #23
0
def main():
    ########################################
    # Initialization things with arguments #
    ########################################
    # use these arguments to get results from paper referenced above
    _train_args = {"epochs": 1000,  # maximum number of times to run through the dataset
                   "batch_size": 100,  # number of examples to process in parallel (minibatch)
                   "min_batch_size": 1,  # the minimum number of examples for a batch to be considered
                   "save_freq": 1,  # how many epochs between saving parameters
                   "stop_threshold": .9995,  # multiplier for how much the train cost to improve to not stop early
                   "stop_patience": 500,  # how many epochs to wait to see if the threshold has been reached
                   "learning_rate": .25,  # initial learning rate for SGD
                   "lr_decay": 'exponential',  # the decay function to use for the learning rate parameter
                   "lr_decay_factor": .995,  # by how much to decay the learning rate each epoch
                   "momentum": 0.5,  # the parameter momentum amount
                   'momentum_decay': False,  # how to decay the momentum each epoch (if applicable)
                   'momentum_factor': 0,  # by how much to decay the momentum (in this case not at all)
                   'nesterov_momentum': False,  # whether to use nesterov momentum update (accelerated momentum)
    }

    config_root_logger()
    log.info("Creating a new GSN")

    mnist = MNIST(concat_train_valid=True)
    gsn = GSN(layers=2,
              walkbacks=4,
              hidden_size=1500,
              visible_activation='sigmoid',
              hidden_activation='tanh',
              input_size=28*28,
              tied_weights=True,
              hidden_add_noise_sigma=2,
              input_salt_and_pepper=0.4,
              outdir='outputs/test_gsn/',
              vis_init=False,
              noiseless_h1=True,
              input_sampling=True,
              weights_init='uniform',
              weights_interval='montreal',
              bias_init=0,
              cost_function='binary_crossentropy')

    recon_cost_channel = MonitorsChannel(name='cost')
    recon_cost_channel.add(Monitor('recon_cost', gsn.get_monitors()['recon_cost'], test=True))
    recon_cost_channel.add(Monitor('noisy_recon_cost', gsn.get_monitors()['noisy_recon_cost'], test=True))

    # Load initial weights and biases from file
    # params_to_load = '../../../outputs/gsn/mnist/trained_epoch_395.pkl'
    # gsn.load_params(params_to_load)

    optimizer = SGD(model=gsn, dataset=mnist, **_train_args)
    # optimizer = AdaDelta(model=gsn, dataset=mnist, epochs=200, batch_size=100, learning_rate=1e-6)
    optimizer.train(monitor_channels=recon_cost_channel)

    # Save some reconstruction output images
    n_examples = 100
    xs_test = mnist.test_inputs[:n_examples]
    noisy_xs_test = gsn.f_noise(xs_test)
    reconstructed = gsn.run(noisy_xs_test)
    # Concatenate stuff
    stacked = numpy.vstack(
        [numpy.vstack([xs_test[i * 10: (i + 1) * 10],
                       noisy_xs_test[i * 10: (i + 1) * 10],
                       reconstructed[i * 10: (i + 1) * 10]])
         for i in range(10)])
    number_reconstruction = PIL.Image.fromarray(
        tile_raster_images(stacked, (gsn.image_height, gsn.image_width), (10, 30))
    )

    number_reconstruction.save(gsn.outdir + 'reconstruction.png')
    log.info("saved output image!")

    # Construct image from the weight matrix
    image = PIL.Image.fromarray(
        tile_raster_images(
            X=gsn.weights_list[0].get_value(borrow=True).T,
            img_shape=(28, 28),
            tile_shape=closest_to_square_factors(gsn.hidden_size),
            tile_spacing=(1, 1)
        )
    )
    image.save(gsn.outdir + "gsn_mnist_weights.png")
Beispiel #24
0
    def get_params(self):
        return self.params

    def get_train_cost(self):
        return self.train_cost

    def get_outputs(self):
        return self.recon_predict


if __name__ == '__main__':
    # set up the logging environment to display outputs (optional)
    # although this is recommended over print statements everywhere
    import logging
    from opendeep.log.logger import config_root_logger
    config_root_logger()
    log = logging.getLogger(__name__)
    log.info("Creating a Denoising Autoencoder!")

    # import the dataset and optimizer to use
    from opendeep.data.dataset import TEST
    from opendeep.data.standard_datasets.image.mnist import MNIST
    from opendeep.optimization.adadelta import AdaDelta

    # grab the MNIST dataset
    mnist = MNIST()

    # create your shiny new DAE
    dae = DenoisingAutoencoder()

    # make an optimizer to train it (AdaDelta is a good default)
Beispiel #25
0
from opendeep.models.single_layer.basic import SoftmaxLayer
# import the dataset and optimizer to use
from opendeep.data.dataset import TEST
from opendeep.data.standard_datasets.image.mnist import MNIST
from opendeep.optimization.adadelta import AdaDelta


if __name__ == '__main__':
    # set up the logging environment to display outputs (optional)
    # although this is recommended over print statements everywhere
    import logging
    import opendeep.log.logger as logger
    logger.config_root_logger()
    log = logging.getLogger(__name__)
    log.info("Creating softmax!")

    # grab the MNIST dataset
    mnist = MNIST()
    # create the softmax classifier
    s = SoftmaxLayer(input_size=28 * 28, output_size=10, out_as_probs=False)
    # make an optimizer to train it (AdaDelta is a good default)
    optimizer = AdaDelta(model=s, dataset=mnist, n_epoch=20)
    # perform training!
    optimizer.train()
    # test it on some images!
    test_data = mnist.getDataByIndices(indices=range(25), subset=TEST)
    # use the predict function!
    preds = s.predict(test_data)
    print '-------'
    print preds
    print mnist.getLabelsByIndices(indices=range(25), subset=TEST)
    def get_params(self):
        return self.params

    def get_train_cost(self):
        return self.train_cost

    def predict(self, input):
        return self.f_predict(input)


if __name__ == '__main__':
    # set up the logging environment to display outputs (optional)
    # although this is recommended over print statements everywhere
    import logging
    import opendeep.log.logger as logger
    logger.config_root_logger()
    log = logging.getLogger(__name__)
    log.info("Creating a Denoising Autoencoder!")

    # import the dataset and optimizer to use
    from opendeep.data.dataset import TEST
    from opendeep.data.standard_datasets.image.mnist import MNIST
    from opendeep.optimization.adadelta import AdaDelta

    # grab the MNIST dataset
    mnist = MNIST()

    # create your shiny new DAE
    dae = DenoisingAutoencoder()

    # make an optimizer to train it (AdaDelta is a good default)
Beispiel #27
0
def main():
    ########################################
    # Initialization things with arguments #
    ########################################
    # use these arguments to get results from paper referenced above
    _train_args = {"n_epoch": 1000,  # maximum number of times to run through the dataset
                   "batch_size": 100,  # number of examples to process in parallel (minibatch)
                   "minimum_batch_size": 1,  # the minimum number of examples for a batch to be considered
                   "save_frequency": 1,  # how many epochs between saving parameters
                   "early_stop_threshold": .9995,  # multiplier for how much the train cost to improve to not stop early
                   "early_stop_length": 500,  # how many epochs to wait to see if the threshold has been reached
                   "learning_rate": .25,  # initial learning rate for SGD
                   "lr_decay": 'exponential',  # the decay function to use for the learning rate parameter
                   "lr_factor": .995,  # by how much to decay the learning rate each epoch
                   "momentum": 0.5,  # the parameter momentum amount
                   'momentum_decay': False,  # how to decay the momentum each epoch (if applicable)
                   'momentum_factor': 0,  # by how much to decay the momentum (in this case not at all)
                   'nesterov_momentum': False,  # whether to use nesterov momentum update (accelerated momentum)
    }

    config_root_logger()
    log.info("Creating a new GSN")

    mnist = MNIST(concat_train_valid=True)
    gsn = GSN(layers=2,
              walkbacks=4,
              hidden_size=1500,
              visible_activation='sigmoid',
              hidden_activation='tanh',
              input_size=28*28,
              tied_weights=True,
              hidden_add_noise_sigma=2,
              input_salt_and_pepper=0.4,
              outdir='outputs/test_gsn/',
              vis_init=False,
              noiseless_h1=True,
              input_sampling=True,
              weights_init='uniform',
              weights_interval='montreal',
              bias_init=0,
              cost_function='binary_crossentropy')

    recon_cost_channel = MonitorsChannel(name='cost')
    recon_cost_channel.add(Monitor('recon_cost', gsn.get_monitors()['recon_cost'], test=True))
    recon_cost_channel.add(Monitor('noisy_recon_cost', gsn.get_monitors()['noisy_recon_cost'], test=True))

    # Load initial weights and biases from file
    # params_to_load = '../../../outputs/gsn/mnist/trained_epoch_395.pkl'
    # gsn.load_params(params_to_load)

    optimizer = SGD(model=gsn, dataset=mnist, **_train_args)
    # optimizer = AdaDelta(model=gsn, dataset=mnist, n_epoch=200, batch_size=100, learning_rate=1e-6)
    optimizer.train(monitor_channels=recon_cost_channel)

    # Save some reconstruction output images
    import opendeep.data.dataset as datasets
    n_examples = 100
    xs_test, _ = mnist.getSubset(datasets.TEST)
    xs_test = xs_test[:n_examples].eval()
    noisy_xs_test = gsn.f_noise(xs_test)
    reconstructed = gsn.run(noisy_xs_test)
    # Concatenate stuff
    stacked = numpy.vstack(
        [numpy.vstack([xs_test[i * 10: (i + 1) * 10],
                       noisy_xs_test[i * 10: (i + 1) * 10],
                       reconstructed[i * 10: (i + 1) * 10]])
         for i in range(10)])
    number_reconstruction = PIL.Image.fromarray(
        tile_raster_images(stacked, (gsn.image_height, gsn.image_width), (10, 30))
    )

    number_reconstruction.save(gsn.outdir + 'reconstruction.png')
    log.info("saved output image!")

    # Construct image from the weight matrix
    image = PIL.Image.fromarray(
        tile_raster_images(
            X=gsn.weights_list[0].get_value(borrow=True).T,
            img_shape=(28, 28),
            tile_shape=closest_to_square_factors(gsn.hidden_size),
            tile_spacing=(1, 1)
        )
    )
    image.save(gsn.outdir + "gsn_mnist_weights.png")