python/methylnet/models.py
def train_vae(model, loader, loss_func, optimizer, cuda=True, epoch=0, kl_warm_up=0, beta=1.):
	"""Function for parameter update during VAE training for one iteration.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Data loader, generator that calls batches of data.
	loss_func : type
		Loss function for reconstruction error, nn.BCELoss or MSELoss
	optimizer : type
		SGD or Adam pytorch optimizer.
	cuda : type
		GPU?
	epoch : type
		Epoch of training, passed in from outer loop.
	kl_warm_up : type
		How many epochs until model is fully utilizes KL Loss.
	beta : type
		Weight given to KL Loss.

	Returns
	-------
	nn.Module
		Pytorch VAE model
	float
		Total Training Loss across all batches
	float
		Total Training reconstruction loss across all batches
	float
		Total KL Loss across all batches
	"""
	model.train(True) #FIXME
	#print(model)
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	stop_iter = loader.dataset.length // loader.batch_size
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	for i,(inputs, _) in enumerate(loader):
		if i == stop_iter:
			break
		inputs = Variable(inputs).view(inputs.size()[0],inputs.size()[1]) # modify for convolutions, add batchnorm2d?
		#print(inputs.size())
		if cuda:
			inputs = inputs.cuda()
		output, mean, logvar = model(inputs)
		loss, reconstruction_loss, kl_loss = vae_loss(output, inputs, mean, logvar, loss_func, epoch, kl_warm_up, beta)
		optimizer.zero_grad()
		loss.backward()
		optimizer.step()
		total_loss+=loss.item()
		total_recon_loss+=reconstruction_loss.item()
		total_kl_loss+=kl_loss.item()
	return model, total_loss,total_recon_loss,total_kl_loss
Function for parameter update during VAE training for one iteration.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Data loader, generator that calls batches of data.
	loss_func : type
		Loss function for reconstruction error, nn.BCELoss or MSELoss
	optimizer : type
		SGD or Adam pytorch optimizer.
	cuda : type
		GPU?
	epoch : type
		Epoch of training, passed in from outer loop.
	kl_warm_up : type
		How many epochs until model is fully utilizes KL Loss.
	beta : type
		Weight given to KL Loss.

	Returns
	-------
	nn.Module
		Pytorch VAE model
	float
		Total Training Loss across all batches
	float
		Total Training reconstruction loss across all batches
	float
		Total KL Loss across all batches
	
Function for parameter update during VAE training for one iteration.
Parameters

model : type
VAE torch model
loader : type
Data loader, generator that calls batches of data.
loss_func : type
Loss function for reconstruction error, nn.BCELoss or MSELoss
optimizer : type
SGD or Adam pytorch optimizer.
cuda : type
GPU.
epoch : type
Epoch of training, passed in from outer loop.
kl_warm_up : type
How many epochs until model is fully utilizes KL Loss.
beta : type
Weight given to KL Loss.

Returns

nn.Module
Pytorch VAE model
float
Total Training Loss across all batches
float
Total Training reconstruction loss across all batches
float
Total KL Loss across all batches
Function for parameter update during VAE training for one iteration . Parameters model : type VAE torch model loader : type Data loader generator that calls batches of data . loss_func : type Loss function for reconstruction error nn . BCELoss or MSELoss optimizer : type SGD or Adam pytorch optimizer . cuda : type GPU . epoch : type Epoch of training passed in from outer loop . kl_warm_up : type How many epochs until model is fully utilizes KL Loss . beta : type Weight given to KL Loss . Returns nn . Module Pytorch VAE model float Total Training Loss across all batches float Total Training reconstruction loss across all batches float Total KL Loss across all batches
def train_vae(model, loader, loss_func, optimizer, cuda=True, epoch=0, kl_warm_up=0, beta=1.):
	model.train(True) 
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	stop_iter = loader.dataset.length // loader.batch_size
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	for i,(inputs, _) in enumerate(loader):
		if i == stop_iter:
			break
		inputs = Variable(inputs).view(inputs.size()[0],inputs.size()[1]) 
		if cuda:
			inputs = inputs.cuda()
		output, mean, logvar = model(inputs)
		loss, reconstruction_loss, kl_loss = vae_loss(output, inputs, mean, logvar, loss_func, epoch, kl_warm_up, beta)
		optimizer.zero_grad()
		loss.backward()
		optimizer.step()
		total_loss+=loss.item()
		total_recon_loss+=reconstruction_loss.item()
		total_kl_loss+=kl_loss.item()
	return model, total_loss,total_recon_loss,total_kl_loss
def train_vae ( model , loader , loss_func , optimizer , cuda = True , epoch = 0 , kl_warm_up = 0 , beta = 1. ) : model . train ( True ) total_loss , total_recon_loss , total_kl_loss = 0. , 0. , 0. stop_iter = loader . dataset . length // loader . batch_size total_loss , total_recon_loss , total_kl_loss = 0. , 0. , 0. for i , ( inputs , _ ) in enumerate ( loader ) : if i == stop_iter : break inputs = Variable ( inputs ) . view ( inputs . size ( ) [ 0 ] , inputs . size ( ) [ 1 ] ) if cuda : inputs = inputs . cuda ( ) output , mean , logvar = model ( inputs ) loss , reconstruction_loss , kl_loss = vae_loss ( output , inputs , mean , logvar , loss_func , epoch , kl_warm_up , beta ) optimizer . zero_grad ( ) loss . backward ( ) optimizer . step ( ) total_loss += loss . item ( ) total_recon_loss += reconstruction_loss . item ( ) total_kl_loss += kl_loss . item ( ) return model , total_loss , total_recon_loss , total_kl_loss
Function for parameter update during VAE training for one iteration.
Function for parameter update during VAE training for one iteration .
"""Function for parameter update during VAE training for one iteration.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Data loader, generator that calls batches of data.
	loss_func : type
		Loss function for reconstruction error, nn.BCELoss or MSELoss
	optimizer : type
		SGD or Adam pytorch optimizer.
	cuda : type
		GPU?
	epoch : type
		Epoch of training, passed in from outer loop.
	kl_warm_up : type
		How many epochs until model is fully utilizes KL Loss.
	beta : type
		Weight given to KL Loss.

	Returns
	-------
	nn.Module
		Pytorch VAE model
	float
		Total Training Loss across all batches
	float
		Total Training reconstruction loss across all batches
	float
		Total KL Loss across all batches
	""" #FIXME #print(model) # modify for convolutions, add batchnorm2d? #print(inputs.size())
model
loader
loss_func
optimizer
cuda
epoch
kl_warm_up
beta