from keras import models from keras import layers from keras.callbacks import EarlyStopping, ModelCheckpoint from keras.applications import MobileNet from keras.models import Model from keras.applications.vgg16 import VGG16 from keras.layers import BatchNormalization, Flatten, Dense, LeakyReLU, Dropout, GaussianNoise from tensorflow import set_random_seed from keras.optimizers import Adam, RMSprop from keras_gradient_noise import add_gradient_noise from keras.initializers import glorot_uniform, glorot_normal, he_uniform, he_normal set_random_seed(1724) model = models.Sequential() model.add( Dense(128, kernel_initializer="glorot_normal", input_shape=(9 * 128, ))) model.add(LeakyReLU()) model.add(Dropout(.15)) model.add(BatchNormalization()) # model.add(GaussianNoise(1)) model.add(Dense(64)) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(Dense(6, activation='softmax')) # In[85]: # Let's use a different optimizer this time noisy = add_gradient_noise(RMSprop) model.compile( optimizer="Adamax",
def perform_learning(train_features, test_features, f_output, local, gglobal, deep=False): print(train_features[0]) if (local and gglobal): train = [x[1:-1] for x in train_features] test = [x[1:-1] for x in test_features] elif (local and not gglobal): train = [x[1:5] for x in train_features] test = [x[1:5] for x in test_features] elif (not local and gglobal): train = [x[5:-1] for x in train_features] test = [x[5:-1] for x in test_features] # print train[0] train_tags = [x[-1] for x in train_features] test_tags = [x[-1] for x in test_features] train = z_scoring(train) test = z_scoring(test) print(len(train[0])) print(train[0]) if not deep: algos = ['adaBoost', 'RF', 'L-SVM', 'RBF-SVM', 'SGD'] # algos = ['RBF-SVM'] for algo in algos: print(algo) f_output.writelines(algo + '\n') if algo == 'adaBoost': clf = AdaBoostClassifier(n_estimators=100) if algo == 'RF': clf = RandomForestClassifier(n_estimators=1000, criterion="gini", min_samples_split=15, oob_score=True, class_weight='balanced', max_depth=3) if algo == 'L-SVM': clf = SVC(kernel='linear', class_weight="balanced", C=0.01, probability=True) if algo == 'RBF-SVM': clf = SVC(class_weight="balanced", C=0.01, probability=True) if algo == 'SGD': clf = SGDClassifier(alpha=0.0001, average=False, class_weight=None, epsilon=0.1, eta0=0.0, fit_intercept=True, l1_ratio=0.15, learning_rate='optimal', loss='hinge', n_iter=5, n_jobs=1, penalty='l2', power_t=0.5, random_state=None, shuffle=True, verbose=0, warm_start=False) # print train clf.fit(train, train_tags) if (algo == 'RF'): print(len(clf.feature_importances_)) print(clf.feature_importances_) f_output.writelines(str(clf.feature_importances_) + '\n') evaluate_auc(clf, test, test_tags, train, train_tags, f_output) else: print(train[0]) from keras.models import Sequential from keras.layers import Dense, Dropout from keras.regularizers import l2, l1_l2 clf = Sequential() clf.add( Dense(100, activation="relu", kernel_initializer="he_normal", input_dim=train.shape[1])) # self.classifier.add(Dropout(0.5)) # self.classifier.add(Dense(100, init='he_normal', activation='relu', W_regularizer=l2(0.5))) clf.add(Dropout(0.1)) clf.add( Dense(1, init='uniform', activation='sigmoid', W_regularizer=l1_l2(0.2))) clf.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) clf.fit(train, train_tags, validation_data=[test, test_tags], epochs=100, batch_size=10, verbose=2) evaluate_auc(clf, test, test_tags, train, train_tags, f_output)