python/methylnet/models.py
def fit(self, train_data):
		"""Fit VAE model to training data, best model returned with lowest validation loss over epochs.

		Parameters
		----------
		train_data : DataLoader
			Training DataLoader that is loading MethylationDataset in batches.

		Returns
		-------
		self
			Autoencoder object with updated VAE model.

		"""
		loss_list = []
		model = self.model
		best_model=copy.deepcopy(self.model)
		animation_plts=[]
		plt_data={'kl_loss':[],'recon_loss':[],'lr':[],'val_kl_loss':[],'val_recon_loss':[], 'val_loss':[]}
		for epoch in range(self.n_epochs):
			model, loss, recon_loss, kl_loss = train_vae(model, train_data, self.loss_fn, self.optimizer, self.cuda, epoch, self.kl_warm_up, self.beta)
			self.scheduler.step()
			plt_data['kl_loss'].append(kl_loss)
			plt_data['recon_loss'].append(recon_loss)
			plt_data['lr'].append(self.scheduler.get_lr())
			print("Epoch {}: Loss {}, Recon Loss {}, KL-Loss {}".format(epoch,loss,recon_loss,kl_loss))
			if self.validation_set:
				model, val_loss, val_recon_loss, val_kl_loss = val_vae(model, self.validation_set, self.loss_fn, self.optimizer, self.cuda, epoch, self.kl_warm_up, self.beta)
				plt_data['val_kl_loss'].append(val_kl_loss)
				plt_data['val_recon_loss'].append(val_recon_loss)
				plt_data['val_loss'].append(val_loss)
				print("Epoch {}: Val-Loss {}, Val-Recon Loss {}, Val-KL-Loss {}".format(epoch,val_loss,val_recon_loss,val_kl_loss))
			if epoch >= self.kl_warm_up:
				loss = loss if not self.validation_set else val_loss
				loss_list.append(loss)
				if loss <= min(loss_list): # next get models for lowest reconstruction and kl, 3 models
					best_model=copy.deepcopy(model)#.state_dict())
					best_epoch=epoch
				if 0 and epoch % self.embed_interval == 0:
					z,samples,outcomes=project_vae(best_model, train_data if not self.validation_set else self.validation_set, self.cuda)
					beta_df=pd.DataFrame(z,index=samples)
					plotly_plot(umap_embed(beta_df,outcomes,n_neighbors=8,supervised=False,min_dist=0.2,metric='euclidean'),'training_{}.html'.format(best_epoch))
			if 0 and self.plot_interval and epoch % self.plot_interval == 0:
				z,_,outcomes=project_vae(model, train_data, self.cuda)
				animation_plts.append(Plot('Latent Embedding, epoch {}'.format(epoch),
						data=PlotTransformer(z,LabelEncoder().fit_transform(outcomes)).transform()))
		if 0:
			plts=Plotter([Plot(k,'epoch','lr' if 'loss' not in k else k,
						  pd.DataFrame(np.vstack((range(len(plt_data[k])),plt_data[k])).T,
									   columns=['x','y'])) for k in plt_data if plt_data[k]],animation=False)
			plts.write_plots(self.loss_plt_fname)
		if 0 and self.plot_interval:
			Plotter(animation_plts).write_plots(self.vae_animation_fname)
		self.min_loss = min(np.array(plt_data['kl_loss'])+np.array(plt_data['recon_loss']))
		if self.validation_set:
			self.min_val_loss = plt_data['val_loss'][best_epoch]
			self.min_val_kl_loss = plt_data['val_kl_loss'][best_epoch]
			self.min_val_recon_loss = plt_data['val_recon_loss'][best_epoch]
		else:
			self.min_val_loss, self.min_val_kl_loss, self.min_val_recon_loss  = -1.,-1.,-1.
		self.best_epoch = best_epoch
		self.model = best_model#self.model.load_state_dict(best_model)
		self.training_plot_data = plt_data
		return self
Fit VAE model to training data, best model returned with lowest validation loss over epochs.

		Parameters
		----------
		train_data : DataLoader
			Training DataLoader that is loading MethylationDataset in batches.

		Returns
		-------
		self
			Autoencoder object with updated VAE model.

		
