def build_model(x_data, y_data, x_test, y_test):
    print(x_data[0])
    print(y_data[0])
    model = Sequential()
    model.add(Dense(input_shape=[x_data.shape[1]],output_dim=300, kernel_initializer= initializers.Zeros()))
    model.add(Activation('linear'))
#    model.add(Dropout(0.1))
#    model.add(Activation('relu'))
    model.add(Dense(output_dim=100))
    model.add(Activation('tanh'))
#    model.add(Dense(output_dim=100))
#    model.add(Activation('relu'))
    model.add(Dense(output_dim=1))
    model.add(Activation('sigmoid'))
    sgd = SGD(lr=0.01, decay=0.0001, momentum=0.2, nesterov=True)
    model.compile(loss='binary_crossentropy', optimizer=sgd)
    model.fit(x_data, y_data, batch_size=10000, nb_epoch=20, shuffle=True, validation_split=0.2)
    preds = model.predict_proba(x_data)
    result = metrics.roc_auc_score(y_data, preds)
    print(result)
    print("test")
    preds = model.predict_proba(x_test)
    for i in range(0, 50):
        print("result=%.4f, %.4f" %(preds[i], y_test[i])) 
    result = metrics.roc_auc_score(y_test, preds)
    print(result)
Example #2
0
def test_nested_sequential(in_tmpdir):
    (x_train, y_train), (x_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
    model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False)

    model.train_on_batch(x_train[:32], y_train[:32])

    loss = model.evaluate(x_test, y_test, verbose=0)

    model.predict(x_test, verbose=0)
    model.predict_classes(x_test, verbose=0)
    model.predict_proba(x_test, verbose=0)

    fname = 'test_nested_sequential_temp.h5'
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(num_hidden, input_shape=(input_dim,)))
    inner.add(Activation('relu'))
    inner.add(Dense(num_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(x_test, y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
Example #3
0
def test_nested_sequential():
    (X_train, y_train), (X_test, y_test) = _get_test_data()

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_test, y_test, verbose=0)

    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)

    fname = "test_nested_sequential_temp.h5"
    model.save_weights(fname, overwrite=True)

    inner = Sequential()
    inner.add(Dense(nb_hidden, input_shape=(input_dim,)))
    inner.add(Activation("relu"))
    inner.add(Dense(nb_class))

    middle = Sequential()
    middle.add(inner)

    model = Sequential()
    model.add(middle)
    model.add(Activation("softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_test, y_test, verbose=0)
    assert loss == nloss

    # test serialization
    config = model.get_config()
    new_model = Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    new_model = model_from_json(json_str)

    yaml_str = model.to_yaml()
    new_model = model_from_yaml(yaml_str)
Example #4
0
def test_merge_overlap():
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, left], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_train, y_train, verbose=0)
    assert(loss < 0.7)
    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)
    model.get_config(verbose=0)

    fname = 'test_merge_overlap_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_train, y_train, verbose=0)
    assert(loss == nloss)
Example #5
0
def test_merge_sum():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)

    # test weight saving
    fname = 'test_merge_sum_temp.h5'
    model.save_weights(fname, overwrite=True)
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))
    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))
    model = Sequential()
    model.add(Merge([left, right], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.load_weights(fname)
    os.remove(fname)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    model_from_json(json_str)

    yaml_str = model.to_yaml()
    model_from_yaml(yaml_str)
Example #6
0
    def test_merge_concat(self):
        print('Test merge: concat')
        left = Sequential()
        left.add(Dense(nb_hidden, input_shape=(input_dim,)))
        left.add(Activation('relu'))

        right = Sequential()
        right.add(Dense(nb_hidden, input_shape=(input_dim,)))
        right.add(Activation('relu'))

        model = Sequential()
        model.add(Merge([left, right], mode='concat'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test], y_test))
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test], y_test))
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
        model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

        loss = model.evaluate([X_train, X_train], y_train, verbose=0)
        print('loss:', loss)
        if loss > 0.7:
            raise Exception('Score too low, learning issue.')
        model.predict([X_test, X_test], verbose=0)
        model.predict_classes([X_test, X_test], verbose=0)
        model.predict_proba([X_test, X_test], verbose=0)
        model.get_config(verbose=0)

        print('test weight saving')
        fname = 'test_merge_concat_temp.h5'
        model.save_weights(fname, overwrite=True)
        left = Sequential()
        left.add(Dense(nb_hidden, input_shape=(input_dim,)))
        left.add(Activation('relu'))

        right = Sequential()
        right.add(Dense(nb_hidden, input_shape=(input_dim,)))
        right.add(Activation('relu'))

        model = Sequential()
        model.add(Merge([left, right], mode='concat'))

        model.add(Dense(nb_class))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        model.load_weights(fname)
        os.remove(fname)

        nloss = model.evaluate([X_train, X_train], y_train, verbose=0)
        assert(loss == nloss)
