Exemplo n.º 1
0
def train():
    model = LogisticRegression((4, 1))
    dataloader = DataLoader()
    dataloader.train_validation_test_split()
    train_X, train_Y = dataloader.get_train()
    val_X, val_Y = dataloader.get_validation()
    test_X, test_Y = dataloader.get_test()
    return train_X, train_Y
Exemplo n.º 2
0
 def _classifier(self, X, y):
     if self.classifier == 'logistic_regression':
         self.model_classifier = LogisticRegression()
         self.model_classifier.fit(X, y)
     elif self.classifier == 'svm':
         self.model_classifier = SVC()
         self.model_classifier.fit(X, y)
     elif self.classifier == 'neural_network':
         self.model_classifier = NeuralNetwork(X.shape[1], len(self.cls2num))
         self.model_classifier.fit(X, y, epochs = 20, batch_size=16)
     else:
         # error
         return None
Exemplo n.º 3
0
def test_Dropout():
    npy_rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(123)
    data_x = theano.shared(
        100 * npy_rng.normal(0, 1, [1000, 50]).astype(theano.config.floatX))
    data_y = theano.shared(
        npy_rng.randint(0, 10, 1000))
    
    ae = ClassicalAutoencoder(
        50, 70, vistype='real', hidtype='binary', tie=True
    )
    sl = LinearLayer(50, 70) + LogisticRegression(70, 10)
    # sl.print_layer()
    lg = LogisticRegression(50, 10)
    # lg.print_layer()
    
    ae_recon = theano.function(
        [],
        ae.reconstruction(),
        givens={ae.varin: data_x}
    )
    sl_output = theano.function(
        [],
        sl.output(),
        givens={sl.varin: data_x}
    )
    lg_output = theano.function(
        [],
        lg.output(),
        givens={lg.varin: data_x}
    )

    recon_before_dropout = ae_recon()
    output_before_dropout = sl_output()
    lgoutput_before_dropout = lg_output()
    
    dropout_ae = Dropout(ae, [0.2, 0.5], theano_rng=theano_rng)
    dropout_sl = Dropout(sl, [0.7, 0.5], theano_rng=theano_rng)
    dropout_lg = Dropout(lg, [0.5], theano_rng=theano_rng)
    # dropout_ae.dropout_model.print_layer()
    # dropout_sl.dropout_model.print_layer()
    # dropout_lg.dropout_model.print_layer()
    
    ae_recon = theano.function(
        [],
        ae.reconstruction(),
        givens={ae.varin: data_x}
    )
    sl_output = theano.function(
        [],
        sl.output(),
        givens={sl.varin: data_x}
    )
    lg_output = theano.function(
        [],
        lg.output(),
        givens={lg.varin: data_x}
    )
    recon_after_dropout = ae_recon()
    output_after_dropout = sl_output()
    lgoutput_after_dropout = lg_output()
    
    assert numpy.allclose(recon_before_dropout, recon_after_dropout)
    assert numpy.allclose(output_before_dropout, output_after_dropout)
    assert numpy.allclose(lgoutput_before_dropout, lgoutput_after_dropout)
                break
        prev_cost = cost
    save_params(
        model,
        'ZLIN_4000_1000_4000_1000_4000_1000_4000_normhid_nolinb_cae1_dropout.npy'
    )
print "Done."

#########################
# BUILD FINE-TUNE MODEL #
#########################

print "\n\n... building fine-tune model -- contraction 1"
for imodel in model.models_stack:
    imodel.threshold = 0.
model_ft = model + LogisticRegression(hid_layer_sizes[-1], 10, npy_rng=npy_rng)
model_ft.print_layer()

train_set_error_rate = theano.function(
    [],
    T.mean(T.neq(model_ft.models_stack[-1].predict(), train_y)),
    givens={model_ft.varin: train_x},
)
test_set_error_rate = theano.function(
    [],
    T.mean(T.neq(model_ft.models_stack[-1].predict(), test_y)),
    givens={model_ft.varin: test_x},
)
print "Done."

print "... training with conjugate gradient: minimize.py"
X_train = df.drop('survived', axis=1)
#test to see what are the feature that are really important for the learning of the model
delete = ['survived', 'age', 'sibling', 'parents', 'ticket', 'fare']
#my titanic experience
my_titanic = [1, 0, 24.0, 0, 0, 7.000]
died = ['sex', 'survived']
Positive_train = df.drop(delete, axis=1)
Negative_train = df.drop(died, axis=1)

y_train = df['survived']

X_test = df2.drop('survived', axis=1)
y_test = df2['survived']

prova = LogisticRegression()
'''
prova2 = LogisticRegression()

prova3 = LogisticRegression()
'''

w, b = prova.fit(X_train, y_train)
#fit the model only with sex feature
'''
wp,bp = prova2.fit(Positive_train,y_train)

#fit the model without sex feature
wn,bn = prova3.fit(Negative_train,y_train)
'''
Exemplo n.º 6
0
datasets = load_data('/data/lisa/data/mnist.pkl.gz')

train_set_x, train_set_y = datasets[0]
valid_set_x, valid_set_y = datasets[1]
test_set_x, test_set_y = datasets[2]

npy_rng = numpy.random.RandomState(123)

###############
# BUILD MODEL #
###############

model = ClassicalAutoencoder(
    784, 784, vistype='binary', npy_rng=npy_rng) + ClassicalAutoencoder(
        784, 784, vistype='binary', npy_rng=npy_rng) + ClassicalAutoencoder(
            784, 784, vistype='binary', npy_rng=npy_rng) + LogisticRegression(
                784, 10, npy_rng=npy_rng)

error_rate = theano.function(
    [],
    T.mean(T.neq(model.models_stack[-1].predict(), test_set_y)),
    givens={model.models_stack[0].varin: test_set_x},
)

#############
# PRE-TRAIN #
#############

for i in range(len(model.models_stack) - 1):
    print "\n\nPre-training layer %d:" % i
    trainer = GraddescentMinibatch(varin=model.varin,
                                   data=train_set_x,
#########################
# BUILD PRE-TRAIN MODEL #
#########################

print "... building models"
npy_rng = numpy.random.RandomState(123)
model = ReluAutoencoder(train_x.get_value().shape[1],
                        hid_layer_sizes[0],
                        vistype='real',
                        tie=True,
                        npy_rng=npy_rng) + ReluAutoencoder(
                            hid_layer_sizes[0],
                            hid_layer_sizes[1],
                            vistype='real',
                            tie=True,
                            npy_rng=npy_rng) + LogisticRegression(
                                hid_layer_sizes[1], 10, npy_rng=npy_rng)

print "\nModel for SGD:"
model.print_layer()

train_set_error_rate = theano.function(
    [],
    T.mean(T.neq(model.models_stack[-1].predict(), train_y)),
    givens={model.varin: train_x},
)
test_set_error_rate = theano.function(
    [],
    T.mean(T.neq(model.models_stack[-1].predict(), test_y)),
    givens={model.varin: test_x},
)