Package: enchanter
EnchanterEnchanter is a library for machine learning tasks for comet.ml users.Getting Started•Docs•Tutorial•LicenceInstallationTo get started,install PyTorchfor your environment.
Then install Enchanter in the following way:To install the stable release.pipinstallenchanterorTo install the latest(unstable) release.pipinstallgit+https://github.com/khirotaka/enchanter.gitIf you want to install with a specific branch, you can use the following.# e.g.) Install enchanter from develop branch.pipinstallgit+https://github.com/khirotaka/enchanter.git@developSupported PlatformsEnchanter supports:macOS 10.15Ubuntu 18.04 or laterGetting StartedTry your first Enchanter Program. To train a neural network written in PyTorch on Enchanter, use theRunner.There are 2 ways to define aRunner:To use aRunneralready implemented underenchanter.tasksTo define a customRunnerthat inheritenchanter.engine.BaseRunner.Let's see how to use theenchanter.tasks.ClassificationRunner, which is the easiest way.Training Neural Networkimportcomet_mlimporttorchimportenchantermodel=torch.nn.Linear(6,10)optimizer=torch.optim.Adam(model.parameters())runner=enchanter.tasks.ClassificationRunner(model,optimizer,criterion=torch.nn.CrossEntropyLoss(),experiment=comet_ml.Experiment())runner.add_loader("train",train_loader)runner.train_config(epochs=10)runner.run()Register atorch.utils.data.DataLoaderwith theRunnerby using.add_loader().Set up the number of epochs using.train_config(), and executeRunnerwith.run().Training Unsupervised Time Series Feature LearningThe wonderful algorithms for unsupervised time series representation learning, adopted atNeurIPS 2019, are now easily available.Please prepare the following:PyTorch Model that can output feature vectors of the same length regardless of the input series.time series data consisting of[N, F, L].(Optional) A teacher label for each sample in2.importcomet_mlimporttorch.nnasnnimporttorch.optimasoptimimportenchanter.tasksastasksimportenchanter.addons.layersasLclassEncoder(nn.Module):def__init__(self,in_features,mid_features,out_features):super(Encoder,self).__init__()self.conv=nn.Sequential(L.CausalConv1d(in_features,mid_features,3),nn.LeakyReLU(),L.CausalConv1d(mid_features,mid_features,3),nn.LeakyReLU(),L.CausalConv1d(mid_features,mid_features,3),nn.LeakyReLU(),nn.AdaptiveMaxPool1d(1))self.fc=nn.Linear(mid_features,out_features)defforward(self,x):batch=x.shape[0]out=self.conv(x).reshape(batch,-1)returnself.fc(out)experiment=comet_ml.Experiment()model=Encoder(...)optimizer=optim.Adam(model.parameters())runner=tasks.TimeSeriesUnsupervisedRunner(model,optimizer,experiment)runner.add_loader("train",...)runner.run()A teacher label is required for validation. Also, Useenchanter.callbacks.EarlyStoppingForTSUSfor early stopping.Hyper parameter searching using Comet.mlfromcomet_mlimportOptimizerimporttorchimporttorch.nnasnnimporttorch.optimasoptimfromsklearn.datasetsimportload_irisimportenchanter.tasksastasksimportenchanter.addonsasaddonsimportenchanter.addons.layersaslayersfromenchanter.utilsimportcometconfig=comet.TunerConfigGenerator(algorithm="bayes",metric="train_avg_loss",objective="minimize",seed=0,trials=1,max_combo=10)config.suggest_categorical("activation",["addons.mish","torch.relu","torch.sigmoid"])opt=Optimizer(config.generate())x,y=load_iris(return_X_y=True)x=x.astype("float32")y=y.astype("int64")forexperimentinopt.get_experiments():model=layers.MLP([4,512,128,3],eval(experiment.get_parameter("activation")))optimizer=optim.Adam(model.parameters())runner=tasks.ClassificationRunner(model,optimizer=optimizer,criterion=nn.CrossEntropyLoss(),experiment=experiment)runner.fit(x,y,epochs=1,batch_size=32)runner.quite()# or# with runner:#   runner.fit(...)# or#   runner.run()Training with Mixed PrecisionRunners with defined inenchanter.tasksare now support Auto Mixed Precision.Write the following.fromtorch.cudaimportampfromenchanter.tasksimportClassificationRunnerrunner=ClassificationRunner(...)runner.scaler=amp.GradScaler()If you want to define a custom runner that supports mixed precision, do the following.fromtorch.cudaimportampimporttorch.nn.functionalasFfromenchanter.engineimportBaseRunnerclassCustomRunner(BaseRunner):# ...deftrain_step(self,batch):x,y=batchwithamp.autocast():# REQUIREDout=self.model(x)loss=F.nll_loss(out,y)return{"loss":loss}runner=CustomRunner(...)runner.scaler=amp.GradScaler()That is, you can enable AMP by usingtorch.cuda.amp.autocast()in.train_step(),.val_step()and.test_step().with-statement trainingfromcomet_mlimportExperimentimporttorchimporttorch.nnasnnimporttorch.optimasoptimfromtorch.utils.dataimportDataLoaderfromsklearn.datasetsimportload_irisfromtqdm.autoimporttqdmimportenchanter.tasksastasksimportenchanter.engine.modulesasmodulesimportenchanter.addonsasaddonsimportenchanter.addons.layersaslayersexperiment=Experiment()model=layers.MLP([4,512,128,3],addons.mish)optimizer=optim.Adam(model.parameters())x,y=load_iris(return_X_y=True)x=x.astype("float32")y=y.astype("int64")train_ds=modules.get_dataset(x,y)val_ds=modules.get_dataset(x,y)test_ds=modules.get_dataset(x,y)train_loader=DataLoader(train_ds,batch_size=32)val_loader=DataLoader(val_ds,batch_size=32)test_loader=DataLoader(test_ds,batch_size=32)runner=tasks.ClassificationRunner(model,optimizer,nn.CrossEntropyLoss(),experiment)withrunner:forepochintqdm(range(10)):withrunner.experiment.train():fortrain_batchintrain_loader:runner.optimizer.zero_grad()train_out=runner.train_step(train_batch)runner.backward(train_out["loss"])runner.update_optimizer()withrunner.experiment.validate(),torch.no_grad():forval_batchinval_loader:val_out=runner.val_step(val_batch)["loss"]runner.experiment.log_metric("val_loss",val_out)withrunner.experiment.test(),torch.no_grad():fortest_batchintest_loader:test_out=runner.test_step(test_batch)["loss"]runner.experiment.log_metric("test_loss",test_out)# The latest checkpoints (model_state & optim_state) are stored# in comet.ml after the with statement.Graph visualizationimporttorchfromenchanter.utilsimportvisualizefromenchanter.addons.layersimportAutoEncoderx=torch.randn(1,32)# [N, in_features]model=AutoEncoder([32,16,8,2])visualize.with_netron(model,(x,))LicenseApache License 2.0