Skip to content

Commit

Permalink
version 0.1.2.1
Browse files Browse the repository at this point in the history
* Switch numpy mini-batching to a tensorflow dataset
* Optimizer sess operators to avoid redundant batch sampling
* Add support of epoch training
* Deprecate 'dropout_percent' for mini-batching, use batchsize instead
* Update recommended batchsize = 4
* TODO: Only models using random partition and CellBox are supported right now
  • Loading branch information
DesmondYuan committed Apr 6, 2020
1 parent 2a4e467 commit 7aae05a
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 130 deletions.
6 changes: 3 additions & 3 deletions configs/debugger.compile.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"n_x" : 99,
"trainset_ratio": 0.7,
"validset_ratio": 0.8,
"dropout_percent": 0.8,
"batchsize": 40,
"batchsize": 4,

"envelop_form": "hill",
"dT": 0.1,
Expand All @@ -23,7 +22,8 @@
"ode_solver": "rk4",
"ode_last_steps": 2,

"n_iter": 100,
"n_epoch": 10000,
"n_iter": 10000,
"n_iter_buffer":5,
"n_iter_patience":10,

Expand Down
2 changes: 0 additions & 2 deletions pertbio/pertbio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
name = "pertbio"

from pertbio.config import Config
from pertbio.model import *
from pertbio.kernel import *
Expand Down
19 changes: 10 additions & 9 deletions pertbio/pertbio/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
import tensorflow as tf


def factory(cfg):
Expand Down Expand Up @@ -33,15 +34,15 @@ def s2c(cfg):
train_data = cfg.expr[~testidx]
dataset = {
"node_index": cfg.node_index,
"pert_train": pert_train.iloc[valid_pos[:ntrain], :],
"pert_valid": pert_train.iloc[valid_pos[ntrain:], :],
"pert_train": pert_train.iloc[valid_pos[:ntrain], :].values,
"pert_valid": pert_train.iloc[valid_pos[ntrain:], :].values,
"pert_test": cfg.pert[testidx],
"pert_full": cfg.pert,
"train_data": train_data.iloc[valid_pos[:ntrain], :],
"valid_data": train_data.iloc[valid_pos[ntrain:], :],
"train_data": train_data.iloc[valid_pos[:ntrain], :].values,
"valid_data": train_data.iloc[valid_pos[ntrain:], :].values,
"test_data": cfg.expr[testidx],
"train_pos": valid_pos[:ntrain],
"valid_pos": valid_pos[ntrain:],
"train_pos": valid_pos[:ntrain].values,
"valid_pos": valid_pos[ntrain:].values,
"test_pos": testidx
}

Expand Down Expand Up @@ -98,9 +99,9 @@ def random_partition(cfg):
"pert_valid": cfg.pert.iloc[random_pos[ntrain:nvalid], :],
"pert_test": cfg.pert.iloc[random_pos[nvalid:], :],
"pert_full": cfg.pert,
"train_data": cfg.expr.iloc[random_pos[:ntrain], :],
"valid_data": cfg.expr.iloc[random_pos[ntrain:nvalid], :],
"test_data": cfg.expr.iloc[random_pos[nvalid:], :],
"expr_train": cfg.expr.iloc[random_pos[:ntrain], :],
"expr_valid": cfg.expr.iloc[random_pos[ntrain:nvalid], :],
"expr_test": cfg.expr.iloc[random_pos[nvalid:], :],
"train_pos": random_pos[:ntrain],
"valid_pos": random_pos[ntrain:nvalid],
"test_pos": random_pos[nvalid:]
Expand Down
58 changes: 33 additions & 25 deletions pertbio/pertbio/model.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import numpy as np
import pertbio.kernel
from pertbio.utils import loss, optimize
import tensorflow as tf


from pertbio.utils import loss, optimize
# import tensorflow_probability as tfp


Expand All @@ -19,9 +17,9 @@ def factory(args):
elif args.model == 'NN':
return NN(args).build
# elif args.model == 'Bayesian':
# return BN(args).build()
# TODO: baysian model

