Exemplo n.º 1
0
def train():
    """
    This examples implements the variational autoencoder from the paper
    Auto-Encoding Variational Bayes by Diederik P Kingma, Max Welling, arXiv:1312.6114
    """

    # build dataset
    data = Mnist(batch_size=100, binary=False, train_valid_test_ratio=[5,1,1])
    # for autoencoder, the output will be equal to input
    data.set_train(X=data.get_train().X, y=data.get_train().X)
    data.set_valid(X=data.get_valid().X, y=data.get_valid().X)

    # build model
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(VariationalAutoencoder(input_dim=28*28, bottlenet_dim=200, z_dim=20))

    # build learning method
    learning_method = SGD(learning_rate=0.0001, momentum=0.9,
                              lr_decay_factor=0.9, decay_batch=10000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = SGVB_bin,
                               valid_cost = SGVB_bin,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 2
0
def train():

    data = Cifar10(batch_size=32, train_valid_test_ratio=[4,1,1])

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Convolution2D(input_channels=3, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=8, filters=16, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4,4), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(16*8*8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # Build Logger
    log = Log(experiment_name = 'cifar10_cnn',
              description = 'This is a tutorial',
              save_outputs = True, # log all the outputs from the screen
              save_model = True, # save the best model
              save_epoch_error = True, # log error at every epoch
              save_to_database = {'name': 'hyperparam.sqlite3',
                                  'records': {'Batch_Size': data.batch_size,
                                              'Learning_Rate': learning_method.learning_rate,
                                              'Momentum': learning_method.momentum}}
             ) # end log

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = log,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 30,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 3
0
def train():
    batch_size = 128
    data = Cifar10(batch_size=batch_size, train_valid_test_ratio=[4,1,1])
    _, c, h, w = data.train.X.shape

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Convolution2D(input_channels=c, filters=8, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    h, w = full(h, w, kernel=3, stride=1)
    model.add(RELU())
    model.add(Convolution2D(input_channels=8, filters=16, kernel_size=(3,3), stride=(1,1), border_mode='valid'))
    h, w = valid(h, w, kernel=3, stride=1)
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4,4), mode='max'))
    h, w = valid(h, w, kernel=4, stride=4)
    model.add(Flatten())
    model.add(Linear(16*h*w, 512))
    model.add(BatchNormalization((512,), short_memory=0.9))
    model.add(RELU())

    model.add(Linear(512, 10))
    model.add(Softmax())

    learning_method = RMSprop(learning_rate=0.01)

    # Build Logger
    log = Log(experiment_name = 'cifar10_cnn_example',
              description = 'This is a tutorial',
              save_outputs = True, # log all the outputs from the screen
              save_model = True, # save the best model
              save_epoch_error = True, # log error at every epoch
              save_to_database = {'name': 'hyperparam.sqlite3',
                                  'records': {'Batch_Size': batch_size,
                                              'Learning_Rate': learning_method.learning_rate}}
             ) # end log

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = log,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 30,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 4
0
def train():

    # build dataset
    batch_size = 64
    data = Mnist(batch_size=batch_size, train_valid_test_ratio=[5,1,1])

    # build model
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Linear(prev_dim=28*28, this_dim=200))
    model.add(RELU())
    model.add(Linear(prev_dim=200, this_dim=100))
    model.add(RELU())
    model.add(Dropout(0.5))
    model.add(Linear(prev_dim=100, this_dim=10))
    model.add(Softmax())

    # build learning method
    decay_batch = int(data.train.X.shape[0] * 2 / batch_size)
    learning_method = SGD(learning_rate=0.1, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=decay_batch)

    # Build Logger
    log = Log(experiment_name = 'MLP',
              description = 'This is a tutorial',
              save_outputs = True, # log all the outputs from the screen
              save_model = True, # save the best model
              save_epoch_error = True, # log error at every epoch
              save_to_database = {'name': 'Example.sqlite3',
                                  'records': {'Batch_Size': batch_size,
                                              'Learning_Rate': learning_method.learning_rate,
                                              'Momentum': learning_method.momentum}}
             ) # end log

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = log,
                               dataset = data,
                               train_cost = mse,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 100,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 5