Example #7
0
def test_siamese_1():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Siamese(Dense(nb_hidden), [left, right], merge_mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss < 0.8)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)
    model.get_config(verbose=0)

    # test weight saving
    fname = 'test_siamese_1.h5'
    model.save_weights(fname, overwrite=True)
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    model = Sequential()
    model.add(Siamese(Dense(nb_hidden), [left, right], merge_mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))

    model.load_weights(fname)
    os.remove(fname)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)
Example #8
0
    def test_sequential(self):
        print('Test sequential')
        model = Sequential()
        model.add(Dense(nb_hidden, input_shape=(input_dim,)))
        model.add(Activation('relu'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=1, validation_data=(X_test, y_test))
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=2, validation_data=(X_test, y_test))
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2, validation_split=0.1)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=1, validation_split=0.1)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
        model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

        model.train_on_batch(X_train[:32], y_train[:32])

        loss = model.evaluate(X_train, y_train, verbose=0)
        print('loss:', loss)
        if loss > 0.7:
            raise Exception('Score too low, learning issue.')
        model.predict(X_test, verbose=0)
        model.predict_classes(X_test, verbose=0)
        model.predict_proba(X_test, verbose=0)
        model.get_config(verbose=0)

        print('test weight saving')
        fname = 'test_sequential_temp.h5'
        model.save_weights(fname, overwrite=True)
        model = Sequential()
        model.add(Dense(nb_hidden, input_shape=(input_dim,)))
        model.add(Activation('relu'))
        model.add(Dense(nb_class))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
        model.load_weights(fname)
        os.remove(fname)

        nloss = model.evaluate(X_train, y_train, verbose=0)
        assert(loss == nloss)

        # test json serialization
        json_data = model.to_json()
        model = model_from_json(json_data)

        # test yaml serialization
        yaml_data = model.to_yaml()
        model = model_from_yaml(yaml_data)
Example #9
0
def getDNN(df, random_split=None):
    df_tr, df_val = split(df, rand_ratio=random_split)
    
    X, Y = to_array(df.drop("validation", axis=1))
    Xtr, Ytr = to_array(df_tr)
    Xval, Yval = to_array(df_val)

    scaler = MinMaxScaler((0, 1))
    Xtr = scaler.fit_transform(Xtr)
    Xval = scaler.transform(Xval)

    # Start create model
    print("Create a DNN Classifier")
    model = Sequential()

    model.add(Dense(100, input_dim=Xtr.shape[1], activation='tanh'))
    model.add(PReLU())
    model.add(Dropout(0.2))
    model.add(Dense(80, activation='linear'))
    model.add(ELU(alpha=0.3))
    model.add(Dropout(0.2))
    model.add(Dense(60, activation='tanh'))
    model.add(PReLU())
    model.add(Dropout(0.2))
    model.add(Dense(40, activation='linear'))
    model.add(ELU(alpha=0.1))
    model.add(Dropout(0.2))
    model.add(Dense(15, activation='linear'))
    model.add(PReLU())
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='sigmoid'))

    # trainer = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    trainer = Adadelta(lr=0.1, tho=0.98, epsilon=1e-7)
    model.compile(loss='binary_crossentropy', optimizer=trainer)
    
    print(Ytr, Yval)
    model.fit(Xtr, Ytr, nb_epoch=30, batch_size=32, verbose=1, validation_data=(Xval, Yval))


    pred_tr = model.predict_proba(Xtr)
    pred = model.predict_proba(Xval)
    print("auc on train: {}".format(roc_auc_score(Ytr, pred_tr)))
    print("auc on validation: {}".format(roc_auc_score(Yval, pred)))

    X = scaler.fit_transform(X)
    model.fit(X, Y, nb_epoch=30, batch_size=32)
    return model, scaler
class MLP(Classifier):
    def __init__(self, runDimRed, dimRedType='', sampleType=1, numDrivers=1, numTrips=1):
        self.runDimRed = runDimRed
        super(MLP, self).__init__('MultilayerPerceptron', dimRedType, sampleType, numDrivers, numTrips)
        self.initmodel()
        self.sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

    def initmodel(self):
        self.model = Sequential()
        self.model.add(Dense(64, input_dim=50, init='uniform'))
        self.model.add(Activation('tanh'))
        self.model.add(Dropout(0.5))
        self.model.add(Dense(64, init='uniform'))
        self.model.add(Activation('tanh'))
        self.model.add(Dropout(0.5))
        self.model.add(Dense(1, init='uniform'))
        self.model.add(Activation('softmax'))

    def runClassifier(self, _driverId, numComponents=0):
        X, Y = self.randomSampler(_driverId)
        if self.runDimRed:
            X = self.dimRed(X, Y, numComponents)

        self.ids = self.globalFeatureHash[_driverId].keys()

        self.model.compile(loss='mean_squared_error', optimizer=self.sgd)
        self.model.fit(X, Y, nb_epoch=500)

        self.label = self.model.predict_proba(X).T[0]
