def main_train(pos_sequences=None, neg_sequences=None, prefix=None, arch_file=None, weights_file=None, **kwargs): kwargs = {key: value for key, value in kwargs.items() if value is not None} # encode fastas print("loading sequence data...") X_pos = encode_fasta_sequences(pos_sequences) y_pos = np.array([[True]] * len(X_pos)) X_neg = encode_fasta_sequences(neg_sequences) y_neg = np.array([[False]] * len(X_neg)) X = np.concatenate((X_pos, X_neg)) y = np.concatenate((y_pos, y_neg)) X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2) if arch_file is not None: # load model print("loading model...") model = SequenceDNN.load(arch_file, weights_file) else: # initialize model print("initializing model...") model = SequenceDNN(seq_length=X_train.shape[-1], **kwargs) # train print("starting model training...") model.train(X_train, y_train, validation_data=(X_valid, y_valid)) valid_result = model.test(X_valid, y_valid) print("final validation metrics:") print(valid_result) # save print("saving model files..") model.save(prefix) print("Done!")
def main_train(pos_sequences=None, neg_sequences=None, prefix=None, arch_file=None, weights_file=None, **kwargs): kwargs = {key: value for key, value in kwargs.items() if value is not None} # encode fastas print("loading sequence data...") X_pos = encode_fasta_sequences(pos_sequences) y_pos = np.array([[True]]*len(X_pos)) X_neg = encode_fasta_sequences(neg_sequences) y_neg = np.array([[False]]*len(X_neg)) X = np.concatenate((X_pos, X_neg)) y = np.concatenate((y_pos, y_neg)) X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2) if arch_file is not None: # load model print("loading model...") model = SequenceDNN.load(arch_file, weights_file) else: # initialize model print("initializing model...") model = SequenceDNN(seq_length=X_train.shape[-1], **kwargs) # train print("starting model training...") model.train(X_train, y_train, validation_data=(X_valid, y_valid)) valid_result = model.test(X_valid, y_valid) print("final validation metrics:") print(valid_result) # save print("saving model files..") model.save(prefix) print("Done!")
def main_train(pos_sequences=None, neg_sequences=None, prefix=None, model_file=None, weights_file=None): # encode fastas print("loading sequence data...") X_pos = encode_fasta_sequences(pos_sequences) y_pos = np.array([[True]]*len(X_pos)) X_neg = encode_fasta_sequences(neg_sequences) y_neg = np.array([[False]]*len(X_neg)) X = np.concatenate((X_pos, X_neg)) y = np.concatenate((y_pos, y_neg)) X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2) if model_file is not None and weights_file is not None: # load model print("loading model...") model = SequenceDNN.load(model_file, weights_file) else: # initialize model print("initializing model...") model = SequenceDNN(seq_length=X_train.shape[-1]) # train print("starting model training...") model.train(X_train, y_train, validation_data=(X_valid, y_valid)) valid_result = model.test(X_valid, y_valid) print("final validation metrics:") print(valid_result) # save print("saving model files..") model.save("%s.model.json" % (prefix), "%s.weights.hd5" % (prefix)) print("Done!")
def train_test_dnn_vary_parameter(prefix, model_parameters, param_name, param_values, X_train=None, y_train=None, X_valid=None, y_valid=None, X_test=None, y_test=None): X_train = np.concatenate((X_train, reverse_complement(X_train))) y_train = np.concatenate((y_train, y_train)) dnn_results = [] for param_value in param_values: model_parameters[param_name] = param_value ofname_infix = dict2string(model_parameters) ofname_prefix = "%s.%s" % (prefix, ofname_infix) model_fname = "%s.arch.json" % (ofname_prefix) weights_fname = "%s.weights.hd5" % (ofname_prefix) try: logger.debug("Checking for model files {} and {}...".format(model_fname, weights_fname)) dnn = SequenceDNN.load(model_fname, weights_fname) logger.debug("Model files found. Loaded model successfully!") except: logger.debug("Model files not found. Training model...") dnn = SequenceDNN(**model_parameters) logger.info("training with %s %s .." % (param_name, param_value)) dnn.train(X_train, y_train, (X_valid, y_valid)) dnn.save(model_fname, weights_fname) dnn_results.append(dnn.test(X_test, y_test)) return dnn_results
def train_test_dnn_vary_parameter(prefix, model_parameters, param_name, param_values, X_train=None, y_train=None, X_valid=None, y_valid=None, X_test=None, y_test=None): X_train = np.concatenate((X_train, reverse_complement(X_train))) y_train = np.concatenate((y_train, y_train)) dnn_results = [] for param_value in param_values: model_parameters[param_name] = param_value ofname_infix = dict2string(model_parameters) ofname_prefix = "%s.%s" % (prefix, ofname_infix) model_fname = "%s.arch.json" % (ofname_prefix) weights_fname = "%s.weights.h5" % (ofname_prefix) try: logger.debug("Checking for model files {} and {}...".format( model_fname, weights_fname)) dnn = SequenceDNN.load(model_fname, weights_fname) logger.debug("Model files found. Loaded model successfully!") except: logger.debug("Model files not found. Training model...") dnn = SequenceDNN(**model_parameters) logger.info("training with %s %s .." % (param_name, param_value)) dnn.train(X_train, y_train, (X_valid, y_valid)) dnn.save(ofname_prefix) dnn_results.append(dnn.test(X_test, y_test)) return dnn_results
def train_test_dnn_vary_data_size(prefix, model_parameters=None, X_train=None, y_train=None, X_valid=None, y_valid=None, X_test=None, y_test=None, train_set_sizes=None): dnn_results = [] for train_set_size in train_set_sizes: ofname_infix = dict2string(model_parameters) ofname_infix = "%s.train_set_size_%s" % (ofname_infix, str(train_set_size)) ofname_prefix = "%s.%s" % (prefix, ofname_infix) model_fname = "%s.arch.json" % (ofname_prefix) weights_fname = "%s.weights.h5" % (ofname_prefix) try: logger.debug("Checking for model files {} and {}...".format( model_fname, weights_fname)) best_dnn = SequenceDNN.load(model_fname, weights_fname) logger.debug("Model files found. Loaded model successfully!") except: logger.debug("Model files not found. Training model...") # try 3 attempts, take best auROC, save that model X_train_subset = X_train[:train_set_size] X_train_subset = np.concatenate( (X_train_subset, reverse_complement(X_train_subset))) y_train_subset = np.concatenate( (y_train[:train_set_size], y_train[:train_set_size])) best_auROC = 0 best_dnn = None for random_seed in [1, 2, 3]: np.random.seed(random_seed) random.seed(random_seed) dnn = SequenceDNN(**model_parameters) logger.info("training with %i examples.." % (train_set_size)) dnn.train(X_train_subset, y_train_subset, (X_valid, y_valid)) result = dnn.test(X_test, y_test) auROCs = [ result.results[i]["auROC"] for i in range(y_valid.shape[-1]) ] # get average auROC across tasks mean_auROC = sum(auROCs) / len(auROCs) if mean_auROC > best_auROC: best_auROC = mean_auROC dnn.save(ofname_prefix) best_dnn = dnn dnn_results.append(best_dnn.test(X_test, y_test)) # reset to original random seed np.random.seed(1) random.seed(1) return dnn_results
def main_train(pos_sequences=None, neg_sequences=None, pos_validation_sequences=None, neg_validation_sequences=None, prefix=None, arch_file=None, weights_file=None, **kwargs): kwargs = {key: value for key, value in kwargs.items() if value is not None} # encode fastas print("loading sequence data...") X_pos = encode_fasta_sequences(pos_sequences) y_pos = np.array([[True]] * len(X_pos)) X_neg = encode_fasta_sequences(neg_sequences) y_neg = np.array([[False]] * len(X_neg)) X = np.concatenate((X_pos, X_neg)) y = np.concatenate((y_pos, y_neg)) #if a validation set is provided by the user, encode that as well if (pos_validation_sequences != None or neg_validation_sequences != None): #both positive and negative validation sequences must be provided. assert neg_validation_sequences != None assert pos_validation_sequences != None X_valid_pos = encode_fasta_sequences(pos_validation_sequences) X_valid_neg = encode_fasta_sequences(neg_validation_sequences) y_valid_pos = np.array([[True]]) * len(X_valid_pos) y_valid_neg = np.array([[False]]) * len(X_valid_neg) X_valid = np.concatenate((X_valid_pos, X_valid_neg)) y_valid = np.concatenate((y_valid_pos, y_valid_neg)) else: X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2) if arch_file is not None: # load model print("loading model...") model = SequenceDNN.load(model_hdf5_file, arch_file, weights_file) else: # initialize model print("initializing model...") model = SequenceDNN(seq_length=X_train.shape[-1], **kwargs) # train print("starting model training...") model.train(X_train, y_train, validation_data=(X_valid, y_valid)) valid_result = model.test(X_valid, y_valid) print("final validation metrics:") print(valid_result) # save print("saving model files..") model.save(prefix) print("Done!")
def train_test_dnn_vary_data_size(prefix, model_parameters=None, X_train=None, y_train=None, X_valid=None, y_valid=None, X_test=None, y_test=None, train_set_sizes=None): dnn_results = [] for train_set_size in train_set_sizes: ofname_infix = dict2string(model_parameters) ofname_infix = "%s.train_set_size_%s" % (ofname_infix, str(train_set_size)) ofname_prefix = "%s.%s" % (prefix, ofname_infix) model_fname = "%s.arch.json" % (ofname_prefix) weights_fname = "%s.weights.hd5" % (ofname_prefix) try: logger.debug("Checking for model files {} and {}...".format(model_fname, weights_fname)) best_dnn = SequenceDNN.load(model_fname, weights_fname) logger.debug("Model files found. Loaded model successfully!") except: logger.debug("Model files not found. Training model...") # try 3 attempts, take best auROC, save that model X_train_subset = X_train[:train_set_size] X_train_subset = np.concatenate((X_train_subset, reverse_complement(X_train_subset))) y_train_subset = np.concatenate((y_train[:train_set_size], y_train[:train_set_size])) best_auROC = 0 best_dnn = None for random_seed in [1, 2, 3]: np.random.seed(random_seed) random.seed(random_seed) dnn = SequenceDNN(**model_parameters) logger.info("training with %i examples.." % (train_set_size)) dnn.train(X_train_subset, y_train_subset, (X_valid, y_valid)) result = dnn.test(X_test, y_test) auROCs = [result.results[i]["auROC"] for i in range(y_valid.shape[-1])] # get average auROC across tasks mean_auROC = sum(auROCs) / len(auROCs) if mean_auROC > best_auROC: best_auROC = mean_auROC dnn.save(model_fname, weights_fname) best_dnn = dnn dnn_results.append(best_dnn.test(X_test, y_test)) # reset to original random seed np.random.seed(1) random.seed(1) return dnn_results