# return BN(args).build()
else:
raise Exception("Illegal model name. Choose from [{}]".format(
'CellBox, CoExp, LinReg, NN, CoExp_nonlinear, Bayesian'
Expand All @@ -32,22 +30,31 @@ class PertBio:
def __init__(self, args):
self.args = args
self.n_x = args.n_x
self.mu = tf.compat.v1.placeholder(tf.float32, [None, self.n_x])
self.mu = tf.compat.v1.placeholder(tf.float32, [None, self.n_x])
self.x_gold = tf.compat.v1.placeholder(tf.float32, [None, self.n_x])
self.idx = tf.compat.v1.placeholder(tf.int32, [None])
self.pert_in = tf.compat.v1.placeholder(tf.float32, [None, self.n_x], name='pert_in')
self.expr_out = tf.compat.v1.placeholder(tf.float32, [None, self.n_x], name='expr_out')

# Prepare datasets
dataset = tf.data.Dataset.from_tensor_slices((self.pert_in, self.expr_out))
self.iter = tf.compat.v1.data.make_initializable_iterator(dataset
.shuffle(buffer_size=1024).batch(args.batchsize))
self.train_x, self.train_y = self.iter.get_next()
self.iter_eval = tf.compat.v1.data.make_initializable_iterator(dataset
.shuffle(buffer_size=1024).batch(args.batchsize))
self.eval_x, self.eval_y = self.iter_eval.get_next()

def get_ops(self):
self.l1_lambda = tf.compat.v1.placeholder(tf.float32)
self.loss, self.loss_mse = loss(self.x_gold, self.xhat,
self.l1_lambda, self.params['W'])
self.lr = tf.compat.v1.placeholder(tf.float32)
self.op_optimize = optimize(self.loss, self.lr)
self.l1_lambda = tf.compat.v1.placeholder(tf.float32, name='lambda')
self.train_loss, self.train_mse_loss = loss(self.train_y, self.train_yhat, self.l1_lambda, self.params['W'])
self.eval_loss, self.eval_mse_loss = loss(self.eval_y, self.eval_yhat, self.l1_lambda, self.params['W'])

self.lr = tf.compat.v1.placeholder(tf.float32, name='lr')
self.op_optimize = optimize(self.train_loss, self.lr)

def build(self):
self.params = {}
self.get_variables()
self.xhat = self.forward(self.mu)
self.train_yhat = self.forward(self.train_x)
self.eval_yhat = self.forward(self.eval_x)
self.get_ops()
return self

Expand Down Expand Up @@ -135,16 +142,16 @@ def get_variables(self):

# TODO: fix after redesign CoExp class
# def forward(self, mu):
# # during training, use mu_full, while during testing use mu
# idx = tf.map_fn(fn=get_idx_pair, elems=mu, dtype=tf.int32)
# # mask the models for prediction
# Ws = tf.gather_nd(self.params['Ws'], idx) # batch_size x [Params,]
# bs = tf.gather_nd(self.params['bs'], idx)
# hidden = tf.tensordot(Ws, tf.transpose(x_gold), axes=1) + bs # batch_size x [Params,] x batch_size
# hidden_transposed = tf.transpose(hidden, perm=[0, 2, 1])
# hidden_masked = tf.gather_nd(hidden_transposed, tf.compat.v2.where(tf.eye(tf.shape(mu)[0])))
# xhat = tf.matmul(tf.tanh(hidden_masked), self.params['W']) + tf.reshape(self.params['b'], [1, -1])
# return xhat
# # during training, use mu_full, while during testing use mu
# idx = tf.map_fn(fn=get_idx_pair, elems=mu, dtype=tf.int32)
# # mask the models for prediction
# Ws = tf.gather_nd(self.params['Ws'], idx) # batch_size x [Params,]
# bs = tf.gather_nd(self.params['bs'], idx)
# hidden = tf.tensordot(Ws, tf.transpose(x_gold), axes=1) + bs # batch_size x [Params,] x batch_size
# hidden_transposed = tf.transpose(hidden, perm=[0, 2, 1])
# hidden_masked = tf.gather_nd(hidden_transposed, tf.compat.v2.where(tf.eye(tf.shape(mu)[0])))
# xhat = tf.matmul(tf.tanh(hidden_masked), self.params['W']) + tf.reshape(self.params['b'], [1, -1])
# return xhat


class LinReg(PertBio):
Expand Down Expand Up @@ -187,7 +194,8 @@ def build(self):
self.envelop_fn = pertbio.kernel.get_envelop(self.args)
self.ode_solver = pertbio.kernel.get_ode_solver(self.args)
self._dxdt = pertbio.kernel.get_dxdt(self.args, self.params)
self.convergence_metric, self.xhat = self.forward(self.mu)
self.convergence_metric_train, self.train_yhat = self.forward(self.train_x)
self.convergence_metric_eval, self.eval_yhat = self.forward(self.eval_x)
self.get_ops()
return self

Expand Down
Loading

0 comments on commit 7aae05a

Please sign in to comment.