Example #11
0
def build_and_fit_model(X_train,y_train,X_test=None,y_test=None,hn=32,dp=0.5,layers=1,epochs=1,batches=64,verbose=0):
    input_dim=X_train.shape[1]
    output_dim=len(labels_train.unique())
    Y_train=y_train.cat.rename_categories(range(len(y_train.unique())))
#     print output_dim
    model = Sequential()
    model.add(Dense(input_dim, hn, init='glorot_uniform'))
    model.add(PReLU((hn,)))
    model.add(Dropout(dp))

    for i in range(layers):
      model.add(Dense(hn, hn, init='glorot_uniform'))
      model.add(PReLU((hn,)))
      model.add(BatchNormalization((hn,)))
      model.add(Dropout(dp))

    model.add(Dense(hn, output_dim, init='glorot_uniform'))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam')
    
    if X_test is not None:
        Y_test=y_test.cat.rename_categories(range(len(y_test.unique())))
        fitting=model.fit(X_train, Y_train, nb_epoch=epochs, batch_size=batches,verbose=verbose,validation_data=(X_test,Y_test))
        test_score = log_loss(y_test, model.predict_proba(X_test,verbose=0))
    else:
        model.fit(X_train, Y_train, nb_epoch=epochs, batch_size=batches,verbose=verbose)
        fitting=0
        test_score = 0
    return test_score, fitting, model
Example #12
0
def brain(x_train, y_train, x_test, y_test):
    from keras.models import Sequential
    from keras.layers.core import Dense, Activation
    from keras.optimizers import SGD

    number_of_classes = y_train.shape[1]
    model = Sequential()

    model.add(Dense(output_dim=64, input_dim=x_train.shape[1]))
    model.add(Activation("relu"))
    model.add(Dense(output_dim=number_of_classes))
    model.add(Activation("sigmoid"))

    #model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    model.compile(loss='binary_crossentropy',
                  optimizer='sgd',#SGD(lr=0.01, momentum=0.9, nesterov=True),
                  metrics=['accuracy']
                 )
    model.fit(x_train, y_train, nb_epoch=5, batch_size=32)

    loss_and_metrics = model.evaluate(x_test, y_test, batch_size=32)
    print("Metrics:")
    print(loss_and_metrics)

    classes = model.predict_classes(x_test, batch_size=32)
    proba = model.predict_proba(x_test, batch_size=32)
