def input_data(): ''' # inputs the raw data. The user is prompted to enter the name of the data # files (in csv format). For example: SPARCS2014.csv, SPARCS2015.csv, # SCARCS2016.csv # inputs the data structure. The user is prompted to enter the desired data # structure. Available options are: list(csv), array (numpy), dataframe(pandas) # @param: none ''' state = False while state == False: print("What are the names of your data files?") input_names = input(">: ") print("What is the data structure you would like to work with?") global data_structure data_structure = input(">: ") state, data = ip.read_data(input_names, data_structure, input_path) return data
def get_SVM_compare(): print("training SVM comparison " + dataset) group = 4 accuracy1 = np.zeros(4) times1 = np.zeros(4) train_x, train_y = input.read_data('optdigits.tra') test_x, test_y = input.read_data('optdigits.tes') data = Model_Validation(train_x, train_y, DecisionTreeClassifier(), len(train_x[0])) data.set_test_data(test_x, test_y) accuracy1[0], times1[0] = data.get_scores(SVC()) print('rbf') accuracy1[1], times1[1] = data.get_scores(SVC(kernel='linear')) print('linear') accuracy1[2], times1[2] = data.get_scores(SVC(kernel='poly')) print('poly') accuracy1[3], times1[3] = data.get_scores(SVC(kernel='sigmoid')) print('sigmoid') accuracy1 = accuracy1 * 100 name = ('rbf', 'linear', 'poly', 'sigmoid') plt.figure() title = "dataset 1: \nSVM kernels comparison" plt.title(title) ax1 = plt.subplot() ax2 = ax1.twinx() opacity = 0.6 ax1.set_ylabel('Accuracy', color = (0, 0, 1, opacity)) ax2.set_ylabel('Running time(s)', color = (1, 0, 0, opacity)) ax1.plot(color = (0, 0, 1, opacity)) ax2.plot(color = (1, 0, 0, opacity)) index = np.arange(4)+1 bar_width = 0.25 bar11 = ax1.bar(index, accuracy1, bar_width, alpha = opacity, color = 'b') bar21 = ax2.bar(index + bar_width, times1, bar_width, alpha = opacity, color = 'r') # autolabel(ax1, bar1, 'left') # autolabel(ax2, bar2, 'right') for rect in bar11: height = rect.get_height() ax1.text(rect.get_x() + rect.get_width()/2., height, '%.1f%%' % height, ha='center', va='bottom') for rect in bar21: height = rect.get_height() ax2.text(rect.get_x() + rect.get_width()/2., height, '%.2fs' % height, ha='center', va='bottom') plt.xticks(index + bar_width/2, name) plt.legend((bar11, bar21), ('Accuracy', 'Running time')) plt.tight_layout() plt.savefig("SVM kernels comparison.png") return plt
plt.xlim([0, 1.0]) plt.ylim([0, 1.0]) plt.plot([0, 1], [0, 1], 'k--', lw=lw) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('dataset 2:\nROC curves') plt.legend(loc="lower right") plt.savefig('datase 2 ROC curves.png') if __name__ == '__main__': # dataset 1 start train_x, train_y = input.read_data('optdigits.tra') test_x, test_y = input.read_data('optdigits.tes') # print(len(train_x)) data = Model_Validation(train_x, train_y, DecisionTreeClassifier(), len(train_x[0])) data.set_test_data(test_x, test_y) dataset = "Dataset 1" print(dataset + " start") get_overview(data, dataset) get_SVM_learning_curve(data, dataset, 0.0025) data.set_estimator(AdaBoostClassifier())
from input import read_data from segment_detector import detect_segments from arc_detector import detect_arcs from display import plot import tkinter as tk from tkinter import filedialog root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename() data = read_data(file_path) segments, filtered_data = detect_segments(data) # plot(filtered_data, segments) arcs, filtered_data = detect_arcs(filtered_data) plot(data, segments, arcs)
def main(): with tf.device('/gpu'): x_train_, y_train_ = read_data(path=FLAGS.data_dir, file=train_file, is_train=True) x_train_batch, y_train_batch = tf.train.shuffle_batch( [x_train_, y_train_], batch_size=batch_size, capacity=2000, min_after_dequeue=1000, num_threads=num_threads) x_val_, y_val_ = read_data(path=FLAGS.data_dir, file=val_file, is_train=False) x_val_batch, y_val_batch = tf.train.batch([x_val_, y_val_], batch_size=batch_size, capacity=10000, num_threads=num_threads) x_test_, y_test_ = read_data(path=FLAGS.data_dir, file=test_file, is_train=False) x_test_batch, y_test_batch = tf.train.batch([x_test_, y_test_], batch_size=batch_size, capacity=10000, num_threads=num_threads) network_teacher, logits_teacher = wresnet_T(x_train_batch, num_classes=num_classes, is_train=False) with tf.variable_scope("student") as scope: network_student, logits_train_stu0 = cifarnet( x_train_batch, num_classes=num_classes, is_train=True) _, logits_val_stu = cifarnet(x_val_batch, num_classes=num_classes, is_train=False) _, logits_test_stu = cifarnet(x_test_batch, num_classes=num_classes, is_train=False) softmax_teacher = tf.nn.softmax(logits_teacher * tea_T) logits_train_stu = logits_train_stu0 * stu_T loss0_ = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2( labels=tf.stop_gradient(softmax_teacher), logits=logits_train_stu)) loss1_ = tl.cost.cross_entropy(logits_train_stu, y_train_batch, name='loss_') loss_ = loss1_ / ratio + loss0_ acc1_ = tf.reduce_mean( tf.cast(tf.nn.in_top_k(logits_train_stu, y_train_batch, 1), tf.float32)) val_loss_ = tl.cost.cross_entropy(logits_val_stu, y_val_batch, name='val_loss_') val_acc1_ = tf.reduce_mean( tf.cast(tf.nn.in_top_k(logits_val_stu, y_val_batch, 1), tf.float32)) test_loss_ = tl.cost.cross_entropy(logits_test_stu, y_test_batch, name='test_loss_') test_acc1_ = tf.reduce_mean( tf.cast(tf.nn.in_top_k(logits_test_stu, y_test_batch, 1), tf.float32)) v_list_student = tl.layers.get_variables_with_name('student', train_only=False) else_variable_list = [] for v in tl.layers.get_variables_with_name('cifarnet/cnn'): else_variable_list.append(v) for v in tl.layers.get_variables_with_name('bnn/b'): else_variable_list.append(v) len_else = len(else_variable_list) print('else_variable_list') for var in else_variable_list: print(var.op.name) scale_variable_list = [] for v in tl.layers.get_variables_with_name('bnn/W'): scale_variable_list.append(v) len_scale = len(scale_variable_list) print('scale_variable_list') for var in scale_variable_list: print(var.op.name) # computing for scaling rate of learning rate scaling fan_in = [] fan_out = [] W_LR_scale = [] for i in range(0, len_scale): variable_shape = scale_variable_list[i].get_shape() fan_in1 = tf.cast(variable_shape[-2], dtype=tf.double) fan_out1 = tf.cast(variable_shape[-1], dtype=tf.double) for dim in variable_shape[:-2]: fan_in1 *= tf.cast(dim, dtype=tf.double) fan_out1 *= tf.cast(dim, dtype=tf.double) fan_in.append(fan_in1) fan_out.append(fan_out1) W_LR_scale.append(1. / tf.sqrt(1.5 / (fan_in1 + fan_out1))) opt_else = tf.train.AdamOptimizer(decayed_LR, beta1=0.99) opt = [] for i in range(0, len_scale): opt.append( tf.train.AdamOptimizer(decayed_LR * W_LR_scale[i], beta1=0.9)) lr_assign_op = tf.assign( decayed_LR, decayed_LR * tf.cast(LR_decay, dtype=tf.double)) grads = tf.gradients(loss_, else_variable_list + scale_variable_list) tf.add_to_collection( 'train_op', opt_else.apply_gradients(zip(grads[:len_else], else_variable_list))) for i in range(0, len_scale): tf.add_to_collection( 'train_op', opt[i].apply_gradients( zip([grads[i + len_else]], [scale_variable_list[i]]))) train_op = tf.get_collection('train_op') # weight clipping print("clip begin") for v in scale_variable_list: print(v.op.name) tf.add_to_collection('clip', tf.assign(v, tf.clip_by_value(v, -1.0, 1.0))) assign_v_op = tf.get_collection('clip') print("clip end") sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) tl.layers.initialize_global_variables(sess) summary_writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) print("---------------------W_LR_scale------------") for i in range(0, len_scale): print(sess.run(W_LR_scale[i])) print("---------------------load_param------------") tl.files.load_and_assign_npz(sess=sess, name=teacher_path, network=network_teacher) network_student.print_params(False) network_student.print_layers() saver = tf.train.Saver(max_to_keep=1, var_list=v_list_student) saver_best = tf.train.Saver(max_to_keep=1, var_list=v_list_student) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) def train_epoch(): train_acc1, train_loss = 0, 0 batches = int(np.ceil(train_num / batch_size)) for s in range(batches): acc1, loss, LR, _ = sess.run( [acc1_, loss_, decayed_LR, train_op]) sess.run(assign_v_op) #weight clip train_acc1 += acc1 train_loss += loss if (s != 0) & (s % printp == 0): print("train-----------", "i", s, "batches", batches) print(acc1) print("loss", loss) print("LR:", LR) sys.stdout.flush() train_acc1 = train_acc1 / batches * 100 train_loss = train_loss / batches return train_acc1, train_loss, LR def val_epoch(): val_acc1, val_loss, teacher_acc1 = 0, 0, 0 batches = int(np.ceil(val_num / batch_size)) for s in range(batches): acc1, loss = sess.run([val_acc1_, val_loss_]) val_acc1 += acc1 val_loss += loss val_acc1 = val_acc1 / batches * 100 val_loss = val_loss / batches return val_acc1, val_loss def test_epoch(): test_acc1, test_loss = 0.0, 0.0 batches = int(np.ceil(test_num / batch_size)) for s in range(batches): acc1, loss = sess.run([test_acc1_, test_loss_]) test_acc1 += acc1 test_loss += loss test_acc1 = test_acc1 / batches * 100 test_loss = test_loss / batches return test_acc1, test_loss best_val_acc = 0 best_epoch = 0 flag_epoch = 0 test_loss = 0 test_acc1 = 0 for epoch in range(train_epochs): start_time = time.time() acc1, train_loss, LR = train_epoch() val_acc1, val_loss = val_epoch() summary = tf.Summary(value=[ tf.Summary.Value(tag="learning_rate", simple_value=LR), tf.Summary.Value(tag="train_loss", simple_value=train_loss), tf.Summary.Value(tag="train_acc1", simple_value=acc1), tf.Summary.Value(tag="val_loss", simple_value=val_loss), tf.Summary.Value(tag="val_acc1", simple_value=val_acc1), ]) summary_writer.add_summary(summary, epoch) summary_writer.flush() saver.save(sess, checkpoint_path, global_step=epoch + 1) if val_acc1 >= best_val_acc: best_val_acc = val_acc1 best_epoch = epoch + 1 test_acc1, test_loss = test_epoch() if val_acc1 >= 80.0: saver_best.save(sess, max_checkpoint_path, global_step=epoch + 1) epoch_duration = time.time() - start_time sess.run(lr_assign_op) if (epoch + 1) % print_freq == 0: print("Epoch " + str(epoch + 1) + " of " + str(train_epochs) + " took " + str(epoch_duration) + "s") print(" LR: " + str(LR)) print(" training loss: " + str(train_loss)) print(" train accuracy rate1: " + str(acc1) + "%") print(" validation loss: " + str(val_loss)) print(" validation accuracy rate1: " + str(val_acc1) + "%") print(" best epoch: " + str(best_epoch)) print(" best validation accuracy rate: " + str(best_val_acc) + "%") print(" test loss: " + str(test_loss)) print(" test accuracy rate1: " + str(test_acc1) + "%") sys.stdout.flush() coord.request_stop() coord.join(threads) summary_writer.close() sess.close()
from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.utils import np_utils import input import sys batch_size = 32 nb_classes = 4 nb_epoch = 100 data_augmentation = True img_rows, img_cols = 112, 112 img_channels = 4 (X_train, Y_train) = input.read_data('./train.txt') (X_test, y_test) = input.read_data('./test.txt') print('X_train shape:', X_train.shape, X_train[0][0][0][0]) print('Y_train shape:', Y_train.shape, Y_train[0][0]) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') Y_train = np_utils.to_categorical(Y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=X_train.shape[1:])) model.add(Activation('relu')) model.add(Conv2D(32, (3, 3)))
import input as inp import output as out import transform as trans if __name__ == '__main__': # Print the welcome message. print("Let's play the FFT game!\n") # Print the rules. print("If you think you've identified the image, enter its number.") print("If you don't know the image, enter a '?' to get a better image.") print("If you are done with the game, enter a 'q' to leave.\n") # Read the data and make a list of keys. data = inp.read_data() keys = list(data.keys()) # Let the user know the image options. message_parts = ['The possible images for this game are:'] for i in range(len(keys)): message_parts.append(' [{0:d}] {1}'.format(i, data[keys[i]][1])) message = '\n'.join(message_parts) print(message) # Play rounds until the user quits the program. while True: key = random.choice(keys) selection = data[key] res = 0.01
def __collect_data__(self): self.filename = input.download_data(self.url, self.filename) self.data = input.read_data(self.filename) self.data, self.count, self.dictionary, self.reverse_dictionary = input.build_dataset(self.data, self.vocabulary_size)