def train_model(num_steps=num_training_steps): '''Train the model with minibatches in a tensorflow session''' with tf.Session(graph=self.graph) as session: tf.initialize_all_variables().run() print('Initializing variables...') for step in range(num_steps): offset = (step * batch_size + startloc) % ( self.train_Y.shape[0] - batch_size) batch_data = self.train_X[offset:(offset + batch_size), :] batch_labels = self.train_Y[offset:(offset + batch_size)] # Data to feed into the placeholder variables in the tensorflow graph feed_dict = { tf_train_batch: batch_data, tf_train_labels: batch_labels, dropout_keep_prob: dropout_prob } _, l, predictions = session.run( [optimizer, loss, batch_prediction], feed_dict=feed_dict) if (step % 200 == 0): train_preds = session.run(train_prediction, feed_dict={ tf_train_dataset: self.train_X, dropout_keep_prob: 1.0 }) val_preds = session.run( valid_prediction, feed_dict={dropout_keep_prob: 1.0}) test_preds = session.run( test_prediction, feed_dict={dropout_keep_prob: 1.0}) print('') print('Batch loss at step %d: %f' % (step, l)) print('Batch training accuracy: %.1f%%' % accuracy(predictions, batch_labels)) print('Validation accuracy: %.1f%%' % accuracy(val_preds, self.val_Y)) print('Full train accuracy: %.1f%%' % accuracy(train_preds, self.train_Y)) #if accuracy(val_preds, self.val_Y)>70: # break print('Test accuracy: %.1f%%' % accuracy(test_preds, self.test_Y)) # for theta in threshold: n = len(self.val_Y) matrix = np.zeros((n, 5)) matrix[:, 0] = np.array(self.val_Y).reshape(n) * 2 - 1 matrix[:, 1] = np.array(self.val_noise).reshape(n) counter = 0 for i in val_preds: if i > (0.5 + theta / 2): matrix[counter, 2:5] = [1, 0, 0] elif i < (0.5 - theta / 2): matrix[counter, 2:5] = [0, 0, 1] else: matrix[counter, 2:5] = [0, 1, 0] counter += 1 Sensitivity, Specificity, MAcc = DataSet.heart_sound_scoring( matrix) # res[0, num], res[1, num], res[2, num] = DataSet.heart_sound_scoring(matrix) # print('Final Results:') print('Sensitivity: ', Sensitivity) print('Specificity: ', Specificity) print('MAcc: ', MAcc) counter = 0 n = len(self.test_Y) matrix = np.zeros((n, 5)) matrix[:, 0] = np.array(self.test_Y).reshape(n) * 2 - 1 matrix[:, 1] = np.array(self.test_noise).reshape(n) for i in test_preds: if i > (0.5 + theta / 2): matrix[counter, 2:5] = [1, 0, 0] elif i < (0.5 - theta / 2): matrix[counter, 2:5] = [0, 0, 1] else: matrix[counter, 2:5] = [0, 1, 0] counter += 1 TestSensitivity, TestSpecificity, TestMAcc = DataSet.heart_sound_scoring( matrix) # res[0, num], res[1, num], res[2, num] = DataSet.heart_sound_scoring(matrix) print('Testing Results') print('Sensitivity: ', TestSensitivity) print('Specificity: ', TestSpecificity) print('MAcc: ', TestMAcc) print('Dropout Probability: %1f' % dropout_prob) print('Weight Penalty: %1f' % weight_penalty) print('Number of Steps: %d' % num_training_steps) if Writetofile: fd = open(OUTPUT_FILE, 'a') fd.write('\n %1f, %d, %d, %d, %d,' % (theta, layer1_depth, layer3_depth, num_training_steps, batch_size)) fd.write('%1f, %1f,' % (dropout_prob, weight_penalty)) fd.write('%1f%%, %1f%%, %1f%%,' % (accuracy(train_preds, self.train_Y), accuracy(val_preds, self.val_Y), accuracy(test_preds, self.test_Y))) fd.write( '%1f%%, %1f%%, %1f%%,' % (Sensitivity * 100, Specificity * 100, MAcc * 100)) fd.write('%1f%%, %1f%%, %1f%%,' % (TestSensitivity * 100, TestSpecificity * 100, TestMAcc * 100)) fd.write('%r,' % balanced) # fd.write('%r, %r, %r, %1f, %1f, %d, %.1f%%, %.1f%%' % (pooling, Augment, invariance, dropout_prob, weight_penalty, num_training_steps, accuracy(val_preds, self.val_Y), accuracy(train_preds, self.train_Y))) fd.close()
matrix = np.zeros((predicted_noise_y.size, 5)) matrix[:, 0] = test_sound_y matrix[:, 1] = test_noise_y counter = 0 for i in predicted_noise_y: if i == 1: if predicted_sound_y[counter] == 1: matrix[counter, 2:5] = [1, 0, 0] else: matrix[counter, 2:5] = [0, 0, 1] else: matrix[counter, 2:5] = [0, 1, 0] counter = counter + 1 Sensitivity, Specificty, MAcc = DataSet.heart_sound_scoring(matrix) print('Final Results:') print('Sensitivity: ', Sensitivity) print('Specificity: ', Specificty) print('MAcc: ', MAcc) print('________________________________') print('Auto on Manual trained Abnormal/Normal') conf = confusion_matrix(test_sound_y, logreg_sound.predict(test_x)) print(conf) print('Sensitivity: ', conf[0, 0] / (conf[0, 0] + conf[1, 0])) print('Specificity: ', conf[1, 1] / (conf[1, 1] + conf[0, 1])) print('________________________________')
temp = max < theta temp = temp.astype(int) counter = 0 for i in temp: if i == 0: if label[counter] == 1: matrix[counter, 2:5] = [1, 0, 0] else: matrix[counter, 2:5] = [0, 0, 1] else: matrix[counter, 2:5] = [0, 1, 0] counter += 1 res[0, num], res[1, num], res[2, num] = DataSet.heart_sound_scoring(matrix) num += 1 # Now on test data theta = threshold[np.argmax(res[2, :])] probs = logreg.predict_proba(test_x) label = logreg.predict(test_x) max = np.amax(probs, axis=1) matrix = np.zeros((label.size, 5)) matrix[:, 0] = test_y_sound matrix[:, 1] = test_y_noise temp = max < theta
def train_model(num_steps=num_training_steps): '''Train the model with minibatches in a tensorflow session''' with tf.Session(graph=self.graph) as session: tf.initialize_all_variables().run() print('Initializing variables...') for step in range(num_steps): # offset = (step * batch_size+startloc) % (self.train_Y.shape[0] - batch_size) # batch_data = self.train_X[offset:(offset + batch_size), :] # batch_labels = self.train_Y[offset:(offset + batch_size)] # # # Data to feed into the placeholder variables in the tensorflow graph # feed_dict = {tf_train_batch: batch_data, tf_train_labels: batch_labels} rand_index = np.random.choice(len(self.train_Y), size=batch_size) batch_data = self.train_X[rand_index, :] batch_labels = self.train_Y[rand_index] feed_dict = { tf_train_batch: batch_data, tf_train_labels: batch_labels } _, l, predictions = session.run( [train_step, loss, batch_prediction], feed_dict=feed_dict) if (step % 200 == 0): train_preds = session.run(train_prediction, feed_dict={ tf_train_dataset: self.train_X, tf_train_batch: batch_data, tf_train_labels: batch_labels }) val_preds = session.run(valid_prediction, feed_dict=feed_dict) test_preds = session.run(test_prediction, feed_dict=feed_dict) print('') print('Batch loss at step %d: %f' % (step, l)) print('Batch training accuracy: %.1f%%' % accuracy(predictions, batch_labels)) print('Validation accuracy: %.1f%%' % accuracy(val_preds, self.val_Y)) print('Full train accuracy: %.1f%%' % accuracy(train_preds, self.train_Y)) #if accuracy(val_preds, self.val_Y)>70: # break print('Test accuracy: %.1f%%' % accuracy(test_preds, self.test_Y)) n = len(self.val_Y) matrix = np.zeros((n, 5)) matrix[:, 0] = np.array(self.val_Y).reshape(n) matrix[:, 1] = np.array(self.val_noise).reshape(n) counter = 0 for i in val_preds: if np.sign(i) > theta: matrix[counter, 2:5] = [1, 0, 0] elif np.sign(i) < -theta: matrix[counter, 2:5] = [0, 0, 1] else: matrix[counter, 2:5] = [0, 1, 0] counter += 1 Sensitivity, Specificity, MAcc = DataSet.heart_sound_scoring( matrix) # res[0, num], res[1, num], res[2, num] = DataSet.heart_sound_scoring(matrix) # print('Final Results:') print('Sensitivity: ', Sensitivity) print('Specificity: ', Specificity) print('MAcc: ', MAcc) print('') print('Parameters') print('Number of Steps: %d' % num_training_steps) if Writetofile: fd = open(OUTPUT_FILE, 'a') fd.write('\n %1f, %d, %d, %1f, %1f, %r,' % (theta, num_training_steps, batch_size, C, gamma, UsePCA)) fd.write('%1f%%, %1f%%, %1f%%,' % (accuracy(train_preds, self.train_Y), accuracy(val_preds, self.val_Y), accuracy(test_preds, self.test_Y))) fd.write( '%1f%%, %1f%%, %1f%%,' % (Sensitivity * 100, Specificity * 100, MAcc * 100)) #fd.write('%r, %r, %r, %1f, %1f, %d, %.1f%%, %.1f%%' % (pooling, Augment, invariance, dropout_prob, weight_penalty, num_training_steps, accuracy(val_preds, self.val_Y), accuracy(train_preds, self.train_Y))) fd.close()