Example #13
0
def f_nn(params):   
    from keras.models import Sequential
    from keras.layers.core import Dense, Dropout, Activation
    from keras.optimizers import Adadelta, Adam, rmsprop

    print ('Params testing: ', params)
    model = Sequential()
    model.add(Dense(output_dim=params['units1'], input_dim = X.shape[1])) 
    model.add(Activation(params['activation1']))
    model.add(Dropout(params['dropout1']))

    model.add(Dense(output_dim=params['units2'], init = "glorot_uniform")) 
    model.add(Activation(params['activation2']))
    model.add(Dropout(params['dropout2']))

    if params['choice']['layers']== 'three':
        model.add(Dense(output_dim=params['choice']['units3'], init = "glorot_uniform")) 
        model.add(Activation(params['choice']['activation3']))
        model.add(Dropout(params['choice']['dropout3']))    

    model.add(Dense(2))
    model.add(Activation('sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer=params['optimizer'])

    model.fit(X, y, nb_epoch=params['nb_epochs'], batch_size=params['batch_size'], verbose = 1)

    pred_auc =model.predict_proba(X_val, batch_size = 128, verbose = 1)
    acc = log_loss(y_val, pred_auc)

    print("\n")
    print('logloss:', acc)
    sys.stdout.flush() 
    return {'loss': acc, 'status': STATUS_OK}
Example #14
0
def train(in_dim, out_dim, X_train, Y_train, X_test, Y_test):
    model = Sequential()
    model.add(Dense(100000, input_dim = in_dim, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(100000, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(out_dim, init='uniform'))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='sgd',\
            metrics=['accuracy'])

    hist = model.fit(X_train, Y_train, nb_epoch=5, batch_size=32,\
            validation_split=0.1, shuffle=True)
    print(hist.history)

    loss_and_metrics = model.evaluate(X_test, Y_test, batch_size=32)

    classes = model.predict_classes(X_test, batch_size=32)

    proba = model.predict_proba(X_test, batch_size=32)
Example #15
0
def test_merge_recursivity():
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    right = Sequential()
    right.add(Dense(nb_hidden, input_shape=(input_dim,)))
    right.add(Activation('relu'))

    righter = Sequential()
    righter.add(Dense(nb_hidden, input_shape=(input_dim,)))
    righter.add(Activation('relu'))

    intermediate = Sequential()
    intermediate.add(Merge([left, right], mode='sum'))
    intermediate.add(Dense(nb_hidden))
    intermediate.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([intermediate, righter], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
    assert(loss < 0.7)

    model.predict([X_test, X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test, X_test], verbose=0)
    model.get_config(verbose=0)

    fname = 'test_merge_recursivity_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
    assert(loss == nloss)
Example #16
0
	def classifier(self):
		level='one'
		data_instance=Load_scale_data(self.config)
		X_0,X_1,uid_0,uid_1=data_instance.load_train_X_separate()
		X_0_train,X_0_test=train_test_split(X_0,test_size=0.2)
		X_1_train,X_1_test=train_test_split(X_1,test_size=0.2)
		
		X_train=np.vstack((X_1_train,X_0_train))
		X_test=np.vstack((X_1_test,X_0_test))

		y_train=np.hstack((np.ones(len(X_1_train)),np.zeros(len(X_0_train))))	
		y_test=np.hstack((np.ones(len(X_1_test)),np.zeros(len(X_0_test))))


		batch_size = 128
		nb_classes = 2
		nb_epoch = 10

		# the data, shuffled and split between train and test sets

		X_train = X_train.astype('float32')
		X_test = X_test.astype('float32')

		print(X_train.shape[0], 'train samples')
		print(X_test.shape[0], 'test samples')

		# convert class vectors to binary class matrices
		Y_train = np_utils.to_categorical(y_train, nb_classes)
		Y_test = np_utils.to_categorical(y_test, nb_classes)


		model = Sequential() 
		model.add(Dense(50,input_shape=(X_train.shape[1],)))
		model.add(Activation('relu'))
		model.add(Dropout(0.2))
		model.add(Dense(50)) 
		model.add(Activation('linear'))
		model.add(Dropout(0.2))
		model.add(Dense(2))
		model.add(Activation('sigmoid'))

		#model.summary()
		sgd = SGD(lr=0.001, decay=1e-6, momentum=0.01, nesterov=True)
		model.compile(loss='binary_crossentropy', optimizer=sgd,metrics=['accuracy'])

		history = model.fit(X_train, Y_train,
                    batch_size=batch_size, nb_epoch=nb_epoch,
                    verbose=1, validation_data=(X_test, Y_test))

		score = model.evaluate(X_test, Y_test, verbose=0)
		y_pred=model.predict_proba(X_test)
		print y_pred
		print len(y_pred[:,0]),sum(y_pred[:,0])

		auc_score=metrics.roc_auc_score(y_test,y_pred[:,0])
		print auc_score
		print('Test score:', score[0])
		print('Test accuracy:', score[1])
Example #17
0
def test_merge_overlap():
    (X_train, y_train), (X_test, y_test) = _get_test_data()
    left = Sequential()
    left.add(Dense(nb_hidden, input_shape=(input_dim,)))
    left.add(Activation('relu'))

    model = Sequential()
    model.add(Merge([left, left], mode='sum'))
    model.add(Dense(nb_class))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, y_test))
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2, validation_split=0.1)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, shuffle=False)

    model.train_on_batch(X_train[:32], y_train[:32])

    loss = model.evaluate(X_test, y_test, verbose=0)
    model.predict(X_test, verbose=0)
    model.predict_classes(X_test, verbose=0)
    model.predict_proba(X_test, verbose=0)

    fname = 'test_merge_overlap_temp.h5'
    print(model.layers)
    model.save_weights(fname, overwrite=True)
    print(model.trainable_weights)

    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate(X_test, y_test, verbose=0)
    assert(loss == nloss)

    # test serialization
    config = model.get_config()
    new_model = Sequential.from_config(config)

    model.summary()
    json_str = model.to_json()
    new_model = model_from_json(json_str)

    yaml_str = model.to_yaml()
    new_model = model_from_yaml(yaml_str)
Example #18
0
def glm_bernoulli_keras(Xr, Yr, Xt):
    model = Sequential()
    model.add(Dense(1, input_dim=np.shape(Xr)[1], init='uniform', activation='linear'))
    model.add(Lambda(lambda x: np.exp(x)))
    #model.compile(loss='poisson', optimizer='rmsprop')
    model.compile(loss='binary_crossentropy',optimizer='rmsprop')
    model.fit(Xr, Yr, nb_epoch=3, batch_size=16, verbose=0, validation_split=0.0)
    Yt = model.predict_proba(Xt, verbose=0)
    return Yt[:,0]
Example #19
0
def test_merge_concat():
    (X_train, y_train), (X_test, y_test) = _get_test_data()

    left = Sequential(name="branch_1")
    left.add(Dense(nb_hidden, input_shape=(input_dim,), name="dense_1"))
    left.add(Activation("relu", name="relu_1"))

    right = Sequential(name="branch_2")
    right.add(Dense(nb_hidden, input_shape=(input_dim,), name="dense_2"))
    right.add(Activation("relu", name="relu_2"))

    model = Sequential(name="merged_branches")
    model.add(Merge([left, right], mode="concat", name="merge"))
    model.add(Dense(nb_class, name="final_dense"))
    model.add(Activation("softmax", name="softmax"))
    model.compile(loss="categorical_crossentropy", optimizer="rmsprop")

    model.fit(
        [X_train, X_train],
        y_train,
        batch_size=batch_size,
        nb_epoch=nb_epoch,
        verbose=0,
        validation_data=([X_test, X_test], y_test),
    )
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)
    model.get_config()

    fname = "test_merge_concat_temp.h5"
    model.save_weights(fname, overwrite=True)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert loss == nloss
