python/methylnet/models.py
def val_vae(model, loader, loss_func, optimizer, cuda=True, epoch=0, kl_warm_up=0, beta=1.):
	"""Function for validation loss computation during VAE training for one epoch.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Validation 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 Validation Loss across all batches
	float
		Total Validation reconstruction loss across all batches
	float
		Total Validation KL Loss across all batches
	"""
	model.eval() #FIXME
	#print(model)
	stop_iter = loader.dataset.length // loader.batch_size
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	with torch.no_grad():
		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)
			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 validation loss computation during VAE training for one epoch.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Validation 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 Validation Loss across all batches
	float
		Total Validation reconstruction loss across all batches
	float
		Total Validation KL Loss across all batches
	
Function for validation loss computation during VAE training for one epoch.
Parameters

model : type
VAE torch model
loader : type
Validation 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 Validation Loss across all batches
float
Total Validation reconstruction loss across all batches
float
Total Validation KL Loss across all batches
Function for validation loss computation during VAE training for one epoch . Parameters model : type VAE torch model loader : type Validation 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 Validation Loss across all batches float Total Validation reconstruction loss across all batches float Total Validation KL Loss across all batches
def val_vae(model, loader, loss_func, optimizer, cuda=True, epoch=0, kl_warm_up=0, beta=1.):
	model.eval() 
	stop_iter = loader.dataset.length // loader.batch_size
	total_loss,total_recon_loss,total_kl_loss=0.,0.,0.
	with torch.no_grad():
		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)
			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 val_vae ( model , loader , loss_func , optimizer , cuda = True , epoch = 0 , kl_warm_up = 0 , beta = 1. ) : model . eval ( ) stop_iter = loader . dataset . length // loader . batch_size total_loss , total_recon_loss , total_kl_loss = 0. , 0. , 0. with torch . no_grad ( ) : 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 ) 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 validation loss computation during VAE training for one epoch.
Function for validation loss computation during VAE training for one epoch .
"""Function for validation loss computation during VAE training for one epoch.

	Parameters
	----------
	model : type
		VAE torch model
	loader : type
		Validation 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 Validation Loss across all batches
	float
		Total Validation reconstruction loss across all batches
	float
		Total Validation 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