0
def train():

    # build dataset
    data = Mnist(batch_size=64, train_valid_test_ratio=[5,1,1])
    # for autoencoder, the output will be equal to input
    data.set_train(X=data.get_train().X, y=data.get_train().X)
    data.set_valid(X=data.get_valid().X, y=data.get_valid().X)

    # build model
    model = Sequential()
    # build encoder
    model.add(Gaussian(input_var=T.matrix()))
    encode_layer1 = Linear(prev_dim=28*28, this_dim=200)
    model.add(encode_layer1)
    model.add(RELU())
    encode_layer2 = Linear(prev_dim=200, this_dim=50)
    model.add(encode_layer2)
    model.add(Tanh())

    # build decoder
    decode_layer1 = Linear(prev_dim=50, this_dim=200, W=encode_layer2.W.T)
    model.add(decode_layer1)
    model.add(RELU())
    decode_layer2 = Linear(prev_dim=200, this_dim=28*28, W=encode_layer1.W.T)
    model.add(decode_layer2)
    model.add(Sigmoid())

    # build learning method
    learning_method = AdaGrad(learning_rate=0.01, momentum=0.9,
                              lr_decay_factor=0.9, decay_batch=10000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = entropy,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 6
0
def train():
    _TEXT_INPUT_DIM_ = 10
    _NUM_EXP_ = 1000
    _IMG_INPUT_DIM_ = (3, 32, 32)
    _OUTPUT_DIM_ = 100
    _TEXT_OUTPUT_DIM_ = 100
    _IMG_OUTPUT_DIM_ = 80

    # build dataset
    txt = np.random.rand(_NUM_EXP_, _TEXT_INPUT_DIM_)
    img = np.random.rand(_NUM_EXP_, *_IMG_INPUT_DIM_)
    y = np.random.rand(_NUM_EXP_, _OUTPUT_DIM_)
    data = MultiInputsData(X=(txt, img), y=y)

    # build left and right model
    left_model = _left_model(_TEXT_INPUT_DIM_, _TEXT_OUTPUT_DIM_)
    right_model = _right_model(_IMG_INPUT_DIM_, _IMG_OUTPUT_DIM_)

    # build the master model
    model = Sequential(input_var=(T.matrix(), T.tensor4()), output_var=T.matrix())
    model.add(Parallel(left_model, right_model))
    model.add(FlattenAll())
    model.add(Concate(_TEXT_OUTPUT_DIM_ + _IMG_OUTPUT_DIM_, _OUTPUT_DIM_))

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 7
0
def train():

    data = VOC(batch_size=32, train_valid_test_ratio=[5, 1, 1])
    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Alexnet(input_shape=(3, 222, 222), output_dim=11))
    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9, lr_decay_factor=0.9, decay_batch=5000)
    # put everything into the train object
    train_object = TrainObject(
        model=model,
        log=None,
        dataset=data,
        train_cost=error,
        valid_cost=error,
        learning_method=learning_method,
        stop_criteria={"max_epoch": 10, "epoch_look_back": 5, "percent_decrease": 0.01},
    )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 8
