Exemple #1
0
def main(dataset='Iris'):
    if dataset == 'Iris':
        # Iris
        X, y = loadData('Iris')
        model, res = tuneParameter(X, y, ['accuracy'])
        print(res)
        return model, res
    else:
        # Drive
        X, y = loadData('Drive')
        model, res = tuneParameter(X, y, ['f1_micro', 'accuracy', 'f1_macro'])
        print(res)
Exemple #2
0
def main(dataset='Iris'):
    parameters = [{
        'gamma': [0, 0.2, 0.4, 0.6, 0.8],
    }, {
        'subsample': [0, 0.5, 0.8],
        'reg_alpha': [0.01, 0.1, 1]
    }, {
        'max_depth': [3, 5, 7, 9],
        'min_child_weight': [1, 3, 5],
    }]
    if dataset == 'Iris':
        # Iris
        X, y = loadData('Iris')
        model, res = tuneParameter(X, y, ['precision'], parameters[0])
        para = format(model.get_params())
        para.update(parameters[1])
        print('a')
        model, res2 = tuneParameter(X, y, ['precision'], para)
        para = format(model.get_params())
        print('b')
        para.update(parameters[2])
        model, res3 = tuneParameter(X, y, ['precision'], para)
        return model, [res, res2, res3]

    else:
        # Drive
        if dataset == 'Drive':
            X, y = loadData('Drive')
        else:
            X, y = loadData('Adult')
        model, res = tuneParameter(X, y, ['precision'], parameters[0])
        para = format(model.get_params())
        para.update(parameters[1])
        model, res2 = tuneParameter(X, y, ['precision'], para)
        para = format(model.get_params())
        para.update(parameters[2])
        model, res3 = tuneParameter(X, y, ['precision'], para)
        return model, [res, res2, res3]
success_emission_fp = open("success_emission.csv", "w")

unsuccess_start_fp = open("unsuccess_start.csv", "w")
unsuccess_trans_fp = open("unsuccess_transition.csv", "w")
unsuccess_emission_fp = open("unsuccess_emission.csv", "w")

# get train and test data
training_filename = "Data/successful_unsuccessful.txt"

# determine codebook size and number of hidden states
all_codebook, success_codebook, unsuccess_codebook = utility.loadCodeBookFromTrainingFile(
    training_filename)

# fit successful model
n_observations = len(success_codebook)
sequences, seq_labels, seq_lengths = utility.loadData(training_filename,
                                                      success_codebook, 1)
success_model = utility.getHMMModel(succ_hidden_states, n_observations,
                                    sequences, seq_lengths)
succ_logproba, succ_state_seq = success_model.decode(sequences, seq_lengths)

# compute state transitions
state_dict = {}
for i in range(len(succ_state_seq) - 1):
    state_from_to = str(succ_state_seq[i]) + "-" + str(succ_state_seq[i + 1])
    if state_from_to in state_dict:
        state_dict[state_from_to] = state_dict[state_from_to] + 1
    else:
        state_dict[state_from_to] = 1

print("Total # of transaction in successfull: ", str(len(succ_state_seq) - 1))
print(state_dict)
Exemple #4
0
 def test_loadData_returnIsStringWhenLoadingString(self):
     ret = utility.loadData('tstFile.json')
     self.assertIsInstance(ret, str)
Exemple #5
0
 def test_loadData_returnIsNoneForInvalidPath(self):
     ret = utility.loadData('Non-existent-file')
     self.assertIsNone(ret)
Exemple #6
0
def Main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "data_directory",
        help=
        "The parent directory to containing subfolders of train and test data.",
        type=str,
        default='flowers')
    parser.add_argument("--save_dir",
                        help="The directory of checkpoint file to be saved.",
                        type=str,
                        default=None)
    parser.add_argument("--gpu",
                        help="Use GPU instead of CPU.",
                        action="store_true")
    parser.add_argument("--learning_rate",
                        help="Set learning rate for training.",
                        type=float,
                        default=0.001)
    parser.add_argument("--hidden_units", nargs="+", type = int, default = [1000], \
        help="Set a list of hidden units: e.g. say two hidden layers of 500 and 200 units, the input format: 500 [space] 200")
    parser.add_argument("-e",
                        "--epochs",
                        help="Set the number of training iterations.",
                        type=int,
                        default=10)
    parser.add_argument(
        "--skip_accuracy",
        help=
        "Skip the validation on training and testing set in each iteration, and reduce the training time.",
        action="store_true")
    parser.add_argument(
        "--arch",
        help="Pre-trained Model Options: 0: Densenet121, 1: VGG16, 2: AlexNet ",
        type=int,
        default=1)

    args = parser.parse_args()

    if args.gpu:
        device = 'cuda'
        print('Compute using GPU')
    else:
        device = 'cpu'
        print('Compute using CPU')

    data_dir = args.data_directory  #'flowers'
    train_dir = data_dir + '/train'
    valid_dir = data_dir + '/valid'
    test_dir = data_dir + '/test'

    file_path = args.save_dir
    print('Training data directory: ', train_dir)
    print('Testing data directory: ', test_dir)
    print('Output checkpoint file directory: ', file_path)

    train_image_dataset, trainloader = loadData(train_dir,
                                                train=True,
                                                batch_size=128,
                                                shuffle=True)
    test_image_dataset, testloader = loadData(test_dir,
                                              train=False,
                                              batch_size=128,
                                              shuffle=True)
    train_size = len(trainloader.dataset.imgs)
    print('Total number of samples in the train set: ', train_size)

    hidden_layer = args.hidden_units
    model_name = args.arch
    print(
        'Building the model with hidden layer: {}, using pre-trained model: {}'
        .format(hidden_layer, model_options[model_name]))
    model = build_model(model_name=model_name, hidden_layer=hidden_layer)

    epochs = args.epochs
    print('Number of iteration: ', epochs)
    learn_rate = args.learning_rate
    print('Using the learning rate: ', learn_rate)
    if args.skip_accuracy:
        print_accuracy = False
        print('Skip calculating the accuracy during the training.')
    else:
        print_accuracy = True
        print(
            'Will calculate the accuracy during the training. Expected longer training time.'
        )
    criterion = nn.NLLLoss()
    optimizer = optim.Adam(model.classifier.parameters(), lr=learn_rate)

    print('Start training...')
    train(model,
          trainloader,
          testloader,
          criterion,
          optimizer,
          epochs=epochs,
          print_every=10,
          print_accuracy=print_accuracy,
          device=device)
    accuracy_score(model, trainloader, device=device, print_score=True)

    save_checkpoint(model,
                    optimizer,
                    train_image_dataset,
                    epochs,
                    file_path=file_path,
                    file_name='checkpoint.pth',
                    print_model=False)
