def get_all_errors(input_file, n_pairs, compensation1, compensation2): count = 1 avg_batch_error_a = [] avg_batch_error_b = [] avg_batch_error_c = [] avg_batch_time_a = [] avg_batch_time_b = [] avg_batch_time_c = [] data_array = load_data(input_file,n_pairs) N = data_array[0].size M = 2000 dataset = input_file.split('.')[1] while count < n_pairs-1: mapping = mapper(N,M) bits = mapping.bits maps = mapping.map print(count) # print ("* Input Dimension of Dataset:",N) # print ("* Output (compressed) Dimension of Dataset:",M) alpha = 1 arr1 = data_array[count-1] arr2 = data_array[count] # print ("* Selected array (1) from Dataset:",arr1) # print ("* Selected array (2) from Dataset:",arr2) norm_arr_1 = array_normalization(arr1) norm_arr_2 = array_normalization(arr2) # norm_arr_1 = arr1 # norm_arr_2 = arr2 # print ("* Normalized array (1):",norm_arr_1) # print ("* Normalized array (2):",norm_arr_2) batch_error_a, batch_time_a, batch_inner_product1_a,batch_inner_product2_a,_,_ = get_feature_deletion_results(Input_dimension = N,Output_dimension = M,default_bits=bits,default_maps=maps,array1=norm_arr_1,array2=norm_arr_2,mapping_scheme=5,max_value=alpha) # plt.plot(range(len(batch_error)), batch_error, label = "Error Without Compensation") # plt.plot(range(len(batch_inner_product1)), batch_inner_product1, label = "IP1 Without Compensation") # plt.plot(range(len(batch_inner_product2)), batch_inner_product2, label = "IP2 Without Compensation") batch_error_b, batch_time_b,batch_inner_product1_b,batch_inner_product2_b,_,_ = get_feature_deletion_results(Input_dimension = N,Output_dimension = M,default_bits=bits,default_maps=maps,array1=norm_arr_1,array2=norm_arr_2,mapping_scheme=6,max_value=alpha) batch_error_c, batch_time_c, batch_inner_product1_c,batch_inner_product2_c,_,_ = get_remap_results(Input_dimension = N,Output_dimension = M,array1=norm_arr_1,array2=norm_arr_2,mapping_scheme=6) # batch_error_c,batch_inner_product1_c,batch_inner_product2_c,_,_ = get_feature_deletion_results(Input_dimension = N,Output_dimension = M,default_bits=bits,default_maps=maps,array1=norm_arr_1,array2=norm_arr_2,mapping_scheme=8,max_value=alpha) # print(batch_error,batch_inner_product1,batch_inner_product2,array1,array2) # plt.plot(range(len(batch_error)), batch_error, label = "Error With Compensation") # plt.plot(range(len(batch_inner_product1)), batch_inner_product1, label = "IP1 With Compensation") # plt.plot(range(len(batch_inner_product2)), batch_inner_product2, label = "IP2 With Compensation") # plt.legend() # plt.show() if count == 1: avg_batch_error_a = batch_error_a avg_batch_error_b = batch_error_b avg_batch_error_c = batch_error_c avg_batch_time_a = batch_time_a avg_batch_time_b = batch_time_b avg_batch_time_c = batch_time_c # avg_inner_product1_a = batch_inner_product1_a # avg_inner_product2_a = batch_inner_product2_a # avg_inner_product1_b = batch_inner_product1_b # avg_inner_product2_b = batch_inner_product2_b # avg_inner_product1_c = batch_inner_product1_c # avg_inner_product2_c = batch_inner_product2_c else : for i in range(len(batch_error_a)): avg_batch_error_a[i] += batch_error_a[i] avg_batch_error_b[i] += batch_error_b[i] avg_batch_error_c[i] += batch_error_c[i] avg_batch_time_a[i] += batch_time_a[i] avg_batch_time_b[i] += batch_time_b[i] avg_batch_time_c[i] += batch_time_c[i] # avg_inner_product1_a[i] += batch_inner_product1_a[i] # avg_inner_product2_a[i] += batch_inner_product2_a[i] # avg_inner_product1_b[i] += batch_inner_product1_b[i] # avg_inner_product2_b[i] += batch_inner_product2_b[i] # avg_inner_product1_c[i] += batch_inner_product1_c[i] # avg_inner_product2_c[i] += batch_inner_product2_c[i] if count%50 == 0 or count == n_pairs-2: np.save('/home/b16032/MTP/Dimensionality-Reduction/Test Files/Outputs/insertion/13March_testing_'+dataset+'_'+str(count)+'.npy', [ (np.array(avg_batch_error_a)/count, np.array(batch_time_a)/count), (np.array(avg_batch_error_b)/count, np.array(batch_time_b)/count), (np.array(avg_batch_error_c)/count, np.array(batch_time_c)/count) ]) count += 1 for i in range(len(avg_batch_error_a)): avg_batch_error_a[i] /= n_pairs avg_batch_error_b[i] /= n_pairs avg_batch_error_c[i] /= n_pairs avg_batch_time_a[i] /= n_pairs avg_batch_time_b[i] /= n_pairs avg_batch_time_c[i] /= n_pairs # avg_inner_product1_a[i] /= n_pairs # avg_inner_product2_a[i] /= n_pairs # avg_inner_product1_b[i] /= n_pairs # avg_inner_product2_b[i] /= n_pairs # avg_inner_product1_c[i] /= n_pairs # avg_inner_product2_c[i] /= n_pairs return avg_batch_error_a, avg_batch_error_b, avg_batch_error_c, avg_batch_time_a, avg_batch_time_b, avg_batch_time_c
from os.path import abspath, exists import numpy as np from Object_Files.mapper5 import mapper from Object_Files.basic_operator import operator #import matplotlib.pyplot as plt import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt import random mapping = mapper(28102, 2000) default_bits = mapping.bits default_maps = mapping.map def array_normalization(input_array): array_norm = np.linalg.norm(input_array) # print ("array norm:",array_norm) result = np.zeros(input_array.size, dtype=float) for i in range(input_array.size): result[i] = (1.0 * input_array[i]) / array_norm return result def get_adversarial_positions(demo_operator, batch_feature_size): feature_counter = demo_operator.get_feature_counter() # print ("Originl feature counter:",feature_counter) batch_positions = [] alpha_map = np.zeros(len(feature_counter)) while len(batch_positions) < batch_feature_size:
from Object_Files.mapper5 import mapper, np from Object_Files.basic_operator import operator import matplotlib.pyplot as plt import random mapping = mapper(50000, 3000) default_bits = mapping.bits default_maps = mapping.map def array_normalization(input_array): array_norm = np.linalg.norm(input_array) # print ("array norm:",array_norm) result = np.zeros(input_array.size, dtype=float) for i in range(input_array.size): result[i] = (1.0 * input_array[i]) / array_norm return result def get_adversarial_positions(demo_operator, batch_feature_size): feature_counter = demo_operator.get_feature_counter() print("Originl feature counter:", demo_operator.get_feature_count()) batch_positions = [] alpha_map = np.zeros(len(feature_counter)) while len(batch_positions) < batch_feature_size: alpha = random.randint(0, len(feature_counter) - 1) if alpha_map[alpha] == 1: continue else: alpha_map[alpha] = 1
def main(): input_file = sys.argv[1] compensation1, compensation2, compensation3 = 0, 1, 2 # 0 = No Compensaation, 1 = 1 step Compensation, 2 = 2 step n_args = len(sys.argv) if n_args > 2: compensation1 = int(sys.argv[2]) compensation2 = int(sys.argv[3]) m1, m2 = 5, 6 #One without compensation, other with one step compensation if compensation1 == 0: m1 = 5 elif compensation1 == 1: m1 = 6 else: m1 = 8 if compensation2 == 0: m2 = 5 elif compensation2 == 1: m2 = 6 else: m2 = 8 n_pairs = 100 if n_args > 4: n_pairs = int(sys.argv[4]) count = 1 avg_batch_error_a = [] avg_batch_error_b = [] avg_inner_product1_a = [] avg_inner_product2_a = [] avg_inner_product1_b = [] avg_inner_product2_b = [] data_array = load_data(input_file, n_pairs) N = data_array[0].size M = 2000 while count < n_pairs - 1: mapping = mapper(N, M) bits = mapping.bits maps = mapping.map print(count) # print ("* Input Dimension of Dataset:",N) # print ("* Output (compressed) Dimension of Dataset:",M) alpha = 1 arr1 = data_array[count - 1] arr2 = data_array[count] # print ("* Selected array (1) from Dataset:",arr1) # print ("* Selected array (2) from Dataset:",arr2) norm_arr_1 = array_normalization(arr1) norm_arr_2 = array_normalization(arr2) # norm_arr_1 = arr1 # norm_arr_2 = arr2 # print ("* Normalized array (1):",norm_arr_1) # print ("* Normalized array (2):",norm_arr_2) batch_error_a, batch_inner_product1_a, batch_inner_product2_a, _, _ = get_feature_deletion_results( Input_dimension=N, Output_dimension=M, default_bits=bits, default_maps=maps, array1=norm_arr_1, array2=norm_arr_2, mapping_scheme=5, max_value=alpha) # plt.plot(range(len(batch_error)), batch_error, label = "Error Without Compensation") # plt.plot(range(len(batch_inner_product1)), batch_inner_product1, label = "IP1 Without Compensation") # plt.plot(range(len(batch_inner_product2)), batch_inner_product2, label = "IP2 Without Compensation") batch_error_b, batch_inner_product1_b, batch_inner_product2_b, _, _ = get_feature_deletion_results( Input_dimension=N, Output_dimension=M, default_bits=bits, default_maps=maps, array1=norm_arr_1, array2=norm_arr_2, mapping_scheme=6, max_value=alpha) batch_error_c, batch_inner_product1_c, batch_inner_product2_c, _, _ = get_remap_results( Input_dimension=N, Output_dimension=M, array1=norm_arr_1, array2=norm_arr_2, mapping_scheme=8) # batch_error_c,batch_inner_product1_c,batch_inner_product2_c,_,_ = get_feature_deletion_results(Input_dimension = N,Output_dimension = M,default_bits=bits,default_maps=maps,array1=norm_arr_1,array2=norm_arr_2,mapping_scheme=8,max_value=alpha) # print(batch_error,batch_inner_product1,batch_inner_product2,array1,array2) # plt.plot(range(len(batch_error)), batch_error, label = "Error With Compensation") # plt.plot(range(len(batch_inner_product1)), batch_inner_product1, label = "IP1 With Compensation") # plt.plot(range(len(batch_inner_product2)), batch_inner_product2, label = "IP2 With Compensation") # plt.legend() # plt.show() if count == 1: avg_batch_error_a = batch_error_a avg_batch_error_b = batch_error_b avg_batch_error_c = batch_error_c # avg_inner_product1_a = batch_inner_product1_a # avg_inner_product2_a = batch_inner_product2_a # avg_inner_product1_b = batch_inner_product1_b # avg_inner_product2_b = batch_inner_product2_b # avg_inner_product1_c = batch_inner_product1_c # avg_inner_product2_c = batch_inner_product2_c else: for i in range(len(batch_error_a)): avg_batch_error_a[i] += batch_error_a[i] avg_batch_error_b[i] += batch_error_b[i] avg_batch_error_c[i] += batch_error_c[i] # avg_inner_product1_a[i] += batch_inner_product1_a[i] # avg_inner_product2_a[i] += batch_inner_product2_a[i] # avg_inner_product1_b[i] += batch_inner_product1_b[i] # avg_inner_product2_b[i] += batch_inner_product2_b[i] # avg_inner_product1_c[i] += batch_inner_product1_c[i] # avg_inner_product2_c[i] += batch_inner_product2_c[i] count += 1 for i in range(len(avg_batch_error_a)): avg_batch_error_a[i] /= n_pairs avg_batch_error_b[i] /= n_pairs avg_batch_error_c[i] /= n_pairs # avg_inner_product1_a[i] /= n_pairs # avg_inner_product2_a[i] /= n_pairs # avg_inner_product1_b[i] /= n_pairs # avg_inner_product2_b[i] /= n_pairs # avg_inner_product1_c[i] /= n_pairs # avg_inner_product2_c[i] /= n_pairs plt.plot(range(len(avg_batch_error_a)), np.array(avg_batch_error_a)**2, label="NO Compensation") # plt.plot(range(len(avg_inner_product1_a)), avg_inner_product1_a, label = "IP1 With "+str(compensation1)+" step Compensation") # plt.plot(range(len(avg_inner_product2_a)), avg_inner_product2_a, label = "IP2 With "+str(compensation1)+" step Compensation") plt.plot(range(len(avg_batch_error_b)), np.array(avg_batch_error_b)**2, label="One Step Compensation") # plt.plot(range(len(avg_inner_product1_b)), avg_inner_product1_b, label = "IP1 With "+str(compensation2)+" step Compensation") # plt.plot(range(len(avg_inner_product2_b)), avg_inner_product2_b, label = "IP2 With "+str(compensation2)+" step Compensation") plt.plot(range(len(avg_batch_error_c)), np.array(avg_batch_error_c)**2, label="Remap") # plt.plot(range(len(avg_inner_product1_c)), avg_inner_product1_c, label = "IP1 With "+str(compensation3)+" step Compensation") # plt.plot(range(len(avg_inner_product2_c)), avg_inner_product2_c, label = "IP2 With "+str(compensation3)+" step Compensation") plt.xlabel("% of features deleted") plt.ylabel("MSE") plt.legend() #plt.show() plt.savefig( '/home/b16032/MTP/Dimensionality-Reduction/Test Files/Plots/KOS_square_27-2-2020.png' )