0
def train():
    """
    This examples implements the variational autoencoder from the paper
    Auto-Encoding Variational Bayes by Diederik P Kingma, Max Welling, arXiv:1312.6114
    """

    # build dataset
    data = Mnist(batch_size=100, binary=False, train_valid_test_ratio=[5,1,1])
    # for autoencoder, the output will be equal to input
    data.set_train(X=data.get_train().X, y=data.get_train().X)
    data.set_valid(X=data.get_valid().X, y=data.get_valid().X)

    # build model
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(VariationalAutoencoder(input_dim=28*28, bottlenet_dim=200, z_dim=20))

    # build learning method
    learning_method = SGD(learning_rate=0.0001, momentum=0.9,
                              lr_decay_factor=0.9, decay_batch=10000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = SGVB_bin,
                               valid_cost = SGVB_bin,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 9
0
def train():

    # build dataset
    batch_size = 64
    data = Mnist(batch_size=batch_size, train_valid_test_ratio=[5, 1, 1])

    # build model
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Linear(prev_dim=28 * 28, this_dim=200))
    model.add(RELU())
    model.add(Linear(prev_dim=200, this_dim=100))
    model.add(RELU())
    model.add(Dropout(0.5))
    model.add(Linear(prev_dim=100, this_dim=10))
    model.add(Softmax())

    # build learning method
    decay_batch = int(data.train.X.shape[0] * 2 / batch_size)
    learning_method = SGD(learning_rate=0.1,
                          momentum=0.9,
                          lr_decay_factor=0.9,
                          decay_batch=decay_batch)

    # Build Logger
    log = Log(
        experiment_name='MLP',
        description='This is a tutorial',
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'Example.sqlite3',
            'records': {
                'Batch_Size': batch_size,
                'Learning_Rate': learning_method.learning_rate,
                'Momentum': learning_method.momentum
            }
        })  # end log

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=mse,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 100,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()

    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print('test accuracy:', accuracy)
Exemplo n.º 10
0
def train():

    data = VOC(batch_size=32, train_valid_test_ratio=[5, 1, 1])
    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Alexnet(input_shape=(3, 222, 222), output_dim=11))
    # build learning method
    learning_method = SGD(learning_rate=0.01,
                          momentum=0.9,
                          lr_decay_factor=0.9,
                          decay_batch=5000)
    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=None,
                               dataset=data,
                               train_cost=error,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 10,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 11