Exemple #7
0
    logger.setLevel(
        logging.DEBUG)  # TODO: Set apropriate logging level. (INFO)
    handler = handlers.RotatingFileHandler(
        filename=Constants.LOG_FILENAME,
        mode='a',  # Append mode? #TODO: Verify
        maxBytes=8 * 1024 * 1024,  # Max size is 8MB
        backupCount=1,
        encoding='utf-8')
    handler.setFormatter(
        logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
    logger.addHandler(handler)
    client.logger = logger

    # TODO: Clean up wet code. Deserialization should done in a function.
    # Deserialize configuration data.
    configData = loadData(Constants.CONFIG_DATA_FILENAME)
    if configData is None:
        client.logger.info('Config data not found.')
        client.config = configuration.Configuration()
        configData = client.config.json(indent=2)
        saveData(Constants.CONFIG_DATA_FILENAME, configData)
    else:
        try:
            # Attempt to parse persistent config data to config.
            client.config = configuration.Configuration.parse_obj(configData)
            client.logger.info('Config data successfully parsed.')
        except ValidationError as e:
            client.logger.warning(
                'Exception thrown, error message is as follows:\n'
                f'{e}\n'
                'Config data was found, but could not be loaded. '
Exemple #8
0
macro_results = []
micro_results = []
codebook = utility.loadCodeBook(codebook_filename)

for k in np.arange(0, kFolds):
    # get train and test data
    training_filename = sampling + "/folds/fold" + str(k + 1) + "/train.txt"
    testing_filename = sampling + "/folds/fold" + str(k + 1) + "/test.txt"

    # determine codebook size and number of hidden states
    all_codebook, success_codebook, unsuccess_codebook = utility.loadCodeBookFromTrainingFile(
        training_filename)

    # fit successful model
    n_observations = len(success_codebook)
    sequences, seq_labels, seq_lengths = utility.loadData(
        training_filename, success_codebook, 1)
    success_model = utility.getHMMModel(succ_hidden_states, n_observations,
                                        sequences, seq_lengths)

    # fit unsuccessful model
    n_observations = len(unsuccess_codebook)
    sequences, seq_labels, seq_lengths = utility.loadData(
        training_filename, unsuccess_codebook, 0)
    unsuccess_model = utility.getHMMModel(unsucc_hidden_states, n_observations,
                                          sequences, seq_lengths)

    # get log likelihood for the given test sequence(s)
    sequences_for_success_model, seq_labels, success_seq_lengths = utility.loadData(
        testing_filename, success_codebook, 2)
    sequences_for_unsuccess_model, seq_labels, unsuccess_seq_lengths = utility.loadData(
        testing_filename, unsuccess_codebook, 2)
Exemple #9
0
def init():
    utility.createAdmin(1, "anuj")
    utility.loadData()
Exemple #10
0
'''
  File name           : main.py
  Author              : Fredrik Dahlin
  Date created        : 11/1/2016
  Date last modified  : 11/1/2016
  Python Version      : 3.4
'''
import priors
import utility
import pyclone_binomial



# Load data
data, sample_ids = utility.loadData()

# Define prior < AB | BB | NoZygosity | TCN | PCN >
prior = "TCN"

# Get possible states for each mutation
mutations = priors.getMutations(prior, data)

#data, sample_ids, tumour_content, trace_dir, num_iters, alpha, alpha_priors

error_rate = 0.001

tumour_content = {}
for id in sample_ids:
	tumour_content[id] = 1.0

trace_dir = 'trace'
Exemple #11
0
'''
  File name           : main.py
  Author              : Fredrik Dahlin
  Date created        : 11/1/2016
  Date last modified  : 11/1/2016
  Python Version      : 3.4
'''
import priors
import utility
import pyclone_binomial

# Load data
data, sample_ids = utility.loadData()

# Define prior < AB | BB | NoZygosity | TCN | PCN >
prior = "TCN"

# Get possible states for each mutation
mutations = priors.getMutations(prior, data)

#data, sample_ids, tumour_content, trace_dir, num_iters, alpha, alpha_priors

error_rate = 0.001

tumour_content = {}
for id in sample_ids:
    tumour_content[id] = 1.0

trace_dir = 'trace'

num_iters = 10000