def blstm_II_2layers(X, y, word_vec_len=300, batch_size=100, nb_epoch=10, threshold=0.5):
    words_size = 13033
    max_sentence_length = 47   # cut texts after this number of words (among top max_features most common words)

    # 数据shuffle
    indices = permutation(X.shape[0])  # shape[0]表示第0轴的长度,通常是训练数据的数量
    X = X[indices]
    y = y[indices]

    X_train, X_test = X[:0.9*len(X)], X[0.9*len(X):]
    y_train, y_test = y[:0.9*len(y)], y[0.9*len(y):]
    print len(X_train), 'train sequences'
    print len(X_test), 'test sequences'

    print "Pad sequences (samples x time)"
    X_train = sequence.pad_sequences(X_train, maxlen=max_sentence_length)
    X_test = sequence.pad_sequences(X_test, maxlen=max_sentence_length)
    print 'X_train shape:', X_train.shape
    print 'X_test shape:', X_test.shape
    print 'y_train shape:', y_train.shape
    print 'y_test shape:', y_test.shape

    print('Build model...')
    model = Sequential()

    model.add(Embedding(words_size+1, word_vec_len))
    # Stacked up BiDirectionLSTM layers
    model.add(BiDirectionLSTM(word_vec_len, 100, output_mode='sum', return_sequences=True))
    model.add(BiDirectionLSTM(100, 100, output_mode='sum', return_sequences=True))

    # MLP layers
    model.add(Reshape(100 * max_sentence_length, ))
    model.add(BatchNormalization((100 * max_sentence_length,)))
    model.add(Dense(100 * max_sentence_length, 50, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(50, 1, activation='sigmoid'))

    # sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    # model.compile(loss='binary_crossentropy', optimizer=sgd, class_mode='binary')
    model.compile(loss='binary_crossentropy', optimizer='adagrad', class_mode='binary')

    print("Train...")
    model.fit(X_train, y_train, shuffle=True, batch_size=batch_size, nb_epoch=nb_epoch, validation_split=0.1, show_accuracy=True)

    probas = model.predict_proba(X_test)
    for threshold in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]:
        classes = get_class(probas, threshold)
        accuracy = accuracy_score(y_test, classes)
        precision = precision_score(y_test, classes)
        recall = recall_score(y_test, classes)
        f1 = f1_score(y_test, classes)
        print '========阈值为%f时的结果========' % threshold
        print 'accuracy:', accuracy
        print 'precision:', precision
        print 'recall:', recall
        print 'F1:', f1
Example #21
0
class KerasNNReg(BaseEstimator):
    def __init__(self, dims=66, nb_classes=1, nb_epoch=30, learning_rate=0.5, validation_split=0.0, batch_size=64,
                 loss='mean_absolute_error', verbose=1):
        self.dims = dims
        self.nb_classes = nb_classes
        self.nb_epoch = nb_epoch
        self.learning_rate = learning_rate
        self.validation_split = validation_split
        self.batch_size = batch_size
        self.loss = loss
        self.verbose = verbose

        # Embedding
        # model_store = Sequential()
        # model_store.add(Embedding(1115, 50, input_length=1))
        # model_store.add(Reshape(dims=(50,)))
        # models.append(model_store)


        self.model = Sequential()
        # self.model.add(Merge(models, mode='concat'))
        # Keras model
        self.model.add(Dense(output_dim=1024, input_dim=dims, init='uniform'))
        self.model.add(Activation('sigmoid'))
        self.model.add(BatchNormalization())
        #self.model.add(Dropout(0.0))

        self.model.add(Dense(output_dim=512, init='uniform'))
        self.model.add(Activation('sigmoid'))
        self.model.add(BatchNormalization())
        #self.model.add(Dropout(0.0))

        #self.model.add(Dense(output_dim=256, init='uniform'))
        #self.model.add(Activation('relu'))
        # self.model.add(Dropout(0.1))

        self.model.add(Dense(1))
        # self.model.add(Activation('sigmoid'))

        print('Compiling Keras Deep Net with loss: %s' % (str(loss)))
        self.model.compile(loss=loss, optimizer='rmsprop')

    def fit(self, X, y, sample_weight=None):
        print('Fitting  Keras Deep Net for regression with batch_size %d, epochs %d  and learning rate: %f' % (
        self.batch_size, self.nb_epoch, self.learning_rate))
        self.model.fit(X, y, nb_epoch=self.nb_epoch, batch_size=self.batch_size,
                       validation_split=self.validation_split)

    def predict_proba(self, Xtest):
        ypred = self.model.predict_proba(Xtest, batch_size=self.batch_size, verbose=self.verbose)
        return ypred

    def predict(self, Xtest):
        ypred = self.model.predict(Xtest, batch_size=self.batch_size, verbose=self.verbose)
        return ypred
