Views
No views yet
fold_*/ contains the trained BPNet model in two formats:fold_0/model.h5 — BPNet model in .h5 (Keras) formatfold_0/saved_model/ — BPNet model in TensorFlow SavedModel format (a directory; load directly)config.json — training / architecture parameters1import numpy as np
2import tensorflow as tf
3from scipy.special import logsumexp
4
5model = tf.saved_model.load("fold_0/saved_model")
6# sequence: (N, 2114, 4) one-hot [A,C,G,T]
7# profile_bias_input: (N, 1000, 2) per-base profile bias from WCE/Input control, or zeros
8# counts_bias_input: (N, 2) log2 total counts from WCE/Input control, or zeros
9predictions = model.signatures["serving_default"](**{
10 "sequence": sequence.astype("float32"),
11 "profile_bias_input_0": profile_bias_input.astype("float32"),
12 "counts_bias_input_0": counts_bias_input.astype("float32")})
13# predictions["profile_predictions"]: (N, 1000, 2) logits (strands NOT independent)
14# predictions["logcounts_predictions"]: (N, 1) total logcount
15
16output_len = 1000
17def vectorized_prediction_to_profile(predictions):
18 logits_arr = predictions["profile_predictions"]
19 counts_arr = predictions["logcounts_predictions"]
20 pred_profile_logits = np.reshape(logits_arr, [-1, 1, output_len * 2])
21 probVals_array = np.exp(pred_profile_logits - logsumexp(
22 pred_profile_logits, axis=2).reshape([len(logits_arr), 1, 1]))
23 profile_predictions = np.multiply(
24 np.exp(counts_arr).reshape([len(counts_arr), 1, 1]), probVals_array)
25 plus = np.reshape(profile_predictions, [len(counts_arr), output_len, 2])[:, :, 0]
26 minus = np.reshape(profile_predictions, [len(counts_arr), output_len, 2])[:, :, 1]
27 return plus, minus, counts_arr
28
29plus, minus, logcounts = vectorized_prediction_to_profile(predictions)1import numpy as np
2import tensorflow as tf
3import tensorflow.keras.backend as kb
4from tensorflow.keras.models import load_model
5from tensorflow.keras.utils import CustomObjectScope
6from bpnet.model.custommodel import CustomModel
7
8def get_model(model_path):
9 with CustomObjectScope({"kb": kb, "tf": tf, "CustomModel": CustomModel}):
10 return load_model(model_path)
11
12model = get_model("fold_0/model.h5")
13N = sequence.shape[0]
14predictions = model.predict([
15 sequence, # (N, 2114, 4)
16 np.zeros((N, 1000, 2)), # profile_bias_input (or real WCE/Input control values)
17 np.zeros((N, 2))]) # counts_bias_input (or real control log2 counts)
18# predictions[0]: (N, 1000, 2) logits; predictions[1]: (N, 1) logcounts
19# convert with the same vectorized_prediction_to_profile() (predictions[0], predictions[1])kundajelab/bpnet-atlas (placeholder — image forthcoming).