Package: nnpf
Quick StartInstall from PypipipinstallnnpfInstall from sourceDownloador clone this repository:gitclonehttps://github.com/PhaseFieldICJ/nnpfcdnnpfInstallminiconda or anaconda, then prepare an environment:for CPU only:condaenvcreate-fenvironment_cpu.yml
condaactivatennpf_cpufor CPU and GPU:condaenvcreate-fenvironment_gpu.yml
condaactivatennpf_gpuToupdateinstead an already created environement:condaenvupdate-fenvironment_cpu.ymland/orcondaenvupdate-fenvironment_gpu.ymldepending on which environment you already have.Installthe nnpf module:pipinstall.Notethat you can pass the conda environment creation step and just rely onpipto download the dependencies.You can now move in yourworking directory.Self testYou can check the installation with:nnpfselftestBasic trainingLaunchthe learning of the reaction term of the Allen-Cahn equation, with default parameters:nnpftrainReaction--batch_size10and/or with custom hidden layers:nnpftrainReaction--batch_size10--layer_dims883--activationReLUIf you have an CUDA compatible GPU, you can speedup the learning by simply adding the--gpuoption:nnpftrainReaction--batch_size10--layer_dims883--activationReLU--gpu1Checkinformations of one trained model:nnpfinfoslogs/Reaction/version_0Visualizethe loss evolution and compare hyper-parameters using TensorBoard:tensorboard--logdirlogsand open your browser athttp://localhost:6006/Custom modelYou can also create a custom model in a file and make it derives from the problem you want to solve.For example, create a filemodel.pywith the following content:fromtorch.nnimportSequentialfromnnpf.problemsimportAllenCahnProblemfromnnpf.modelsimportReaction,HeatArrayfromnnpf.utilsimportget_default_argsclassModelDR(AllenCahnProblem):def__init__(self,kernel_size=17,kernel_init='zeros',layers=[8,3],activation='GaussActivation',**kwargs):super().__init__(**kwargs)# Fix kernel size to match domain dimensionifisinstance(kernel_size,int):kernel_size=[kernel_size]else:kernel_size=list(kernel_size)iflen(kernel_size)==1:kernel_size=kernel_size*self.domain.dim# Hyper-parameters (used for saving/loading the module)self.save_hyperparameters('kernel_size','kernel_init','layers','activation',)self.model=Sequential(HeatArray(kernel_size=kernel_size,init=kernel_init,bounds=self.hparams.bounds,N=self.hparams.N),Reaction(layers,activation),)defforward(self,x):returnself.model(x)@staticmethoddefadd_model_specific_args(parent_parser,defaults={}):parser=AllenCahnProblem.add_model_specific_args(parent_parser,defaults)group=parser.add_argument_group("Allen-Cahn DR","Options specific to this model")group.add_argument('--kernel_size',type=int,nargs='+',help='Size of the kernel (nD)')group.add_argument('--kernel_init',choices=['zeros','random'],help="Initialization of the convolution kernel")group.add_argument('--layers',type=int,nargs='+',help='Sizes of the hidden layers')group.add_argument('--activation',type=str,help='Name of the activation function')group.set_defaults(**{**get_default_args(ModelDR),**defaults})returnparserwith some boilerplate to handle command-line arguments and save hyper-parameters (see Lightning documentation).ModelDRis declared as a model of the Allen-Cahn problem and thus inherits from the associated training and validation datasets.You can then display the command-line interface with the--helpoption after specifying the file and the model name:nnpftrainmodel.py:ModelDR--helpYou can start the training for the 2D case with some custom arguments:nnpftrainmodel.py:ModelDR--kernel_size33--max_epochs2000--check_val_every_n_epoch100For the 3D case:nnpftrainmodel.py:ModelDR--bounds[0,1]x[0,1]x[0,1]--N64--max_epochs2000--check_val_every_n_epoch100Using a GPU:nnpftrainmodel.py:ModelDR--bounds[0,1]x[0,1]x[0,1]--N64--max_epochs2000--check_val_every_n_epoch100--gpus1Using a configuration file in YAML format:nnpftrainmodel.py:ModelDR--configconfig.yml