def build_model(x_data, y_data, x_test, y_test):
    model = Sequential()
    model.add(Dense(input_shape=[x_data.shape[1]],output_dim=100, kernel_initializer= initializers.RandomNormal()))
    model.add(Activation('linear'))
    #model.add(Dropout(0.4))
    #model.add(Activation('relu'))
    model.add(Dense(output_dim=100))
    model.add(Activation('tanh'))
#    model.add(Dense(output_dim=100))
#    model.add(Activation('relu'))
    model.add(Dense(output_dim=3))
    model.add(Activation('softmax'))
    sgd = SGD(lr=0.01, decay=0.0001, momentum=0.2, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    model.fit(x_data, y_data, batch_size=100, nb_epoch=20, shuffle=True, validation_split=0.2)
    preds = model.predict_proba(x_data)
    result = metrics.roc_auc_score(y_data[:,2], preds[:,2])
    print(result)
    print("test")
    preds = model.predict_proba(x_test)
    pred1 = utils.to_categorical(model.predict_classes(x_test))
    for i in range(0, 50):
        print("result=%.4f, %.4f" %(preds[i][0], y_test[i][2])) 
    result = metrics.roc_auc_score(y_test[:,2], preds[:,2])
    print(result)
    print(y_test[0])
    print(pred1[0])
    result = confusion_matrix(y_test, pred1)
    print(result)
    num = [0.0001, 0, 0]
    res_list = numpy.hstack((y_test[:,2].reshape(y_test.shape[0],1), preds[:,2].reshape(preds.shape[0],1)))
    res_sorted = sorted(res_list, key = lambda x:x[1], reverse = True)
    for i in range(0, y_test.shape[0]):
        if  i<=3 and res_sorted[i][1] >= 0.4:
            print("single result = %s " %(res_sorted[i]))
            num[0] += 1
        if res_sorted[i][1]>=0.4 and res_sorted[i][0]== 1  and i<=3:
            num[1] += 1
        if y_test[i][2] == 1:
           num[2] += 1
    print("process result = %.4f\t%d\t%d\t%dt\t%.4f" %(num[0], num[1], num[2], preds.shape[0], num[1]/num[0]))
def build_model(x_data, y_data, x_test, y_test):
    model = Sequential()
    model.add(Dense(input_shape=[x_data.shape[1]],output_dim=1000, kernel_initializer= initializers.RandomNormal()))
    model.add(Activation('tanh'))
#    model.add(Dropout(0.4))
#    model.add(Activation('linear'))
    model.add(Dense(output_dim=1000, kernel_initializer= initializers.RandomNormal()))
    model.add(Activation('tanh'))
#    model.add(Dense(output_dim=100))
#    model.add(Activation('relu'))
    model.add(Dense(output_dim=1, kernel_initializer= initializers.RandomNormal()))
    model.add(Activation('sigmoid'))
    sgd = SGD(lr=0.005, decay=0.0001, momentum=0.2, nesterov=True)
    model.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer=sgd)
#    model.fit(x_data, y_data, batch_size=4, nb_epoch=20, shuffle=True, validation_split=0.2)
    model.fit(x_data, y_data)
    preds = model.predict_proba(x_data)
    result = metrics.roc_auc_score(y_data, preds)
    print(result)
    print("test")
    preds = model.predict_proba(x_test)
    for i in range(0, 10):
        print("result=%.4f, %.4f" %(preds[i], y_test[i])) 
    result = metrics.roc_auc_score(y_test, preds)
    print(result)
    num = [0.0001, 0, 0]
    res_list = numpy.hstack((y_test, preds))
    res_sorted = sorted(res_list, key = lambda x:x[1], reverse = True)
    for i in range(0, y_test.shape[0]):
        if  i<=3 and res_sorted[i][1] >= 0.5:
            print("single result = %s" %(res_sorted[i]))
            num[0] += 1
        if res_sorted[i][1]>=0.5 and res_sorted[i][0]== 1  and i<=3:
            num[1] += 1
        if y_test[i] == 1:
           num[2] += 1
    for i in model.model.metrics_names:
        print("gradient update = %s" %(i))
    print("process_result: %.4f %d %d %d %.4f" %(num[0], num[1], num[2], preds.shape[0], num[1]/num[0]))