Fit VAE model to training data, best model returned with lowest validation loss over epochs.
Parameters



Returns

self
Autoencoder object with updated VAE model.
Fit VAE model to training data best model returned with lowest validation loss over epochs . Parameters Returns self Autoencoder object with updated VAE model .
def fit(self, train_data):
		loss_list = []
		model = self.model
		best_model=copy.deepcopy(self.model)
		animation_plts=[]
		plt_data={'kl_loss':[],'recon_loss':[],'lr':[],'val_kl_loss':[],'val_recon_loss':[], 'val_loss':[]}
		for epoch in range(self.n_epochs):
			model, loss, recon_loss, kl_loss = train_vae(model, train_data, self.loss_fn, self.optimizer, self.cuda, epoch, self.kl_warm_up, self.beta)
			self.scheduler.step()
			plt_data['kl_loss'].append(kl_loss)
			plt_data['recon_loss'].append(recon_loss)
			plt_data['lr'].append(self.scheduler.get_lr())
			print("Epoch {}: Loss {}, Recon Loss {}, KL-Loss {}".format(epoch,loss,recon_loss,kl_loss))
			if self.validation_set:
				model, val_loss, val_recon_loss, val_kl_loss = val_vae(model, self.validation_set, self.loss_fn, self.optimizer, self.cuda, epoch, self.kl_warm_up, self.beta)
				plt_data['val_kl_loss'].append(val_kl_loss)
				plt_data['val_recon_loss'].append(val_recon_loss)
				plt_data['val_loss'].append(val_loss)
				print("Epoch {}: Val-Loss {}, Val-Recon Loss {}, Val-KL-Loss {}".format(epoch,val_loss,val_recon_loss,val_kl_loss))
			if epoch >= self.kl_warm_up:
				loss = loss if not self.validation_set else val_loss
				loss_list.append(loss)
				if loss <= min(loss_list): 
					best_model=copy.deepcopy(model)
					best_epoch=epoch
				if 0 and epoch % self.embed_interval == 0:
					z,samples,outcomes=project_vae(best_model, train_data if not self.validation_set else self.validation_set, self.cuda)
					beta_df=pd.DataFrame(z,index=samples)
					plotly_plot(umap_embed(beta_df,outcomes,n_neighbors=8,supervised=False,min_dist=0.2,metric='euclidean'),'training_{}.html'.format(best_epoch))
			if 0 and self.plot_interval and epoch % self.plot_interval == 0:
				z,_,outcomes=project_vae(model, train_data, self.cuda)
				animation_plts.append(Plot('Latent Embedding, epoch {}'.format(epoch),
						data=PlotTransformer(z,LabelEncoder().fit_transform(outcomes)).transform()))
		if 0:
			plts=Plotter([Plot(k,'epoch','lr' if 'loss' not in k else k,
						  pd.DataFrame(np.vstack((range(len(plt_data[k])),plt_data[k])).T,
									   columns=['x','y'])) for k in plt_data if plt_data[k]],animation=False)
			plts.write_plots(self.loss_plt_fname)
		if 0 and self.plot_interval:
			Plotter(animation_plts).write_plots(self.vae_animation_fname)
		self.min_loss = min(np.array(plt_data['kl_loss'])+np.array(plt_data['recon_loss']))
		if self.validation_set:
			self.min_val_loss = plt_data['val_loss'][best_epoch]
			self.min_val_kl_loss = plt_data['val_kl_loss'][best_epoch]
			self.min_val_recon_loss = plt_data['val_recon_loss'][best_epoch]
		else:
			self.min_val_loss, self.min_val_kl_loss, self.min_val_recon_loss  = -1.,-1.,-1.
		self.best_epoch = best_epoch
		self.model = best_model
		self.training_plot_data = plt_data
		return self
