python/methylnet/models.py
def train_mlp(model, loader, loss_func, optimizer_vae, optimizer_mlp, cuda=True, categorical=False, train_decoder=False):
	"""Train Multi-layer perceptron appended to latent embeddings of VAE via transfer learning. Do this for one iteration.

	Parameters
	----------
	model : type
		VAE_MLP model.
	loader : type
		DataLoader with MethylationDataset.
	loss_func : type
		Loss function (BCE, CrossEntropy, MSE).
	optimizer_vae : type
		Optimizer for pytorch VAE.
	optimizer_mlp : type
		Optimizer for outcome MLP layers.
	cuda : type
		GPU?
	categorical : type
		Predicting categorical or continuous outcomes.
	train_decoder : type
		Retrain decoder during training loop to adjust for fine-tuned embeddings.

	Returns
	-------
	nn.Module
		Training VAE_MLP model with updated parameters.
	float
		Training loss over all batches

	"""
	model.train(True)

	#model.vae.eval() also freeze for depth of tuning?
	#print(loss_func)
	stop_iter = loader.dataset.length // loader.batch_size
	running_loss=0.
	running_decoder_loss=0.
	for i,(inputs, y_true) in enumerate(loader): # change dataloder for classification/regression tasks
		#print(samples)
		if inputs.size()[0] == 1 and i == stop_iter:
			break
		inputs = Variable(inputs).view(inputs.size()[0],inputs.size()[1])
		y_true = Variable(y_true)
		if categorical:
			y_true=y_true.argmax(1).long()
		#print(inputs.size())
		if cuda:
			inputs = inputs.cuda()
			y_true = y_true.cuda()
		y_predict, z = model(inputs)
		loss = loss_func(y_predict,y_true)

		optimizer_vae.zero_grad()
		optimizer_mlp.zero_grad()
		loss.backward()
		if train_decoder:
			model, decoder_loss = train_decoder_(model, inputs, z)
			running_decoder_loss += decoder_loss
		optimizer_vae.step()
		optimizer_mlp.step()
		running_loss+=loss.item()
	if train_decoder:
		print('Decoder Loss is {}'.format(running_decoder_loss))
	return model, running_loss
Train Multi-layer perceptron appended to latent embeddings of VAE via transfer learning. Do this for one iteration.

	Parameters
	----------
	model : type
		VAE_MLP model.
	loader : type
		DataLoader with MethylationDataset.
	loss_func : type
		Loss function (BCE, CrossEntropy, MSE).
	optimizer_vae : type
		Optimizer for pytorch VAE.
	optimizer_mlp : type
		Optimizer for outcome MLP layers.
	cuda : type
		GPU?
	categorical : type
		Predicting categorical or continuous outcomes.
	train_decoder : type
		Retrain decoder during training loop to adjust for fine-tuned embeddings.

	Returns
	-------
	nn.Module
		Training VAE_MLP model with updated parameters.
	float
		Training loss over all batches

	
Train Multi-layer perceptron appended to latent embeddings of VAE via transfer learning. Do this for one iteration.
Parameters

model : type
VAE_MLP model.

Returns

nn.Module
Training VAE_MLP model with updated parameters.
float
Training loss over all batches
Train Multi - layer perceptron appended to latent embeddings of VAE via transfer learning . Do this for one iteration . Parameters model : type VAE_MLP model . Returns nn . Module Training VAE_MLP model with updated parameters . float Training loss over all batches
def train_mlp(model, loader, loss_func, optimizer_vae, optimizer_mlp, cuda=True, categorical=False, train_decoder=False):
	model.train(True)
	stop_iter = loader.dataset.length // loader.batch_size
	running_loss=0.
	running_decoder_loss=0.
	for i,(inputs, y_true) in enumerate(loader): 
		if inputs.size()[0] == 1 and i == stop_iter:
			break
		inputs = Variable(inputs).view(inputs.size()[0],inputs.size()[1])
		y_true = Variable(y_true)
		if categorical:
			y_true=y_true.argmax(1).long()
		if cuda:
			inputs = inputs.cuda()
			y_true = y_true.cuda()
		y_predict, z = model(inputs)
		loss = loss_func(y_predict,y_true)
		optimizer_vae.zero_grad()
		optimizer_mlp.zero_grad()
		loss.backward()
		if train_decoder:
			model, decoder_loss = train_decoder_(model, inputs, z)
			running_decoder_loss += decoder_loss
		optimizer_vae.step()
		optimizer_mlp.step()
		running_loss+=loss.item()
	if train_decoder:
		print('Decoder Loss is {}'.format(running_decoder_loss))
	return model, running_loss
def train_mlp ( model , loader , loss_func , optimizer_vae , optimizer_mlp , cuda = True , categorical = False , train_decoder = False ) : model . train ( True ) stop_iter = loader . dataset . length // loader . batch_size running_loss = 0. running_decoder_loss = 0. for i , ( inputs , y_true ) in enumerate ( loader ) : if inputs . size ( ) [ 0 ] == 1 and i == stop_iter : break inputs = Variable ( inputs ) . view ( inputs . size ( ) [ 0 ] , inputs . size ( ) [ 1 ] ) y_true = Variable ( y_true ) if categorical : y_true = y_true . argmax ( 1 ) . long ( ) if cuda : inputs = inputs . cuda ( ) y_true = y_true . cuda ( ) y_predict , z = model ( inputs ) loss = loss_func ( y_predict , y_true ) optimizer_vae . zero_grad ( ) optimizer_mlp . zero_grad ( ) loss . backward ( ) if train_decoder : model , decoder_loss = train_decoder_ ( model , inputs , z ) running_decoder_loss += decoder_loss optimizer_vae . step ( ) optimizer_mlp . step ( ) running_loss += loss . item ( ) if train_decoder : print ( 'Decoder Loss is {}' . format ( running_decoder_loss ) ) return model , running_loss
Train Multi-layer perceptron appended to latent embeddings of VAE via transfer learning.
Train Multi - layer perceptron appended to latent embeddings of VAE via transfer learning .
"""Train Multi-layer perceptron appended to latent embeddings of VAE via transfer learning. Do this for one iteration.

	Parameters
	----------
	model : type
		VAE_MLP model.
	loader : type
		DataLoader with MethylationDataset.
	loss_func : type
		Loss function (BCE, CrossEntropy, MSE).
	optimizer_vae : type
		Optimizer for pytorch VAE.
	optimizer_mlp : type
		Optimizer for outcome MLP layers.
	cuda : type
		GPU?
	categorical : type
		Predicting categorical or continuous outcomes.
	train_decoder : type
		Retrain decoder during training loop to adjust for fine-tuned embeddings.

	Returns
	-------
	nn.Module
		Training VAE_MLP model with updated parameters.
	float
		Training loss over all batches

	""" #model.vae.eval() also freeze for depth of tuning? #print(loss_func) # change dataloder for classification/regression tasks #print(samples) #print(inputs.size())
model
loader
loss_func
optimizer_vae
optimizer_mlp
cuda
categorical
train_decoder