def build_NN(X_train,y_train_nn,X_cal,y_cal,X_test):
    cal_prob,test_prob = 0,0
    nb_classes = y_train_nn.shape[1]
    dims = X_train.shape[1]

    for i in range(5):
        print("--Building and Training model %s" % i)
        seed = randrange(1,10000)
        np.random.seed(seed)

        model = Sequential()

        model.add(Dense(dims, 512, init='glorot_uniform'))
        model.add(PReLU((512,)))
        model.add(BatchNormalization((512,)))
        model.add(Dropout(0.5))

        model.add(Dense(512, 512, init='glorot_uniform'))
        model.add(PReLU((512,)))
        model.add(BatchNormalization((512,)))
        model.add(Dropout(0.5))

        model.add(Dense(512, 512, init='glorot_uniform'))
        model.add(PReLU((512,)))
        model.add(BatchNormalization((512,)))
        model.add(Dropout(0.5))

        model.add(Dense(512, nb_classes, init='glorot_uniform'))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer="adam")

        model.fit(X_train, y_train_nn, nb_epoch=20, batch_size=16, validation_split=0.15)

        cal_prob += model.predict_proba(X_cal)
        test_prob += model.predict_proba(X_test)
    cal_prob,test_prob = cal_prob/10.,test_prob/10.
    print("Avg NN Model Loss: %0.4f" % of.logloss_mc(y_cal,cal_prob))
    return(cal_prob,test_prob)
Example #25
0
def testNN(X_train, X_test, y_train, y_test, hiddenSize=256, nepochs=500, dropouts=0.25):
    """
    Train a Neural Network with dropouts and predict test data.

    :param X_train: training data
    :param X_test: test data
    :param y_train: training labels
    :param y_test: test labels
    :param hiddenSize: set the number of hidden nodes in each layer
    :param nepochs: number of times to update the weights
    :param dropouts: dropout fraction - randomly drop units from each layer

    :return: predictions for the test data
    """
    # specify sequential model - input, two middle layers, and output layer
    model = Sequential()

    # input layer
    model.add(Dense(hiddenSize, input_dim=X_train.shape[1], init='glorot_uniform', activation='relu'))
    model.add(Dropout(dropouts))

    # hidden layers
    model.add(Dense(hiddenSize, activation='relu'))
    model.add(Dropout(dropouts))
    model.add(Dense(hiddenSize, activation='relu'))
    model.add(Dropout(dropouts))

    # output layer
    model.add(Dense(1, activation='sigmoid'))

    # set the optimizer and compile
    opt = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    #opt = RMSprop(lr=0.001, rho=0.9, epsilon=1e-06)
    model.compile(loss='binary_crossentropy', optimizer=opt)

    # fit the model using training data
    model.fit(X_train, y_train, nb_epoch=nepochs, batch_size=8192,
              validation_split=0.1, show_accuracy=True, verbose=1,
              callbacks=[EarlyStopping(monitor='val_loss', patience=10)])
    print('Training completed...')

    # make a figure of the model
    keras.utils.visualize_util.plot(model, to_file='model.png')

    print('predict test data...')
    yhat = model.predict_proba(X_test, batch_size=5000)
    auc = metrics.roc_auc_score(y_test, yhat)
    print('Results:')
    print('NN AUC:', auc)

    return yhat
Example #26
0
def build_model(x_data, y_data, x_test, y_test):
    model = Sequential()
    model.add(LSTM(input_shape=[x_data.shape[1], 1],  output_dim =10, return_sequences = True))
    model.add(Flatten())
    model.add(Activation('linear'))
    model.add(Dense(output_dim=20))
    model.add(Activation('linear'))
    model.add(Dense(output_dim=10))
    model.add(Activation('tanh'))
    model.add(Dense(output_dim=4))
    model.add(Activation('sigmoid'))
    sgd = SGD(lr=0.1, decay=1e-5, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)

    model.fit(x_data, y_data, batch_size=1000, nb_epoch=50, validation_split=0.2)
    preds = model.predict_proba(x_data)
    result = metrics.roc_auc_score(y_data, preds)
    print(result)

    preds = model.predict_proba(x_test)
    result = metrics.roc_auc_score(y_test, preds)
    print(result)
    return model
