python/methylnet/models.py
def vae_loss(output, input, mean, logvar, loss_func, epoch, kl_warm_up=0, beta=1.):
	"""Function to calculate VAE Loss, Reconstruction Loss + Beta KLLoss.

	Parameters
	----------
	output : torch.tensor
		Reconstructed output from autoencoder.
	input : torch.tensor
		Original input data.
	mean : type
		Learned mean tensor for each sample point.
	logvar : type
		Variation around that mean sample point, learned from reparameterization.
	loss_func : type
		Loss function for reconstruction loss, MSE or BCE.
	epoch : type
		Epoch of training.
	kl_warm_up : type
		Number of epochs until fully utilizing KLLoss, begin saving models here.
	beta : type
		Weighting for KLLoss.

	Returns
	-------
	torch.tensor
		Total loss
	torch.tensor
		Recon loss
	torch.tensor
		KL loss

	"""
	if type(output) != type([]):
		output = [output]
	recon_loss = sum([loss_func(out, input) for out in output])
	kl_loss = torch.mean(0.5 * torch.sum(
		torch.exp(logvar) + mean**2 - 1. - logvar, 1))
	kl_loss *= beta
	if epoch < kl_warm_up:
		kl_loss *= np.clip(epoch/kl_warm_up,0.,1.)
	#print(recon_loss,kl_loss)
	return recon_loss + kl_loss, recon_loss, kl_loss
Function to calculate VAE Loss, Reconstruction Loss + Beta KLLoss.

	Parameters
	----------
	output : torch.tensor
		Reconstructed output from autoencoder.
	input : torch.tensor
		Original input data.
	mean : type
		Learned mean tensor for each sample point.
	logvar : type
		Variation around that mean sample point, learned from reparameterization.
	loss_func : type
		Loss function for reconstruction loss, MSE or BCE.
	epoch : type
		Epoch of training.
	kl_warm_up : type
		Number of epochs until fully utilizing KLLoss, begin saving models here.
	beta : type
		Weighting for KLLoss.

	Returns
	-------
	torch.tensor
		Total loss
	torch.tensor
		Recon loss
	torch.tensor
		KL loss

	
Function to calculate VAE Loss, Reconstruction Loss + Beta KLLoss.
Parameters

output : torch.tensor
Reconstructed output from autoencoder.
input : torch.tensor
Original input data.
mean : type
Learned mean tensor for each sample point.
logvar : type
Variation around that mean sample point, learned from reparameterization.
loss_func : type
Loss function for reconstruction loss, MSE or BCE.
epoch : type
Epoch of training.
kl_warm_up : type
Number of epochs until fully utilizing KLLoss, begin saving models here.
beta : type
Weighting for KLLoss.

Returns


Function to calculate VAE Loss Reconstruction Loss + Beta KLLoss . Parameters output : torch . tensor Reconstructed output from autoencoder . input : torch . tensor Original input data . mean : type Learned mean tensor for each sample point . logvar : type Variation around that mean sample point learned from reparameterization . loss_func : type Loss function for reconstruction loss MSE or BCE . epoch : type Epoch of training . kl_warm_up : type Number of epochs until fully utilizing KLLoss begin saving models here . beta : type Weighting for KLLoss . Returns
def vae_loss(output, input, mean, logvar, loss_func, epoch, kl_warm_up=0, beta=1.):
	if type(output) != type([]):
		output = [output]
	recon_loss = sum([loss_func(out, input) for out in output])
	kl_loss = torch.mean(0.5 * torch.sum(
		torch.exp(logvar) + mean**2 - 1. - logvar, 1))
	kl_loss *= beta
	if epoch < kl_warm_up:
		kl_loss *= np.clip(epoch/kl_warm_up,0.,1.)
	return recon_loss + kl_loss, recon_loss, kl_loss
def vae_loss ( output , input , mean , logvar , loss_func , epoch , kl_warm_up = 0 , beta = 1. ) : if type ( output ) != type ( [ ] ) : output = [ output ] recon_loss = sum ( [ loss_func ( out , input ) for out in output ] ) kl_loss = torch . mean ( 0.5 * torch . sum ( torch . exp ( logvar ) + mean ** 2 - 1. - logvar , 1 ) ) kl_loss *= beta if epoch < kl_warm_up : kl_loss *= np . clip ( epoch / kl_warm_up , 0. , 1. ) return recon_loss + kl_loss , recon_loss , kl_loss
Function to calculate VAE Loss, Reconstruction Loss + Beta KLLoss.
Function to calculate VAE Loss Reconstruction Loss + Beta KLLoss .
"""Function to calculate VAE Loss, Reconstruction Loss + Beta KLLoss.

	Parameters
	----------
	output : torch.tensor
		Reconstructed output from autoencoder.
	input : torch.tensor
		Original input data.
	mean : type
		Learned mean tensor for each sample point.
	logvar : type
		Variation around that mean sample point, learned from reparameterization.
	loss_func : type
		Loss function for reconstruction loss, MSE or BCE.
	epoch : type
		Epoch of training.
	kl_warm_up : type
		Number of epochs until fully utilizing KLLoss, begin saving models here.
	beta : type
		Weighting for KLLoss.

	Returns
	-------
	torch.tensor
		Total loss
	torch.tensor
		Recon loss
	torch.tensor
		KL loss

	""" #print(recon_loss,kl_loss)
output
input
mean
logvar
loss_func
epoch
kl_warm_up
beta