Ejemplo n.º 1
0
    def __init__(self):
        Dataset.__init__(self)

        # Initialize the additional containers
        self.vx = None
        self.file_ids = None
Ejemplo n.º 2
0
    def run(self):
        ###### Create the datasets

        training_data_path = self.data_path + "train.h5"
        testing_data_path = self.data_path + "test.h5"

        # If files don't already exist, create them
        if not os.path.isfile(training_data_path):
            transform_mnist_to_h5()

        prop_validation = 0.3  # Percentage of the training dataset that is used for validation (early stopping)
        ds_training = Dataset.create_and_read(training_data_path)
        ds_validation, ds_training = ds_training.split_dataset_proportions(
            [prop_validation, 1 - prop_validation])
        ds_testing = Dataset.create_and_read(testing_data_path)

        # Few stats about the targets
        analyse_classes(np.argmax(ds_training.outputs, axis=1),
                        "Training data")

        # Scale the data
        s = Scaler([slice(None, None)])
        s.compute_parameters(ds_training.inputs)
        s.scale(ds_training.inputs)
        s.scale(ds_validation.inputs)
        s.scale(ds_testing.inputs)

        ###### Create the network

        net = NetworkMNIST()
        net.init(28, 28, 10)
        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostNegLL()

        # Learning update
        learning_rate = 0.13
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        err_training = MonitorErrorRate(1, "Training", ds_training)
        err_testing = MonitorErrorRate(1, "Testing", ds_testing)
        err_validation = MonitorErrorRate(1, "Validation", ds_validation)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(300)
        early_stopping = EarlyStopping(err_validation)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 200
        t = Trainer(net, cost_function, params_selector,
                    [max_epoch, early_stopping], lr_update, ds_training,
                    batch_size, [err_training, err_testing, err_validation])

        ###### Train the network

        t.train()

        ###### Plot the records

        save_records_plot(self.path,
                          [err_training, err_testing, err_validation],
                          "errors", t.n_train_batches)

        ###### Save the network

        net.save_parameters(self.path + "net.net")
Ejemplo n.º 3
0
    def run(self):
        ###### Create the datasets

        training_data_path = self.data_path + "train.h5"
        testing_data_path = self.data_path + "test.h5"

        # If files don't already exist, create them
        if not os.path.isfile(training_data_path):
            transform_mnist_to_h5()

        prop_validation = 0.15  # Percentage of the training dataset that is used for validation (early stopping)
        ds_training = Dataset.create_and_read(training_data_path)
        ds_validation, ds_training = ds_training.split_dataset_proportions([prop_validation, 1-prop_validation])
        ds_testing = Dataset.create_and_read(testing_data_path)
        ds_training.outputs = ds_training.inputs
        ds_validation.outputs = ds_validation.inputs
        ds_testing.outputs = ds_testing.inputs

        # Scale the data
        # s = Scaler([slice(None, None)])
        # s.compute_parameters(ds_training.inputs)
        # s.scale(ds_training.inputs)
        # s.scale(ds_validation.inputs)
        # s.scale(ds_testing.inputs)

        ###### Create the network

        # net = NetworkMNIST()
        net = AutoEncoder()
        net.init([28**2, 256, 28**2], dropout=True, dropout_p=[0.5], neuron_function=NeuronSigmoid())
        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostMSE()

        # Learning update
        learning_rate = 0.01
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        err_training = MonitorMSE(1, "Training", ds_training)
        err_testing = MonitorMSE(1, "Testing", ds_testing)
        err_validation = MonitorMSE(1, "Validation", ds_validation)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(50)
        early_stopping = EarlyStopping(err_validation)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 20
        t = Trainer(net, cost_function, params_selector, [max_epoch, early_stopping],
                    lr_update, ds_training, batch_size,
                    [err_training, err_testing, err_validation])

        ###### Train the network

        t.train()

        ###### Plot the records

        save_records_plot(self.path, [err_training, err_testing, err_validation], "errors", t.n_train_batches)

        ###### Save the network

        net.save_parameters(self.path + "netdrop.net")