0
def train():

    # build dataset
    data = Mnist(batch_size=64, train_valid_test_ratio=[5,1,1])

    # build model
    model = Sequential()
    model.add(Linear(prev_dim=28*28, this_dim=200))
    model.add(RELU())
    model.add(Linear(prev_dim=200, this_dim=100))
    model.add(RELU())
    model.add(Dropout(0.5))
    model.add(Linear(prev_dim=100, this_dim=10))
    model.add(Softmax())

    # build learning method
    learning_method = AdaGrad(learning_rate=0.1, momentum=0.9,
                              lr_decay_factor=0.9, decay_batch=10000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = mse,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    print 'test accuracy:', accuracy_score(y, ypred)
Exemplo n.º 12
0
def train():

    data = Cifar10(batch_size=32, train_valid_test_ratio=[4,1,1])

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Convolution2D(input_channels=3, filters=32, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=32, filters=32, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Convolution2D(input_channels=32, filters=64, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=64, filters=64, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(64*8*8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 13
0
def train():

    # build dataset
    data = Mnist(batch_size=64, train_valid_test_ratio=[5, 1, 1])
    # for autoencoder, the output will be equal to input
    data.set_train(X=data.get_train().X, y=data.get_train().X)
    data.set_valid(X=data.get_valid().X, y=data.get_valid().X)

    # build model
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    # build encoder
    model.add(Gaussian())
    encode_layer1 = Linear(prev_dim=28 * 28, this_dim=200)
    model.add(encode_layer1)
    model.add(RELU())
    encode_layer2 = Linear(prev_dim=200, this_dim=50)
    model.add(encode_layer2)
    model.add(Tanh())

    # build decoder
    decode_layer1 = Linear(prev_dim=50, this_dim=200, W=encode_layer2.W.T)
    model.add(decode_layer1)
    model.add(RELU())
    decode_layer2 = Linear(prev_dim=200, this_dim=28 * 28, W=encode_layer1.W.T)
    model.add(decode_layer2)
    model.add(Sigmoid())

    # build learning method
    learning_method = AdaGrad(learning_rate=0.01,
                              momentum=0.9,
                              lr_decay_factor=0.9,
                              decay_batch=10000)

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=None,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=entropy,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 10,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()
def train():
    _TEXT_INPUT_DIM_ = 10
    _NUM_EXP_ = 1000
    _IMG_INPUT_DIM_ = (3, 32, 32)
    _OUTPUT_DIM_ = 100
    _TEXT_OUTPUT_DIM_ = 100
    _IMG_OUTPUT_DIM_ = 80

    # build dataset
    txt = np.random.rand(_NUM_EXP_, _TEXT_INPUT_DIM_)
    img = np.random.rand(_NUM_EXP_, *_IMG_INPUT_DIM_)
    y = np.random.rand(_NUM_EXP_, _OUTPUT_DIM_)
    data = MultiInputsData(X=(txt, img), y=y)

    # build left and right model
    left_model = _left_model(_TEXT_INPUT_DIM_, _TEXT_OUTPUT_DIM_)
    right_model = _right_model(_IMG_INPUT_DIM_, _IMG_OUTPUT_DIM_)

    # build the master model
    model = Sequential(input_var=(T.matrix(), T.tensor4()),
                       output_var=T.matrix())
    model.add(Parallel(left_model, right_model))
    model.add(FlattenAll())
    model.add(Concate(_TEXT_OUTPUT_DIM_ + _IMG_OUTPUT_DIM_, _OUTPUT_DIM_))

    # build learning method
    learning_method = SGD(learning_rate=0.01,
                          momentum=0.9,
                          lr_decay_factor=0.9,
                          decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=None,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 10,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 15
0
def train():
    # create a fake dataset
    X1 = np.random.rand(100000, 1000)
    y1 = np.random.rand(100000, 10)
    with open('X1.npy', 'wb') as xin, open('y1.npy', 'wb') as yin:
        np.save(xin, X1)
        np.save(yin, y1)

    X2 = np.random.rand(100000, 1000)
    y2 = np.random.rand(100000, 10)
    with open('X2.npy', 'wb') as xin, open('y2.npy', 'wb') as yin:
        np.save(xin, X2)
        np.save(yin, y2)

    X3 = np.random.rand(100000, 1000)
    y3 = np.random.rand(100000, 10)
    with open('X3.npy', 'wb') as xin, open('y3.npy', 'wb') as yin:
        np.save(xin, X3)
        np.save(yin, y3)

    # now we can create the data by putting the paths
    # ('X1.npy', 'y1.npy') and ('X2.npy', 'y2.npy') into DataBlocks
    data = DataBlocks(data_paths=[('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy'), ('X3.npy', 'y3.npy')],
                      batch_size=100, train_valid_test_ratio=[3,2,0], allow_preload=False)


    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Linear(prev_dim=1000, this_dim=200))
    model.add(RELU())
    model.add(Linear(prev_dim=200, this_dim=100))
    model.add(RELU())
    model.add(Dropout(0.5))
    model.add(Linear(prev_dim=100, this_dim=10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    for X_path, y_path in [('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy')]:
        with open(X_path) as Xin, open(y_path) as yin:
            # test the model on test set
            ypred = model.fprop(np.load(Xin))
            ypred = np.argmax(ypred, axis=1)
            y = np.argmax(np.load(yin), axis=1)
            accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
            print('combined accuracy for blk %s:'%X_path, accuracy)
Exemplo n.º 16
0
def train():
    batch_size = 256
    short_memory = 0.9
    learning_rate = 0.005
    data = Cifar10(batch_size=batch_size, train_valid_test_ratio=[4, 1, 1])
    _, c, h, w = data.train.X.shape

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(
        Convolution2D(input_channels=c,
                      filters=8,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='full'))
    h, w = full(h, w, kernel=3, stride=1)
    model.add(
        BatchNormalization(dim=8, layer_type='conv',
                           short_memory=short_memory))
    model.add(RELU())
    model.add(
        Convolution2D(input_channels=8,
                      filters=16,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='valid'))
    h, w = valid(h, w, kernel=3, stride=1)
    model.add(
        BatchNormalization(dim=16,
                           layer_type='conv',
                           short_memory=short_memory))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4, 4), mode='max'))
    h, w = valid(h, w, kernel=4, stride=4)
    model.add(Flatten())
    model.add(Linear(16 * h * w, 512))
    model.add(
        BatchNormalization(dim=512, layer_type='fc',
                           short_memory=short_memory))
    model.add(RELU())

    model.add(Linear(512, 10))
    model.add(Softmax())

    # learning_method = RMSprop(learning_rate=learning_rate)
    learning_method = Adam(learning_rate=learning_rate)
    # learning_method = SGD(learning_rate=0.001)

    # Build Logger
    log = Log(
        experiment_name='cifar10_cnn_tutorial',
        description='This is a tutorial',
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'hyperparam.sqlite3',
            'records': {
                'Batch_Size': batch_size,
                'Learning_Rate': learning_method.learning_rate
            }
        })  # end log

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 100,
                                   'epoch_look_back': 10,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 17
0
def train():
    max_features=20000
    maxseqlen = 100 # cut texts after this number of words (among top max_features most common words)
    batch_size = 16
    word_vec_len = 256
    iter_class = 'SequentialRecurrentIterator'
    seq_len = 10

    data = IMDB(pad_zero=True, maxlen=100, nb_words=max_features, batch_size=batch_size,
                train_valid_test_ratio=[8,2,0], iter_class=iter_class, seq_len=seq_len)

    print('Build model...')
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Embedding(max_features, word_vec_len))

    # MLP layers
    model.add(Transform((word_vec_len,))) # transform from 3d dimensional input to 2d input for mlp
    model.add(Linear(word_vec_len, 100))
    model.add(RELU())
    model.add(BatchNormalization(dim=100, layer_type='fc'))
    model.add(Linear(100,100))
    model.add(RELU())
    model.add(BatchNormalization(dim=100, layer_type='fc'))
    model.add(Linear(100, word_vec_len))
    model.add(RELU())
    model.add(Transform((maxseqlen, word_vec_len))) # transform back from 2d to 3d for recurrent input

    # Stacked up BiLSTM layers
    model.add(BiLSTM(word_vec_len, 50, output_mode='concat', return_sequences=True))
    model.add(BiLSTM(100, 24, output_mode='sum', return_sequences=True))
    model.add(LSTM(24, 24, return_sequences=True))

    # MLP layers
    model.add(Reshape((24 * maxseqlen,)))
    model.add(BatchNormalization(dim=24 * maxseqlen, layer_type='fc'))
    model.add(Linear(24 * maxseqlen, 50))
    model.add(RELU())
    model.add(Dropout(0.2))
    model.add(Linear(50, 1))
    model.add(Sigmoid())

    # build learning method
    decay_batch = int(data.train.X.shape[0] * 5 / batch_size)
    learning_method = SGD(learning_rate=0.1, momentum=0.9,
                          lr_decay_factor=1.0, decay_batch=decay_batch)

    # Build Logger
    log = Log(experiment_name = 'MLP',
              description = 'This is a tutorial',
              save_outputs = True, # log all the outputs from the screen
              save_model = True, # save the best model
              save_epoch_error = True, # log error at every epoch
              save_to_database = {'name': 'Example.sqlite3',
                                  'records': {'Batch_Size': batch_size,
                                              'Learning_Rate': learning_method.learning_rate,
                                              'Momentum': learning_method.momentum}}
             ) # end log

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = log,
                               dataset = data,
                               train_cost = mse,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 100,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 18
0
def train(args):
    # build dataset

    xpath = os.environ['MOZI_DATA_PATH'] + '/X_{}_augment_{}.npy'.format(
        '_'.join([str(d) for d in _IMG_INPUT_DIM_]), str(img_augment))
    ypath = os.environ['MOZI_DATA_PATH'] + '/y_{}_augment_{}.npy'.format(
        '_'.join([str(d) for d in _IMG_INPUT_DIM_]), str(img_augment))
    if not os.path.exists(xpath) or not os.path.exists(ypath):
        X, y = make_Xy(args, img_augment)
        with open(xpath, 'wb') as fout:
            np.save(fout, X)
            print '..saved to', xpath

        with open(ypath, 'wb') as fout:
            np.save(fout, y)
            print '..saved to', ypath
    else:
        with open(xpath) as xin, open(ypath) as yin:
            X = np.load(xin)
            y = np.load(yin)
            print '..data loaded'

    if img_preprocess:
        X = img_preprocess.apply(X)
    # import pdb; pdb.set_trace()
    idxs = np.arange(len(X))
    np.random.shuffle(idxs)
    data = MultiInputsData(X=X[idxs][:10000],
                           y=y[idxs][:10000],
                           train_valid_test_ratio=train_valid_test_ratio,
                           batch_size=batch_size)

    if load_model:
        print '..loading model', load_model
        model_path = os.environ[
            'MOZI_SAVE_PATH'] + '/' + load_model + '/model.pkl'
        with open(model_path) as fin:
            model = cPickle.load(fin)
    else:
        # c, h, w = _IMG_INPUT_DIM_
        # build the master model
        model = Sequential(input_var=T.tensor4(),
                           output_var=T.tensor4(),
                           verbose=verbose)

        ks = 11
        model.add(
            Convolution2D(input_channels=3,
                          filters=16,
                          kernel_size=(ks, ks),
                          stride=(1, 1),
                          border_mode='full'))
        model.add(Crop(border=(ks / 2, ks / 2)))
        model.add(
            BatchNormalization(dim=16,
                               layer_type='conv',
                               short_memory=short_memory))
        model.add(RELU())
        model.add(
            Pooling2D(poolsize=(3, 3),
                      stride=(1, 1),
                      padding=(1, 1),
                      mode='max'))
        # model.add(RELU())
        # h, w = full(h, w, 5, 1)

        ks = 9
        model.add(
            Convolution2D(input_channels=16,
                          filters=32,
                          kernel_size=(ks, ks),
                          stride=(1, 1),
                          border_mode='full'))
        model.add(Crop(border=(ks / 2, ks / 2)))
        model.add(
            BatchNormalization(dim=32,
                               layer_type='conv',
                               short_memory=short_memory))
        model.add(RELU())
        model.add(
            Pooling2D(poolsize=(3, 3),
                      stride=(1, 1),
                      padding=(1, 1),
                      mode='max'))

        ks = 5
        model.add(
            Convolution2D(input_channels=32,
                          filters=1,
                          kernel_size=(ks, ks),
                          stride=(1, 1),
                          border_mode='full'))
        # model.add(BatchNormalization(dim=1, layer_type='conv', short_memory=short_memory))

        model.add(Crop(border=(ks / 2, ks / 2)))
        model.add(Sigmoid())

    # build learning method
    # learning_method = SGD(learning_rate=lr, momentum=momentum,
    #                       lr_decay_factor=lr_decay_factor, decay_batch=decay_batch)
    learning_method = Adam(learning_rate=lr)
    # learning_method = RMSprop(learning_rate=lr)

    # Build Logger
    log = Log(
        experiment_name=experiment_name,
        description=desc,
        save_outputs=True,  # log all the outputs from the screen
        save_model=save_model,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'skin_segment.sqlite3',
            'records': {
                'learning_rate': lr,
                'valid_cost_func': valid_cost,
                'train_cost_func': train_cost
            }
        })  # end log

    os.system('cp {} {}'.format(__file__, log.exp_dir))
    dname = os.path.dirname(os.path.realpath(__file__))

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=train_cost,
                               valid_cost=valid_cost,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 100,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 19
