def main(): train_data = get_data(TRAIN_DATASET) test_data = get_data(TEST_DATASET) feature_extraction_fn_name = ARGS.feature_extraction_fn tagger = MaxentPosTagger(feature_extraction_fn=getattr( feature_extraction, feature_extraction_fn_name)) tagger.train(train_data) test(tagger, test_data)
def train_on_noisy(noise_rat=1.0, num_epochs=30, nb_labelled = 50000, batch_size = 100, \ wd=0.0, lr = 0.1, dataset = 'cifar10'): net_w = ResNet18().cuda() optimizer_w = optim.SGD([{ 'params': net_w.parameters() }], lr, momentum=0.9, weight_decay=wd) trainloader_l = get_loaders_cifar(nb_labelled, dataset, batch_size, noise_rat) # get the loss for the random weight to have equal starts: _, loss = test(net_w, nll_loss, trainloader_l) losses = [loss[0]] for epoch in range(num_epochs): st_time = time.time() _, loss = train_w(net_w, optimizer_w, nll_loss, trainloader_l) losses.append(loss[0]) print ("noise_rat=%f, epoch=%d, time=%f, loss=%f" % \ (noise_rat, epoch, time.time()-st_time, loss[0])) return losses
user_continue = True while user_continue: # get user response response = input(available_features).lower().strip("!?,. ") replit.clear() if response == "learn": learn_counter = 0 helper.learn(current_scene, learn_counter) elif response == "speech": helper.speech() elif response == "test": test_score = helper.test(current_scene) high_scores = helper.set_high_score(current_scene, test_score, high_scores) elif response == "move": new_scene = helper.move() helper.show_scene(new_scene) current_scene = new_scene elif response == "score": helper.show_high_score(high_scores) elif response == "exit": user_continue = False else:
torch.cuda.set_device(args.gpu) logging.info('generate config') pretrained_embedding = pkl.load(open(args.emb_file)) config = Config(vocab_size=pretrained_embedding.shape[0], embedding_dim=pretrained_embedding.shape[1], position_size=500, position_dim=50, word_input_size=100, sent_input_size=2 * args.hidden, word_GRU_hidden_units=args.hidden, sent_GRU_hidden_units=args.hidden, pretrained_embedding=pretrained_embedding) word2id = pkl.load(open('../data/word2id.pkl')) logging.info('loadding test dataset') test_dataset = pkl.load(open(args.test_file)) test_loader = DataLoader(test_dataset, shuffle=False) net = SummaRuNNer(config).cuda() net.load_state_dict(torch.load(args.model_file)) for index, docs in enumerate(test_loader): doc = docs[0] x, y = prepare_data(doc, word2id) sents = Variable(torch.from_numpy(x)).cuda() outputs = net(sents) hyp, gold, predict = test(doc, outputs.data.tolist(), index)
model = createModel(modelPackage) modelPackage.model = model # plot_model(model, to_file='model.png') precision = 0 recoil = 0 f1 = 0 file = open("Results/"+str(datasetName)+str(samplingMethod) + str(modelName)+ "_Results.txt", "w+") #ctive Learning Starts here if datasetName == "Twitter": #Initial Training The Model model = train(model, train_batch, epochs, modelPackage) pre_test, rec_test, f1_test = test(model, test_batch, idx2Label, modelPackage) file.write("Initial Precision: " + str(pre_test) + " Initial Recoil : "+str(rec_test) + " Initial F1_Score : " + str(f1_test)+ " Data size : " + str(len(train_batch))+ "\n\n") file.flush() l = len(learn_batch) flagged = [] last = [] for i in range(3): last.append(0) for i in range(l): flagged.append(0) iter = 0 while(True): modelPackage.model = model #Finding the actice data active_data, flagged = active_learn(model, learn_batch, flagged, samplingMethod, modelPackage)
def main(): # read data (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() # read data (x_train, y_train, x_test, y_test) = preprocess(x_train, y_train, x_test, y_test) # Reshape the data inputs such that we can put those inputs into MLP train_inputs = np.reshape(x_train, (-1, 28 * 28)) test_inputs = np.reshape(x_test, (-1, 28 * 28)) orig_model = MLP(28 * 28) # train original model epochs = 10 print("Training Original MLP...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_acc = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_acc)) # calculate fgs and deepfool for original model, on both test set and training set print("Creating DeepFool images set... will take aobut 5 mins") (train_adv_orig, train_r_orig) = deepfool(orig_model, train_inputs) (test_adv_orig, test_r_orig) = deepfool(orig_model, test_inputs) # fine tuning tuning_model = MLP_tuning(28 * 28, orig_model) epochs = 5 print("Training Fine Tuning MLP...") for i in range(epochs): train(tuning_model, train_adv_orig, y_train) tuning_test_acc = test(tuning_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, tuning_test_acc)) # train deepdefense model regu_model = regu_MLP(28 * 28, orig_model) epochs = 5 print("Training Deep Defense MLP...") for i in range(epochs): regu_train(regu_model, train_adv_orig, y_train, train_r_orig) regu_test_acc = test(regu_model, test_adv_orig, y_test) print("Epoch: {} ------ Testing accuracy: {}".format( i + 1, regu_test_acc)) # keep training original model for comparison epochs = 5 print("Training MLP for 5 more epochs...") for i in range(epochs): train(orig_model, train_inputs, y_train) test_accu = test(orig_model, test_inputs, y_test) print("Epoch: {} ------ Testing accuracy: {}".format(i + 1, test_accu)) ################### Evaluation ######################### # ROC curve on deepfool testing image generated from origianl MLP model roc1 = roc(orig_model, test_adv_orig, y_test, "Vanilla MLP") roc2 = roc(tuning_model, test_adv_orig, y_test, "Fine tuning MLP") roc3 = roc(regu_model, test_adv_orig, y_test, "Deep Defense MLP") AUC = pd.DataFrame( { "Vanilla MLP": list(roc1.values()), "Fine-Tune MLP": list(roc2.values()), "Deep Defense": list(roc3.values()) }, index=["label " + str(i + 1) for i in range(10)]) print("Area Under the Curve:") print(AUC) # testing acc on benign images benign_test_acc = pd.DataFrame( { "Vanilla MLP": test(orig_model, test_inputs, y_test), "Fine-Tune MLP": test(tuning_model, test_inputs, y_test), "Deep Defense": test(regu_model, test_inputs, y_test) }, index=["TestAcc"]) # rho2 scores (test_adv_orig2, test_r_orig2) = deepfool(orig_model, test_inputs) (test_adv_tuning, test_r_tuning) = deepfool(tuning_model, test_inputs) (test_adv_regu, test_r_regu) = deepfool(regu_model, test_inputs) regu_rho2 = rho2(test_r_regu, test_inputs) tuning_rho2 = rho2(test_r_tuning, test_inputs) orig_rho2 = rho2(test_r_orig2, test_inputs) rho2_all = pd.DataFrame( { "Vanilla MLP": orig_rho2, "Fine-Tune MLP": tuning_rho2, "Deep Defense": regu_rho2 }, index=["Rho2 Score"]) # plot accuracy on FGS images epsilon_ref_100, epsilon_ref_50, epsilon_ref_20 = plot_acc_on_FGS( orig_model, regu_model, tuning_model, test_inputs, y_test, test_adv_orig) epsilon_list = [epsilon_ref_20, epsilon_ref_50, epsilon_ref_100] # calculating testing accuracy of vanilla, regu, and finetune on FGS examples with these three epsilon values pert_test_orig = FGS(orig_model, test_inputs, y_test, 1) pert_test_regu = FGS(regu_model, test_inputs, y_test, 1, True, test_adv_orig) pert_test_tuning = FGS(tuning_model, test_inputs, y_test, 1) FGS_orig_test_acc = list( map( lambda x: test(orig_model, x * pert_test_orig + test_inputs, y_test ), epsilon_list)) FGS_regu_test_acc = list( map( lambda x: test(regu_model, x * pert_test_regu + test_inputs, y_test ), epsilon_list)) FGS_tuning_test_acc = list( map( lambda x: test(tuning_model, x * pert_test_tuning + test_inputs, y_test), epsilon_list)) acc_fgs = pd.DataFrame( { "Vanilla MLP": FGS_orig_test_acc, "Fine-Tune MLP": FGS_tuning_test_acc, "Deep Defense": FGS_regu_test_acc }, index=["[email protected]", "[email protected]", "[email protected]"]) result_table = pd.concat([benign_test_acc, rho2_all, acc_fgs], ignore_index=False).transpose() print(result_table)
""" import met, helper import pandas as pd import numpy as np from tqdm import tqdm from sklearn.linear_model import LinearRegression np.set_printoptions(precision=3) # Define processing parameters chunk_sizes = [10, 100, 200, 300, 500, 1000] # number of training instances steps = [10, 100, 200, 300, 500, 1000] # prediction time horizon in instances methods = {"PCALR": met.PCALR(), "LR": LinearRegression()} # Read data data = np.array(pd.read_csv("params_with_output.csv").values[:, 1:]) X = data[:, :-1] y = data[:, -1] # Experimental loop scores = np.zeros((len(chunk_sizes), len(steps), len(methods))) for i, chunk_size in enumerate(tqdm(chunk_sizes)): for j, step in enumerate(tqdm(steps)): results = helper.test(chunk_size, step, X, y, methods) scores[i, j] = results # Store scores np.save("results/experiment_1", scores)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Friday Feb 20 2020 This code was implemented by Louis Weyland, Floris Fok and Julien Fer """ import helper as helper from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np ''' To test the finite method changing the different parameters default parameters: s_min=0,s_max=100,ds=1,t_max=1,dt=0.001,S0=50,K=60,r=0.05,sigma=0.2,option='put' or 'call',fm_type='forward' or 'crank-nicolson' ''' helper.test() """ Compare the FTCS with the Cran-nicolson method using different stock price S0 default parameter: s_min=0,s_max=200,ds=1,t_max=1,dt=(0.00001,0.01),S0=(100,110,120),K=110,r=0.04,sigma=0.3,option='call """ #helper.diff_S0() #helper.convergence(fm_type='crank-nicolson')