Ejemplo n.º 4
0
    def run(self):
        ###### Create the datasets

        training_data_path = self.data_path + "train.h5"
        testing_data_path = self.data_path + "test.h5"

        # If files don't already exist, create them
        if not os.path.isfile(training_data_path):
            transform_mnist_to_h5()

        prop_validation = 0.15  # Percentage of the training dataset that is used for validation (early stopping)
        ds_training = Dataset.create_and_read(training_data_path)
        ds_validation, ds_training = ds_training.split_dataset_proportions(
            [prop_validation, 1 - prop_validation])
        ds_testing = Dataset.create_and_read(testing_data_path)
        ds_training.outputs = ds_training.inputs
        ds_validation.outputs = ds_validation.inputs
        ds_testing.outputs = ds_testing.inputs

        # Scale the data
        # s = Scaler([slice(None, None)])
        # s.compute_parameters(ds_training.inputs)
        # s.scale(ds_training.inputs)
        # s.scale(ds_validation.inputs)
        # s.scale(ds_testing.inputs)

        ###### Create the network

        # net = NetworkMNIST()
        net = AutoEncoder()
        net.init([28**2, 256, 28**2],
                 dropout=True,
                 dropout_p=[0.5],
                 neuron_function=NeuronSigmoid())
        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostMSE()

        # Learning update
        learning_rate = 0.01
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        err_training = MonitorMSE(1, "Training", ds_training)
        err_testing = MonitorMSE(1, "Testing", ds_testing)
        err_validation = MonitorMSE(1, "Validation", ds_validation)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(50)
        early_stopping = EarlyStopping(err_validation)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 20
        t = Trainer(net, cost_function, params_selector,
                    [max_epoch, early_stopping], lr_update, ds_training,
                    batch_size, [err_training, err_testing, err_validation])

        ###### Train the network

        t.train()

        ###### Plot the records

        save_records_plot(self.path,
                          [err_training, err_testing, err_validation],
                          "errors", t.n_train_batches)

        ###### Save the network

        net.save_parameters(self.path + "netdrop.net")
Ejemplo n.º 5
0
from spynet.models.network import *
from spynet.models.neuron_type import *
from spynet.data.dataset import Dataset
from spynet.utils.utilities import open_h5file, tile_raster_images, MSE


if __name__ == '__main__':

    mode = "drop"

    experiment_path = "./experiments/mnist_example/"
    data_path = "./datasets/mnist/"

    testing_data_path = data_path + "test.h5"
    ds_testing = Dataset.create_and_read(testing_data_path)

    # Load the network
    net = AutoEncoder()
    net.init([28**2, 256, 28**2], dropout=True, dropout_p=[0.5], neuron_function=NeuronSigmoid())
    net.load_parameters(open_h5file(experiment_path + "netdrop.net"))

    i = ds_testing.inputs[0:10,:]
    e = net.predict(i, 10)

    print ""
    print MSE(e,i)

    image = PIL.Image.fromarray(tile_raster_images(X=net.ls_layers[0].ls_layer_blocks[0].w.get_value(borrow=True).T,
                 img_shape=(28, 28), tile_shape=(16, 16),
                 tile_spacing=(1, 1)))
    def __init__(self):
        Dataset.__init__(self)

        # Initialize the additional containers
        self.vx = None
        self.file_ids = None
Ejemplo n.º 7
0
import PIL

from spynet.models.network import *
from spynet.models.neuron_type import *
from spynet.data.dataset import Dataset
from spynet.utils.utilities import open_h5file, tile_raster_images, MSE

if __name__ == '__main__':

    mode = "drop"

    experiment_path = "./experiments/mnist_example/"
    data_path = "./datasets/mnist/"

    testing_data_path = data_path + "test.h5"
    ds_testing = Dataset.create_and_read(testing_data_path)

    # Load the network
    net = AutoEncoder()
    net.init([28**2, 256, 28**2],
             dropout=True,
             dropout_p=[0.5],
             neuron_function=NeuronSigmoid())
    net.load_parameters(open_h5file(experiment_path + "netdrop.net"))

    i = ds_testing.inputs[0:10, :]
    e = net.predict(i, 10)

    print ""
    print MSE(e, i)
Ejemplo n.º 8
0
    def run(self):
        ###### Create the datasets

        training_data_path = self.data_path + "train.h5"
        testing_data_path = self.data_path + "test.h5"

        # If files don't already exist, create them
        if not os.path.isfile(training_data_path):
            transform_mnist_to_h5()

        prop_validation = 0.3  # Percentage of the training dataset that is used for validation (early stopping)
        ds_training = Dataset.create_and_read(training_data_path)
        ds_validation, ds_training = ds_training.split_dataset_proportions([prop_validation, 1-prop_validation])
        ds_testing = Dataset.create_and_read(testing_data_path)

        # Few stats about the targets
        analyse_classes(np.argmax(ds_training.outputs, axis=1), "Training data")

        # Scale the data
        s = Scaler([slice(None, None)])
        s.compute_parameters(ds_training.inputs)
        s.scale(ds_training.inputs)
        s.scale(ds_validation.inputs)
        s.scale(ds_testing.inputs)

        ###### Create the network

        net = NetworkMNIST()
        net.init(28, 28, 10)
        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostNegLL()

        # Learning update
        learning_rate = 0.13
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        err_training = MonitorErrorRate(1, "Training", ds_training)
        err_testing = MonitorErrorRate(1, "Testing", ds_testing)
        err_validation = MonitorErrorRate(1, "Validation", ds_validation)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(300)
        early_stopping = EarlyStopping(err_validation)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 200
        t = Trainer(net, cost_function, params_selector, [max_epoch, early_stopping],
                    lr_update, ds_training, batch_size,
                    [err_training, err_testing, err_validation])

        ###### Train the network

        t.train()

        ###### Plot the records

        save_records_plot(self.path, [err_training, err_testing, err_validation], "errors", t.n_train_batches)

        ###### Save the network

        net.save_parameters(self.path + "net.net")