def main(): # sample_sizes = [int(sys.argv[1]), int(sys.argv[2])] sample_0 = 20 sample_1 = 200 values = 80 seed = int(sys.argv[1]) tot_ = 150 # seed = 4 np.random.seed(seed) results = {} tf.reset_default_graph() sess = tf.InteractiveSession() #create sin_data data = json.loads(open('data_light_curves_2.json').read()) curve_0 = np.asarray(data['2']) curve_0_x = curve_0 curve_0_y = np.asarray([np.arange(values)] * sample_0) # curve_0_x, curve_0_y = #sample_curves(curve_0, values) #random sampling curves #create saw_data curve_1 = np.asarray(data['3']) curve_1_x = curve_1 curve_1_y = np.asarray([np.arange(values)] * sample_1) #curve_1_x, curve_1_y = #sample_curves(curve_1, values) print(curve_0_y[:sample_0].shape, curve_0_x[:sample_0].shape) #GAN first curve curve_0_y_test = np.asarray([np.arange(values)] * (tot_ - sample_0)) t_0_g_out, curves_0_g_out = routine(sess, curve_0_x[:sample_0], curve_0_y[:sample_0], curve_0_x[sample_0:tot_ - sample_0], curve_0_y_test, 10, values, sample_0, tot_ - sample_0, False) print('first') tf.reset_default_graph() sess = tf.InteractiveSession() #GAN second curve curve_1_y_test = np.asarray([np.arange(values)] * (tot_ - sample_1)) t_1_g_out, curves_1_g_out = routine(sess, curve_1_x[:sample_1], curve_1_y[:sample_1], curve_1_x[sample_1:tot_ - sample_1], curve_1_y_test, 10, values, sample_1, tot_ - sample_1, False) # CLASSIFIER ON REAL VALUES train_data_classifier_curve_0 = np.concatenate( (curve_0_x[:sample_0], curve_0_y[:sample_0]), axis=1) train_data_classifier_curve_1 = np.concatenate( (curve_1_x[:sample_1], curve_1_y[:sample_1]), axis=1) train_data_classifier = np.concatenate( (train_data_classifier_curve_0, train_data_classifier_curve_1), axis=0) classificatore_real = classifier(values) cl_real = classificatore_real.create_clas() num_rounds = 600 label_x = [[int(i)] for i in np.zeros(sample_0)] label_y = [[int(i)] for i in np.ones(sample_1)] labels = np.concatenate((label_x, label_y), axis=0) classificatore_real.train(train_data_classifier, labels, cl_real, num_rounds) #CLASSIFIER ON GAN VALUES train_x_0 = np.concatenate((curve_0_x[:sample_0], curves_0_g_out), axis=0) train_y_0 = np.concatenate((curve_0_y[:sample_0], t_0_g_out), axis=0) train_x_1 = curve_1_x[:sample_1] # train_x_1 = np.concatenate((curve_1_x[:sample_1], curves_1_g_out), axis=0) train_y_1 = curve_1_y[:sample_1] #train_y_1 = np.concatenate((curve_1_y[:sample_1], t_1_g_out), axis=0) train_data_classifier_curve_0 = np.concatenate( (train_x_0[:tot_], train_y_0[:tot_]), axis=1) train_data_classifier_curve_1 = np.concatenate( (train_x_1[:tot_], train_y_1[:tot_]), axis=1) train_data_classifier = np.concatenate( (train_data_classifier_curve_0, train_data_classifier_curve_1), axis=0) classificatore = classifier(values) cl = classificatore.create_clas() num_rounds = 600 label_x = [[int(i)] for i in np.zeros(tot_)] label_y = [[int(i)] for i in np.ones(tot_)] labels = np.concatenate((label_x, label_y), axis=0) classificatore.train(train_data_classifier, labels, cl, num_rounds) curve_0_y_ = np.asarray([np.arange(values)] * 100) test_data_classifier_curve_0 = np.concatenate( (curve_0_x[sample_0:sample_0 + 100], curve_0_y_), axis=1) test_data_classifier_curve_1 = np.concatenate( (curve_1_x[sample_1:sample_1 + 100], curve_0_y_), axis=1) test_data = np.concatenate( (test_data_classifier_curve_0, test_data_classifier_curve_1), axis=0) label_x_test = [[int(i)] for i in np.zeros(100)] label_y_test = [[int(i)] for i in np.ones(100)] labels_test = np.concatenate((label_x_test, label_y_test), axis=0) #evaluate classifier on real curve acc_class_real = classificatore.valuate(test_data, labels_test, cl)['accuracy'] acc_class_real_real = classificatore_real.valuate(test_data, labels_test, cl_real)['accuracy'] results['accuracy_real'] = float(acc_class_real) results['accuracy_real_real'] = float(acc_class_real_real) # results['loss_sin'] = [g_loss_sin, d_loss_sin] # results['loss_saw'] = [g_loss_saw, d_loss_saw] out_file_ac = 'results.s{}.sam{}.noise{}.json'.format( seed, sample_0, values) open(out_file_ac, 'w').write(json.dumps(results)) out_file_curves = 'curves.s{}.sam{}.noise{}.json'.format( seed, sample_0, values) open(out_file_curves, 'wb').write( pickle.dumps([[curves_0_g_out, t_0_g_out], [curves_1_g_out, t_1_g_out]])) return results
def main(): # sample_sizes = [int(sys.argv[1]), int(sys.argv[2])] sample_0 = 100 sample_1 = 200 values = 24 seed = int(sys.argv[1]) tot_ = 300 size = 8 num = int(values / size) # seed = 4 np.random.seed(seed) results = {} tf.reset_default_graph() sess = tf.InteractiveSession() #create sin_data data = json.loads(open('PowerDemand.json').read()) curve_0 = np.asarray(data['1']) curve_0_x = curve_0 curve_0_y = np.asarray([np.arange(values)] * sample_0) # curve_0_x, curve_0_y = #sample_curves(curve_0, values) #random sampling curves #create saw_data curve_1 = np.asarray(data['2']) curve_1_x = curve_1 curve_1_y = np.asarray([np.arange(values)] * sample_1) #curve_1_x, curve_1_y = #sample_curves(curve_1, values) slice_x_0, slice_y_0 = create_slices(curve_0_x, size) slice_x_1, slice_y_1 = create_slices(curve_1_x, size) train_x_0 = slice_x_0[:num * sample_0] train_y_0 = slice_y_0[:num * sample_0] train_x_1 = slice_x_1[:num * sample_1] train_y_1 = slice_y_1[:num * sample_1] #CLASSIFIER ON GAN VALUES train_x_0 = np.concatenate((curve_0_x[:sample_0], curves_0_g_out), axis=0) train_y_0 = np.concatenate((curve_0_y[:sample_0], t_0_g_out), axis=0) #train_x_1 = curve_1_x[:sample_1] print(np.asarray(curve_1_x).shape, np.asarray(curves_1_g_out).shape) train_x_1 = np.concatenate((curve_1_x[:sample_1], curves_1_g_out), axis=0) #train_y_1 = curve_1_y[:sample_1] train_y_1 = np.concatenate((curve_1_y[:sample_1], t_1_g_out), axis=0) train_data_classifier_curve_0 = np.concatenate( (train_x_0[:tot_], train_y_0[:tot_]), axis=1) train_data_classifier_curve_1 = np.concatenate( (train_x_1[:tot_], train_y_1[:tot_]), axis=1) train_data_classifier = np.concatenate( (train_data_classifier_curve_0, train_data_classifier_curve_1), axis=0) classificatore = classifier(values) cl = classificatore.create_clas() num_rounds = 900 label_x = [[int(i)] for i in np.zeros(train_data_classifier_curve_0.shape[0])] label_y = [[int(i)] for i in np.ones(train_data_classifier_curve_1.shape[0])] labels = np.concatenate((label_x, label_y), axis=0) print(labels.shape, train_data_classifier.shape) classificatore.train(train_data_classifier, labels, cl, num_rounds) curve_0_y_ = np.asarray([np.arange(values)] * 200) test_data_classifier_curve_0 = np.concatenate( (curve_0_x[sample_0:sample_0 + 200], curve_0_y_), axis=1) test_data_classifier_curve_1 = np.concatenate( (curve_1_x[sample_1:sample_1 + 200], curve_0_y_), axis=1) test_data = np.concatenate( (test_data_classifier_curve_0, test_data_classifier_curve_1), axis=0) label_x_test = [[int(i)] for i in np.zeros(200)] label_y_test = [[int(i)] for i in np.ones(200)] labels_test = np.concatenate((label_x_test, label_y_test), axis=0) #evaluate classifier on real curve acc_class_real = classificatore.valuate(test_data, labels_test, cl)['accuracy'] acc_class_real_real = classificatore_real.valuate(test_data, labels_test, cl_real)['accuracy'] results['accuracy_real'] = float(acc_class_real) results['accuracy_real_real'] = float(acc_class_real_real) # results['loss_sin'] = [g_loss_sin, d_loss_sin] # results['loss_saw'] = [g_loss_saw, d_loss_saw] out_file_ac = 'results.s{}.sam{}.noise{}.json'.format( seed, sample_0, values) open(out_file_ac, 'w').write(json.dumps(results)) out_file_curves = 'curves.s{}.sam{}.noise{}.json'.format( seed, sample_0, values) open(out_file_curves, 'wb').write( pickle.dumps([[curves_0_g_out, t_0_g_out], [curves_1_g_out, t_1_g_out]])) return results
def main(): # sample_sizes = [20] sample_sizes = [int(sys.argv[1])] sample = sample_sizes[0] # noise_sizes = [15] noise_sizes = [int(sys.argv[2])] values = noise_sizes[0] seed = int(sys.argv[3]) # seed = 4 np.random.seed(seed) results = {} len_t = sample tf.reset_default_graph() sess = tf.InteractiveSession() #create sin_data sin_data = sample_data( 'sin', n_samples=sample*4, no_drop=values, reuse=False, mul_range=[1, 2]) sin_data.create_points() #create saw_data saw_data = sample_data('saw', n_samples=sample*4, mul_range=[ 1, 2], no_drop=values, pred=1) saw_data.create_points() #GAN sin Gz_sin, d_g_sin, d_x_sin, g_loss_sin, d_loss_sin, y_out_sin, curves_g_sin = routine( sess, sin_data.y_values[:len_t], sin_data.x_values[:len_t],sin_data.y_values[len_t:], sin_data.x_values[len_t:], 10, values, len_t, len_t*3, False) tf.reset_default_graph() sess = tf.InteractiveSession() #GAN saw Gz_saw, d_g_saw, d_x_saw, g_loss_saw, d_loss_saw, y_out_saw, curves_g_saw = routine( sess, saw_data.y_values[:len_t], saw_data.x_values[:len_t],saw_data.y_values[len_t:], saw_data.x_values[len_t:], 10, values, len_t,len_t*3, False) #CLASSIFIER GAN #train_data_classifier train_data_classifier_sin = np.concatenate((curves_g_sin[:len_t*2], y_out_sin[:len_t*2]), axis=1) train_data_classifier_saw = np.concatenate((curves_g_saw[:len_t*2], y_out_saw[:len_t*2]), axis=1) train_data_classifier = np.concatenate((train_data_classifier_sin, train_data_classifier_saw), axis=0) #test_data_classifier test_data_classifier_sin = np.concatenate((curves_g_sin[len_t*2:], y_out_sin[len_t*2:]), axis=1) test_data_classifier_saw = np.concatenate((curves_g_saw[len_t*2:], y_out_saw[len_t*2:]), axis=1) test_data_classifier = np.concatenate((test_data_classifier_sin, test_data_classifier_saw), axis=0) #classifier train on gan classificatore = classifier(values) cl = classificatore.create_clas() num_rounds = 400 label_x = [[int(i)] for i in np.zeros(len_t*2)] label_y = [[int(i)] for i in np.ones(len_t*2)] labels = np.concatenate((label_x, label_y), axis=0) label_x_test = [[int(i)] for i in np.zeros(int(len_t))] label_y_test = [[int(i)] for i in np.ones(int(len_t))] labels_test = np.concatenate((label_x_test, label_y_test), axis=0) acc_class = 0 print(train_data_classifier.shape, labels.shape) while acc_class < 0.1: classificatore.train(train_data_classifier, labels, cl, num_rounds) print(test_data_classifier.shape, labels_test.shape) acc_class = classificatore.valuate(test_data_classifier, labels_test, cl)['accuracy'] print(acc_class) num_rounds += 100 results['accuracy_classifier'] = float(acc_class) #CLASSIFIER REAL #train_data_classifier sin_data = sample_data( 'sin', n_samples=len_t*3, no_drop=values, reuse=False, mul_range=[1, 2]) sin_data.create_points() #create saw_data saw_data = sample_data('saw', n_samples=len_t*3, mul_range=[ 1, 2], no_drop=values, pred=1) saw_data.create_points() train_data_classifier_sin = np.concatenate((sin_data.y_values[:len_t*2], sin_data.x_values[:len_t*2]), axis=1) train_data_classifier_saw = np.concatenate((saw_data.y_values[:len_t*2],saw_data.x_values[:len_t*2]), axis=1) train_data_classifier = np.concatenate((train_data_classifier_sin, train_data_classifier_saw), axis=0) #test_data_classifier test_data_classifier_sin = np.concatenate((sin_data.y_values[len_t*2:], sin_data.x_values[len_t*2:]), axis=1) test_data_classifier_saw = np.concatenate((saw_data.y_values[len_t*2:], saw_data.x_values[len_t*2:]), axis=1) test_data_classifier = np.concatenate((test_data_classifier_sin, test_data_classifier_saw), axis=0) #classifier train on gan classificatore_real = classifier(values) cl_real = classificatore_real.create_clas() num_rounds = 400 label_x = [[int(i)] for i in np.zeros(len_t*2)] label_y = [[int(i)] for i in np.ones(len_t*2)] labels = np.concatenate((label_x, label_y), axis=0) label_x_test = [[int(i)] for i in np.zeros(int(len_t))] label_y_test = [[int(i)] for i in np.ones(int(len_t))] labels_test = np.concatenate((label_x_test, label_y_test), axis=0) acc_class = 0 print(train_data_classifier.shape, labels.shape) while acc_class < 0.1: classificatore_real.train(train_data_classifier, labels, cl_real, num_rounds) print(test_data_classifier.shape, labels_test.shape) acc_class = classificatore_real.valuate(test_data_classifier, labels_test, cl_real)['accuracy'] print(acc_class) num_rounds += 100 results['accuracy_classifier_real'] = float(acc_class) #create new curves sin #create sin_data sin_data = sample_data( 'sin', n_samples=sample, no_drop=values, reuse=False, mul_range=[1, 2]) sin_data.create_points() #create saw_data saw_data = sample_data('saw', n_samples=sample, mul_range=[ 1, 2], no_drop=values, pred=1) saw_data.create_points() #train_data_sin_ test_data_sin = np.concatenate((sin_data.y_values, sin_data.x_values), axis=1) #train_data_saw_ test_data_saw = np.concatenate((saw_data.y_values, saw_data.x_values), axis=1) test_data = np.concatenate((test_data_sin, test_data_saw), axis=0) #evaluate classifier on real curve acc_class_real = classificatore.valuate(test_data, labels_test, cl)['accuracy'] acc_class_real_real = classificatore_real.valuate(test_data, labels_test, cl_real)['accuracy'] results['accuracy_real'] = float(acc_class_real) results['accuracy_real_real'] = float(acc_class_real_real) results['loss_sin'] = [g_loss_sin, d_loss_sin] results['loss_saw'] = [g_loss_saw, d_loss_saw] out_file_ac = 'results.s{}.sam{}.noise{}.json'.format(seed, sample_sizes, noise_sizes) open(out_file_ac, 'w').write(json.dumps(results)) out_file_curves = 'curves.s{}.sam{}.noise{}.json'.format(seed, sample_sizes[0], noise_sizes[0]) open(out_file_curves, 'wb').write(pickle.dumps([[curves_g_sin,y_out_sin], [curves_g_saw,y_out_saw]])) return results
def main(): # sample_sizes = [int(sys.argv[1]), int(sys.argv[2])] sample_0 = 27 sample_1 = 93 values = 96 size = 32 num = int(values / size) seed = 4 np.random.seed(seed) results = {} tf.reset_default_graph() sess = tf.InteractiveSession() #create sin_data data = json.loads(open('ecg_data.json').read()) curve_0 = np.asarray(data['-1']) curve_0_x = curve_0 curve_0_y = np.asarray([np.arange(values)] * sample_0) # curve_0_x, curve_0_y = #sample_curves(curve_0, values) #random sampling curves #create saw_data curve_1 = np.asarray(data['1']) curve_1_x = curve_1 curve_1_y = np.asarray([np.arange(values)] * sample_1) #curve_1_x, curve_1_y = #sample_curves(curve_1, values) slice_x_0, slice_y_0 = create_slices(curve_0_x, size) slice_x_1, slice_y_1 = create_slices(curve_1_x, size) print('sl', slice_x_0.shape, slice_x_1.shape) train_x_0 = slice_x_0[:num * sample_0] train_y_0 = slice_y_0[:num * sample_0] train_x_1 = slice_x_1[:num * sample_1] train_y_1 = slice_y_1[:num * sample_1] print('k', train_x_0.shape, train_x_1.shape) #CLASSIFIER ON GAN VALUES #train_x_1 = curve_1_x[:sample_1] train_data_classifier_curve_0 = np.concatenate((train_x_0, train_y_0), axis=1) train_data_classifier_curve_1 = np.concatenate((train_x_1, train_y_1), axis=1) train_data_classifier = np.concatenate( (train_data_classifier_curve_0, train_data_classifier_curve_1), axis=0) classificatore = classifier(size) cl = classificatore.create_clas() num_rounds = 900 label_x = [[int(i)] for i in np.zeros(train_data_classifier_curve_0.shape[0])] label_y = [[int(i)] for i in np.ones(train_data_classifier_curve_1.shape[0])] labels = np.concatenate((label_x, label_y), axis=0) classificatore.train(train_data_classifier, labels, cl, num_rounds) curve_0_y_ = np.asarray( [np.arange(size)] * (slice_x_0.shape[0] - train_data_classifier_curve_0.shape[0])) curve_1_y_ = np.asarray( [np.arange(size)] * (slice_x_1.shape[0] - train_data_classifier_curve_1.shape[0])) print('u', slice_x_1[sample_1 * num:sample_1 * num + 40 * num].shape, curve_1_y_.shape) # print(curve_1_y_.shape, slice_x_1[sample_1*num:sample_0*num+40*num].shape) test_data_classifier_curve_0 = np.concatenate( (slice_x_0[sample_0 * num:sample_0 * num + 40 * num], curve_0_y_), axis=1) test_data_classifier_curve_1 = np.concatenate( (slice_x_1[sample_1 * num:sample_1 * num + 40 * num], curve_1_y_), axis=1) test_data = np.concatenate( (test_data_classifier_curve_0, test_data_classifier_curve_1), axis=0) label_x_test = [[int(i)] for i in np.zeros(40 * num)] label_y_test = [[int(i)] for i in np.ones(40 * num)] labels_test = np.concatenate((label_x_test, label_y_test), axis=0) #evaluate classifier on real curve acc_class_real = list(classificatore.epredict(test_data, labels_test, cl)) precision = 0 count = 0 for k in range(len(acc_class_real)): if int(acc_class_real[k]['classes']) == 1: count += 1 if (k + 1) % 3 == 0: if count >= 2 and labels[k] == 1: precision += 1 if count < 2 and labels[k] == 0: precision += 1 count = 0 print(precision) # acc_class_real = classificatore.valuate(test_data, labels_test, cl)['accuracy'] # acc_class_real_real = classificatore_real.valuate(test_data, labels_test, cl_real)['accuracy'] results['accuracy_real'] = precision / (len(acc_class_real) / 3) #results['accuracy_real_real'] = float(acc_class_real_real) # results['loss_sin'] = [g_loss_sin, d_loss_sin] # results['loss_saw'] = [g_loss_saw, d_loss_saw] out_file_ac = 'results.s{}.sam{}.noise{}.json'.format( seed, sample_0, values) open(out_file_ac, 'w').write(json.dumps(results)) # out_file_curves = 'curves.s{}.sam{}.noise{}.json'.format(seed, sample_0, values) # open(out_file_curves, 'wb').write(pickle.dumps([[curves_0_g_out,t_0_g_out], [curves_1_g_out,t_1_g_out]])) return results