def fit ( self , train_data ) : loss_list = [ ] model = self . model best_model = copy . deepcopy ( self . model ) animation_plts = [ ] plt_data = { 'kl_loss' : [ ] , 'recon_loss' : [ ] , 'lr' : [ ] , 'val_kl_loss' : [ ] , 'val_recon_loss' : [ ] , 'val_loss' : [ ] } for epoch in range ( self . n_epochs ) : model , loss , recon_loss , kl_loss = train_vae ( model , train_data , self . loss_fn , self . optimizer , self . cuda , epoch , self . kl_warm_up , self . beta ) self . scheduler . step ( ) plt_data [ 'kl_loss' ] . append ( kl_loss ) plt_data [ 'recon_loss' ] . append ( recon_loss ) plt_data [ 'lr' ] . append ( self . scheduler . get_lr ( ) ) print ( "Epoch {}: Loss {}, Recon Loss {}, KL-Loss {}" . format ( epoch , loss , recon_loss , kl_loss ) ) if self . validation_set : model , val_loss , val_recon_loss , val_kl_loss = val_vae ( model , self . validation_set , self . loss_fn , self . optimizer , self . cuda , epoch , self . kl_warm_up , self . beta ) plt_data [ 'val_kl_loss' ] . append ( val_kl_loss ) plt_data [ 'val_recon_loss' ] . append ( val_recon_loss ) plt_data [ 'val_loss' ] . append ( val_loss ) print ( "Epoch {}: Val-Loss {}, Val-Recon Loss {}, Val-KL-Loss {}" . format ( epoch , val_loss , val_recon_loss , val_kl_loss ) ) if epoch >= self . kl_warm_up : loss = loss if not self . validation_set else val_loss loss_list . append ( loss ) if loss <= min ( loss_list ) : best_model = copy . deepcopy ( model ) best_epoch = epoch if 0 and epoch % self . embed_interval == 0 : z , samples , outcomes = project_vae ( best_model , train_data if not self . validation_set else self . validation_set , self . cuda ) beta_df = pd . DataFrame ( z , index = samples ) plotly_plot ( umap_embed ( beta_df , outcomes , n_neighbors = 8 , supervised = False , min_dist = 0.2 , metric = 'euclidean' ) , 'training_{}.html' . format ( best_epoch ) ) if 0 and self . plot_interval and epoch % self . plot_interval == 0 : z , _ , outcomes = project_vae ( model , train_data , self . cuda ) animation_plts . append ( Plot ( 'Latent Embedding, epoch {}' . format ( epoch ) , data = PlotTransformer ( z , LabelEncoder ( ) . fit_transform ( outcomes ) ) . transform ( ) ) ) if 0 : plts = Plotter ( [ Plot ( k , 'epoch' , 'lr' if 'loss' not in k else k , pd . DataFrame ( np . vstack ( ( range ( len ( plt_data [ k ] ) ) , plt_data [ k ] ) ) . T , columns = [ 'x' , 'y' ] ) ) for k in plt_data if plt_data [ k ] ] , animation = False ) plts . write_plots ( self . loss_plt_fname ) if 0 and self . plot_interval : Plotter ( animation_plts ) . write_plots ( self . vae_animation_fname ) self . min_loss = min ( np . array ( plt_data [ 'kl_loss' ] ) + np . array ( plt_data [ 'recon_loss' ] ) ) if self . validation_set : self . min_val_loss = plt_data [ 'val_loss' ] [ best_epoch ] self . min_val_kl_loss = plt_data [ 'val_kl_loss' ] [ best_epoch ] self . min_val_recon_loss = plt_data [ 'val_recon_loss' ] [ best_epoch ] else : self . min_val_loss , self . min_val_kl_loss , self . min_val_recon_loss = - 1. , - 1. , - 1. self . best_epoch = best_epoch self . model = best_model self . training_plot_data = plt_data return self
Fit VAE model to training data, best model returned with lowest validation loss over epochs.
Fit VAE model to training data best model returned with lowest validation loss over epochs .
"""Fit VAE model to training data, best model returned with lowest validation loss over epochs.

		Parameters
		----------
		train_data : DataLoader
			Training DataLoader that is loading MethylationDataset in batches.

		Returns
		-------
		self
			Autoencoder object with updated VAE model.

		""" # next get models for lowest reconstruction and kl, 3 models #.state_dict()) #self.model.load_state_dict(best_model)
self
train_data