0
def train():

    data = Cifar10(batch_size=32, train_valid_test_ratio=[4, 1, 1])

    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(
        Convolution2D(input_channels=3,
                      filters=8,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      border_mode='full'))
    model.add(RELU())
    model.add(
        Convolution2D(input_channels=8,
                      filters=16,
                      kernel_size=(3, 3),
                      stride=(1, 1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(4, 4), stride=(4, 4), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(16 * 8 * 8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01,
                          momentum=0.9,
                          lr_decay_factor=0.9,
                          decay_batch=5000)

    # Build Logger
    log = Log(
        experiment_name='cifar10_cnn',
        description='This is a tutorial',
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            'name': 'hyperparam.sqlite3',
            'records': {
                'Batch_Size': data.batch_size,
                'Learning_Rate': learning_method.learning_rate,
                'Momentum': learning_method.momentum
            }
        })  # end log

    # put everything into the train object
    train_object = TrainObject(model=model,
                               log=log,
                               dataset=data,
                               train_cost=entropy,
                               valid_cost=error,
                               learning_method=learning_method,
                               stop_criteria={
                                   'max_epoch': 30,
                                   'epoch_look_back': 5,
                                   'percent_decrease': 0.01
                               })
    # finally run the code
    train_object.setup()
    train_object.run()

    # test the model on test set
    ypred = model.fprop(data.get_test().X)
    ypred = np.argmax(ypred, axis=1)
    y = np.argmax(data.get_test().y, axis=1)
    accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
    print 'test accuracy:', accuracy
Exemplo n.º 20
0
def train():
    max_features = 20000
    maxseqlen = 100  # cut texts after this number of words (among top max_features most common words)
    batch_size = 16
    word_vec_len = 256
    iter_class = "SequentialRecurrentIterator"
    seq_len = 10

    data = IMDB(
        pad_zero=True,
        maxlen=100,
        nb_words=max_features,
        batch_size=batch_size,
        train_valid_test_ratio=[8, 2, 0],
        iter_class=iter_class,
        seq_len=seq_len,
    )

    print("Build model...")
    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Embedding(max_features, word_vec_len))

    # MLP layers
    model.add(Transform((word_vec_len,)))  # transform from 3d dimensional input to 2d input for mlp
    model.add(Linear(word_vec_len, 100))
    model.add(RELU())
    model.add(BatchNormalization(dim=100, layer_type="fc"))
    model.add(Linear(100, 100))
    model.add(RELU())
    model.add(BatchNormalization(dim=100, layer_type="fc"))
    model.add(Linear(100, word_vec_len))
    model.add(RELU())
    model.add(Transform((maxseqlen, word_vec_len)))  # transform back from 2d to 3d for recurrent input

    # Stacked up BiLSTM layers
    model.add(BiLSTM(word_vec_len, 50, output_mode="concat", return_sequences=True))
    model.add(BiLSTM(100, 24, output_mode="sum", return_sequences=True))
    model.add(LSTM(24, 24, return_sequences=True))

    # MLP layers
    model.add(Reshape((24 * maxseqlen,)))
    model.add(BatchNormalization(dim=24 * maxseqlen, layer_type="fc"))
    model.add(Linear(24 * maxseqlen, 50))
    model.add(RELU())
    model.add(Dropout(0.2))
    model.add(Linear(50, 1))
    model.add(Sigmoid())

    # build learning method
    decay_batch = int(data.train.X.shape[0] * 5 / batch_size)
    learning_method = SGD(learning_rate=0.1, momentum=0.9, lr_decay_factor=1.0, decay_batch=decay_batch)

    # Build Logger
    log = Log(
        experiment_name="MLP",
        description="This is a tutorial",
        save_outputs=True,  # log all the outputs from the screen
        save_model=True,  # save the best model
        save_epoch_error=True,  # log error at every epoch
        save_to_database={
            "name": "Example.sqlite3",
            "records": {
                "Batch_Size": batch_size,
                "Learning_Rate": learning_method.learning_rate,
                "Momentum": learning_method.momentum,
            },
        },
    )  # end log

    # put everything into the train object
    train_object = TrainObject(
        model=model,
        log=log,
        dataset=data,
        train_cost=mse,
        valid_cost=error,
        learning_method=learning_method,
        stop_criteria={"max_epoch": 100, "epoch_look_back": 5, "percent_decrease": 0.01},
    )
    # finally run the code
    train_object.setup()
    train_object.run()
Exemplo n.º 21
0
def train():
    # create a fake dataset
    X1 = np.random.rand(100000, 1000)
    y1 = np.random.rand(100000, 10)
    with open('X1.npy', 'wb') as xin, open('y1.npy', 'wb') as yin:
        np.save(xin, X1)
        np.save(yin, y1)

    X2 = np.random.rand(100000, 1000)
    y2 = np.random.rand(100000, 10)
    with open('X2.npy', 'wb') as xin, open('y2.npy', 'wb') as yin:
        np.save(xin, X2)
        np.save(yin, y2)

    X3 = np.random.rand(100000, 1000)
    y3 = np.random.rand(100000, 10)
    with open('X3.npy', 'wb') as xin, open('y3.npy', 'wb') as yin:
        np.save(xin, X3)
        np.save(yin, y3)

    # now we can create the data by putting the paths
    # ('X1.npy', 'y1.npy') and ('X2.npy', 'y2.npy') into DataBlocks
    data = DataBlocks(data_paths=[('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy'), ('X3.npy', 'y3.npy')],
                      batch_size=100, train_valid_test_ratio=[3,2,0])


    model = Sequential(input_var=T.matrix(), output_var=T.matrix())
    model.add(Linear(prev_dim=1000, this_dim=200))
    model.add(RELU())
    model.add(Linear(prev_dim=200, this_dim=100))
    model.add(RELU())
    model.add(Dropout(0.5))
    model.add(Linear(prev_dim=100, this_dim=10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    for X_path, y_path in [('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy')]:
        with open(X_path) as Xin, open(y_path) as yin:
            # test the model on test set
            ypred = model.fprop(np.load(Xin))
            ypred = np.argmax(ypred, axis=1)
            y = np.argmax(np.load(yin), axis=1)
            accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
            print 'combined accuracy for blk %s:'%X_path, accuracy
Exemplo n.º 22
0
def train():
    X1 = np.random.rand(1000, 3, 32, 32)
    y1 = np.random.rand(1000, 10)
    with open('X1.npy', 'wb') as xin, open('y1.npy', 'wb') as yin:
        np.save(xin, X1)
        np.save(yin, y1)

    X2 = np.random.rand(1000, 3, 32, 32)
    y2 = np.random.rand(1000, 10)
    with open('X2.npy', 'wb') as xin, open('y2.npy', 'wb') as yin:
        np.save(xin, X1)
        np.save(yin, y1)

    # now we can create the data by putting the paths
    # ('X1.npy', 'y1.npy') and ('X2.npy', 'y2.npy') into DataBlocks
    data = DataBlocks(data_paths=[('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy')],
                      batch_size=100, train_valid_test_ratio=[3,1,1])


    model = Sequential(input_var=T.tensor4(), output_var=T.matrix())
    model.add(Convolution2D(input_channels=3, filters=32, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=32, filters=32, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Convolution2D(input_channels=32, filters=64, kernel_size=(3,3), stride=(1,1), border_mode='full'))
    model.add(RELU())
    model.add(Convolution2D(input_channels=64, filters=64, kernel_size=(3,3), stride=(1,1)))
    model.add(RELU())
    model.add(Pooling2D(poolsize=(2, 2), mode='max'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Linear(64*8*8, 512))
    model.add(RELU())
    model.add(Dropout(0.5))

    model.add(Linear(512, 10))
    model.add(Softmax())

    # build learning method
    learning_method = SGD(learning_rate=0.01, momentum=0.9,
                          lr_decay_factor=0.9, decay_batch=5000)

    # put everything into the train object
    train_object = TrainObject(model = model,
                               log = None,
                               dataset = data,
                               train_cost = entropy,
                               valid_cost = error,
                               learning_method = learning_method,
                               stop_criteria = {'max_epoch' : 10,
                                                'epoch_look_back' : 5,
                                                'percent_decrease' : 0.01}
                               )
    # finally run the code
    train_object.setup()
    train_object.run()

    for X_path, y_path in [('X1.npy', 'y1.npy'), ('X2.npy', 'y2.npy')]:
        with open(X_path) as Xin, open(y_path) as yin:
            # test the model on test set
            ypred = model.fprop(np.load(Xin))
            ypred = np.argmax(ypred, axis=1)
            y = np.argmax(np.load(yin), axis=1)
            accuracy = np.equal(ypred, y).astype('f4').sum() / len(y)
            print 'combined accuracy for blk %s:'%X_path, accuracy