Example #27
0
    def test_merge_recursivity(self):
        print('Test merge recursivity')

        left = Sequential()
        left.add(Dense(input_dim, nb_hidden))
        left.add(Activation('relu'))

        right = Sequential()
        right.add(Dense(input_dim, nb_hidden))
        right.add(Activation('relu'))

        righter = Sequential()
        righter.add(Dense(input_dim, nb_hidden))
        righter.add(Activation('relu'))

        intermediate = Sequential()
        intermediate.add(Merge([left, right], mode='sum'))
        intermediate.add(Dense(nb_hidden, nb_hidden))
        intermediate.add(Activation('relu'))

        model = Sequential()
        model.add(Merge([intermediate, righter], mode='sum'))

        model.add(Dense(nb_hidden, nb_class))
        model.add(Activation('softmax'))

        model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_data=([X_test, X_test, X_test], y_test))
        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=False, verbose=0, validation_split=0.1)
        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
        model.fit([X_train, X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

        loss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
        print('loss:', loss)
        if loss > 0.6:
            raise Exception('Score too low, learning issue.')
        preds = model.predict([X_test, X_test, X_test], verbose=0)
        classes = model.predict_classes([X_test, X_test, X_test], verbose=0)
        probas = model.predict_proba([X_test, X_test, X_test], verbose=0)
        print(model.get_config(verbose=1))

        model.save_weights('temp.h5', overwrite=True)
        model.load_weights('temp.h5')

        nloss = model.evaluate([X_train, X_train, X_train], y_train, verbose=0)
        print(nloss)
        assert(loss == nloss)
Example #28
0
def run_neural_net(X, y):
    print 'running neural network...'
    model = Sequential()
    
    #split 80/20 train test
    sss = StratifiedShuffleSplit(y, n_iter = 1, test_size = 0.2, random_state = 42)
    for train_index, test_index in sss:
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

    #Oversampling of unbalanced dataset
    sm = SMOTE(kind = 'regular', verbose = True)
    X_train, y_train = sm.fit_transform(X_train, y_train)
    X_train, y_train = sm.fit_transform(X_train, y_train)

    y_train = y_train.reshape(y_train.shape[0],1)
    y_test = y_test.reshape(y_test.shape[0],1)
    y_train, y_test = [np_utils.to_categorical(x) for x in (y_train, y_test)]
    # Dense(64) is a fully-connected layer with 64 hidden units.
    # in the first layer, you must specify the expected input data shape:
    # here, 20-dimensional vectors.
    #tr = ThresholdedReLU(theta = 0.3)
    model.add(Dense(input_dim=X.shape[1], output_dim=1000, init='uniform',activation='relu'))
    #model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(output_dim=1000, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(output_dim=1000, init='uniform'))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(output_dim=2, init='uniform'))
    model.add(Activation('softmax'))
    #sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='binary_crossentropy', optimizer='adam')
    model.fit(X_train, y_train, nb_epoch=10, batch_size = 200)
    score = model.evaluate(X_test, y_test, show_accuracy=True)
    pred = model.predict_proba(X_test) #get back probabilities
    pred2 = model.predict_classes(X_test) #get back predictions
    fpr, tpr, thresholds = roc_curve(y_test[:,1], pred[:,1])
    
    #get the AUC
    AUC = roc_auc_score(y_test[:,1], pred[:,1])
    
    #get the AUC for precision and recall curve
    AUC2 = average_precision_score(y_test[:,1], pred[:,1])
    recall = recall_score(y_test[:,1], pred2)
    precision = precision_score(y_test[:,1], pred2)
    print score
    return model, X_train, y_train, X_test, y_test, score
Example #29
0
def train_ensemble(X_train, X_test, Y_train, n_models=6):
    n_categs = Y_train.shape[1]
    for i in range(n_models):
        print('---' * 20)
        print('Training model #: {}'.format(i + 1))
        print('---' * 20)

        model = Sequential()

        dim = random.choice(np.arange(512, 769))
        model.add(Dense(output_dim=dim, input_dim=X_train.shape[1],
                        init="glorot_uniform", W_constraint=maxnorm(1)))
        model.add(PReLU())
        model.add(Dropout(0.6))

        model.add(Dense(output_dim=n_categs, init="glorot_uniform",
                        W_constraint=maxnorm(1)))
        model.add(Activation("softmax"))
        model.compile(loss='categorical_crossentropy',
                      optimizer='adagrad')
        print("Training model...")

        epoch = random.choice(np.arange(100, 400))

        model.fit(X_train, Y_train, nb_epoch=epoch, batch_size=512)

        if i == 0:
            probs = model.predict_proba(X_test, batch_size=512)
        else:
            probs += model.predict_proba(X_test, batch_size=512)

    probs /= n_models
    column_names, index = get_extra()
    df_out = pd.DataFrame(data=np.c_[index, probs], columns=column_names)
    df_out['VisitNumber'] = np.int32(df_out['VisitNumber'])
    df_out.to_csv('nnEnsemble.csv', index=False)
    return df_out
Example #30
0
def test_merge_concat():
    (X_train, y_train), (X_test, y_test) = _get_test_data()

    left = Sequential(name='branch_1')
    left.add(Dense(nb_hidden, input_shape=(input_dim,), name='dense_1'))
    left.add(Activation('relu', name='relu_1'))

    right = Sequential(name='branch_2')
    right.add(Dense(nb_hidden, input_shape=(input_dim,), name='dense_2'))
    right.add(Activation('relu', name='relu_2'))

    model = Sequential(name='merged_branches')
    model.add(Merge([left, right], mode='concat', name='merge'))
    model.add(Dense(nb_class, name='final_dense'))
    model.add(Activation('softmax', name='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_data=([X_test, X_test], y_test))
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, validation_split=0.1)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0, shuffle=False)

    loss = model.evaluate([X_test, X_test], y_test, verbose=0)

    model.predict([X_test, X_test], verbose=0)
    model.predict_classes([X_test, X_test], verbose=0)
    model.predict_proba([X_test, X_test], verbose=0)
    model.get_config()

    fname = 'test_merge_concat_temp.h5'
    model.save_weights(fname, overwrite=True)
    model.fit([X_train, X_train], y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=0)
    model.load_weights(fname)
    os.remove(fname)

    nloss = model.evaluate([X_test, X_test], y_test, verbose=0)
    assert(loss == nloss)