Skip to main content

Quick Start

Python

This package requires a Python version between 3.10 and 3.12 (versions above have not been tested).

You can use a currently installed Python or, for example, create a conda environment:

conda create --name nnpf python=3.10
conda activate nnpf

If you need a specific computation platform context, like an older CUDA version or the support of ROCm, you should install PyTorch manually using the instructions available on the official website.

Install from Pypi

pip install nnpf

Install from source

Download or clone this repository:

git clone https://github.com/PhaseFieldICJ/nnpf
cd nnpf

Install the nnpf module:

pip install .

You can now move in your working directory.

Self test

You can check the installation with:

nnpf selftest

Basic training

Launch the learning of the reaction term of the Allen-Cahn equation, with default parameters:

nnpf train Reaction --batch_size 10

and/or with custom hidden layers:

nnpf train Reaction --batch_size 10 --layer_dims 8 8 3 --activation ReLU

If you have an CUDA compatible GPU, you can speedup the learning by simply adding the --gpu option:

nnpf train Reaction --batch_size 10 --layer_dims 8 8 3 --activation ReLU --gpu 1

Check informations of one trained model:

nnpf infos logs/Reaction/version_0

Visualize the loss evolution and compare hyper-parameters using TensorBoard:

tensorboard --logdir logs

and open your browser at http://localhost:6006/

Custom model

You can also create a custom model in a file and make it derives from the problem you want to solve.

For example, create a file model.py with the following content:

from torch.nn import Sequential

from nnpf.problems import AllenCahnProblem
from nnpf.models import Reaction, HeatArray
from nnpf.utils import get_default_args

class ModelDR(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 dimension
        if isinstance(kernel_size, int):
            kernel_size = [kernel_size]
        else:
            kernel_size = list(kernel_size)
        if len(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),
        )

    def forward(self, x):
        return self.model(x)

    @staticmethod
    def add_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})
        return parser

with some boilerplate to handle command-line arguments and save hyper-parameters (see Lightning documentation). ModelDR is 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 --help option after specifying the file and the model name:

nnpf train model.py:ModelDR --help

You can start the training for the 2D case with some custom arguments:

nnpf train model.py:ModelDR --kernel_size 33 --max_epochs 2000 --check_val_every_n_epoch 100

For the 3D case:

nnpf train model.py:ModelDR --bounds [0,1]x[0,1]x[0,1] --N 64 --max_epochs 2000 --check_val_every_n_epoch 100

Using a GPU:

nnpf train model.py:ModelDR --bounds [0,1]x[0,1]x[0,1] --N 64 --max_epochs 2000 --check_val_every_n_epoch 100 --gpus 1

Using a configuration file in YAML format:

nnpf train model.py:ModelDR --config config.yml

Change log

Version 1.1.1

  • fixing radius of Wulff shape when customizing data orientation (and in anisotropical case).

Version 1.1.0

  • fixing dependency versions so that it can be installed on up-to-date OS,
  • adding theta parameter to rotate the training/validation dataset.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nnpf-1.1.1.tar.gz (63.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nnpf-1.1.1-py3-none-any.whl (84.7 kB view details)

Uploaded Python 3

File details

Details for the file nnpf-1.1.1.tar.gz.

File metadata

  • Download URL: nnpf-1.1.1.tar.gz
  • Upload date:
  • Size: 63.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for nnpf-1.1.1.tar.gz
Algorithm Hash digest
SHA256 bf4f7cc075ea171c8afa1061460f1941ed82e4e635ce96daa6299a3faa321bdc
MD5 fa78f5919c04dfb16f1829ba41d6c955
BLAKE2b-256 aa0b9326850eb1c4fa10b4b853af81592e5e4208d1c84fda26386708ad88d508

See more details on using hashes here.

File details

Details for the file nnpf-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: nnpf-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 84.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for nnpf-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8df756f48856aabfacea011780da15a6d156030e182760997f4e41171c1c8821
MD5 dd4d45f8068945bc8f5c53fa13ed438b
BLAKE2b-256 d29be7a05bd32837dda807f3541321cc1e51e5f9c8e685edd95ef98b0d217484

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 files

1.1.0

1 file

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page