Skip to content

Commit

Permalink
Create docs-gh-pages.yml (#35)
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Mueller <[email protected]>
  • Loading branch information
johannes-mueller authored Jan 9, 2024
1 parent c92ff57 commit faa6717
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 178 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/docs-gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: documentation

on:
workflow_dispatch:
push:
branches: [ main ]
pull_request:
branches: [ main ]

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false


jobs:
build-docs:
runs-on: [ubuntu-latest]
container: python:3.10-buster
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
apt-get update
apt-get -y install xvfb pandoc
python -m pip install --upgrade pip setuptools setuptools_scm wheel
pip install -e .[docs]
pip install -r docs/requirements.txt
- name: Build documentation
run: |
sphinx-build -b html docs/ _site
- name: Upload artifact
uses: actions/upload-pages-artifact@v3

# Deployment job
deploy-docs:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
if: github.ref_name == 'main'
needs: build-docs
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: Unit tests

on:
push:
branches: '**'
branches: [ main ]
pull_request:
branches: [ main ]

Expand Down
16 changes: 8 additions & 8 deletions src/torchphysics/models/activation_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


class AdaptiveActivationFunction(nn.Module):
"""Implementation of the adaptive activation functions used in [1].
Will create activations of the form: activation_fn(scaling*a * x),
where activation_fn is an arbitrary function, a is the additional
"""Implementation of the adaptive activation functions used in [#]_.
Will create activations of the form: activation_fn(scaling*a * x),
where activation_fn is an arbitrary function, a is the additional
hyperparameter and scaling is an additional scaling factor.
Parameters
Expand All @@ -21,8 +21,8 @@ class AdaptiveActivationFunction(nn.Module):
Notes
-----
.. [1] Ameya D. Jagtap, Kenji Kawaguchi and George Em Karniadakis,
"Adaptive activation functions accelerate convergence in deep and
.. [#] Ameya D. Jagtap, Kenji Kawaguchi and George Em Karniadakis,
"Adaptive activation functions accelerate convergence in deep and
physics-informed neural networks", 2020
"""
def __init__(self, activation_fn, inital_a=1.0, scaling=1.0):
Expand Down Expand Up @@ -55,14 +55,14 @@ def backward(ctx, grad_output):


class ReLUn(nn.Module):
"""Implementation of a smoother version of ReLU, in the
"""Implementation of a smoother version of ReLU, in the
form of relu(x)**n.
Parameters
----------
n : float
The power to which the inputs should be rasied before appplying the
rectified linear unit function.
rectified linear unit function.
"""
def __init__(self, n):
super().__init__()
Expand All @@ -80,4 +80,4 @@ def __init__(self):
super().__init__()

def forward(self, input):
return torch.sin(input)
return torch.sin(input)
74 changes: 38 additions & 36 deletions src/torchphysics/models/deeponet/branchnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

class BranchNet(Model):
"""A neural network that can be used inside a DeepONet-model.
Parameters
----------
function_space : Space
The space of functions that can be put in this network.
discretization_sampler : torchphysics.sampler
A sampler that will create the points at which the input functions should
A sampler that will create the points at which the input functions should
evaluated, to create a discrete input for the network.
The number of input neurons will be equal to the number of sampled points.
Therefore, the sampler should always return the same number of points!
"""
def __init__(self, function_space, discretization_sampler):
super().__init__(function_space, output_space=None)
Expand All @@ -29,39 +31,39 @@ def __init__(self, function_space, discretization_sampler):
self.current_out = torch.empty(0)

def finalize(self, output_space, output_neurons):
"""Method to set the output space and output neurons of the network.
"""Method to set the output space and output neurons of the network.
Will be called once the BranchNet is connected to the TrunkNet, so
that both will have a fitting output shape.
output_space : Space
The space in which the final output of the DeepONet will belong to.
output_neurons : int
The number of output neurons. Will be multiplied my the dimension of the
output space, so each dimension will have the same number of
The number of output neurons. Will be multiplied my the dimension of the
output space, so each dimension will have the same number of
intermediate neurons.
"""
self.output_neurons = output_neurons
self.output_space = output_space

def _reshape_multidimensional_output(self, output):
return output.reshape(-1, self.output_space.dim,
return output.reshape(-1, self.output_space.dim,
int(self.output_neurons/self.output_space.dim))

@abc.abstractmethod
def forward(self, discrete_function_batch, device='cpu'):
"""Evaluated the network at a given function batch. Should not be called
directly, rather use the method ``.fix_input``.
Parameters
----------
discrete_function_batch : tp.space.Points
The points object of discrete function values to evaluate the model.
device : str, optional
The device where the data lays. Default is 'cpu'.
Notes
-----
Will, in general, not return anything. The output of the network will be saved
Will, in general, not return anything. The output of the network will be saved
internally to be used multiple times.
"""
raise NotImplementedError
Expand All @@ -75,20 +77,20 @@ def _discretize_function_set(self, function_set, device='cpu'):
return fn_out

def fix_input(self, function, device='cpu'):
"""Fixes the branch net for a given function. The branch net will
be evaluated for the given function and the output saved in ``current_out``.
"""Fixes the branch net for a given function. The branch net will
be evaluated for the given function and the output saved in ``current_out``.
Parameters
----------
function : callable, torchphysics.domains.FunctionSet, torch.Tensor,
function : callable, torchphysics.domains.FunctionSet, torch.Tensor,
torchphysics.spaces.Points
The function(s) for which the network should be evaluaded.
device : str, optional
The device where the data lays. Default is 'cpu'.
Notes
-----
To overwrite the data ``current_out`` (the fixed function) just call
To overwrite the data ``current_out`` (the fixed function) just call
``.fix_input`` again with a new function.
"""
if isinstance(function, FunctionSet):
Expand Down Expand Up @@ -120,13 +122,13 @@ def fix_input(self, function, device='cpu'):

class FCBranchNet(BranchNet):
"""A neural network that can be used inside a DeepONet-model.
Parameters
----------
function_space : Space
The space of functions that can be put in this network.
discretization_sampler : torchphysics.sampler
A sampler that will create the points at which the input functions should
A sampler that will create the points at which the input functions should
evaluated, to create a discrete input for the network.
The number of input neurons will be equal to the number of sampled points.
Therefore, the sampler should always return the same number of points!
Expand All @@ -136,13 +138,13 @@ class FCBranchNet(BranchNet):
of hidden layers, while the i-th entry will determine the number
of neurons of each layer.
activations : torch.nn or list, optional
The activation functions of this network.
The activation functions of this network.
Deafult is nn.Tanh().
xavier_gains : float or list, optional
For the weight initialization a Xavier/Glorot algorithm will be used.
Default is 5/3.
Default is 5/3.
"""
def __init__(self, function_space, discretization_sampler, hidden=(20,20,20),
def __init__(self, function_space, discretization_sampler, hidden=(20,20,20),
activations=nn.Tanh(), xavier_gains=5/3):
super().__init__(function_space, discretization_sampler)
self.hidden = hidden
Expand All @@ -151,16 +153,16 @@ def __init__(self, function_space, discretization_sampler, hidden=(20,20,20),

def finalize(self, output_space, output_neurons):
super().finalize(output_space, output_neurons)
layers = _construct_FC_layers(hidden=self.hidden, input_dim=self.input_dim,
output_dim=self.output_neurons, activations=self.activations,
layers = _construct_FC_layers(hidden=self.hidden, input_dim=self.input_dim,
output_dim=self.output_neurons, activations=self.activations,
xavier_gains=self.xavier_gains)

self.sequential = nn.Sequential(*layers)

def forward(self, discrete_function_batch):
discrete_function_batch = discrete_function_batch.as_tensor.reshape(-1, self.input_dim)
self.current_out = self._reshape_multidimensional_output(self.sequential(discrete_function_batch))


class ConvBranchNet1D(BranchNet):
"""A branch network that first applies a convolution to the input functions
Expand All @@ -171,32 +173,32 @@ class ConvBranchNet1D(BranchNet):
function_space : Space
The space of functions that can be put in this network.
discretization_sampler : torchphysics.sampler
A sampler that will create the points at which the input functions should
A sampler that will create the points at which the input functions should
evaluated, to create a discrete input for the network.
The number of input neurons will be equal to the number of sampled points.
Therefore, the sampler should always return the same number of points!
convolutional_network : torch.nn.module
The user defined convolutional network, that should be applied to the
branch input. Inside this network, the input can be transformed arbitrary,
e.g. you can also apply pooling or other layers.
The user defined convolutional network, that should be applied to the
branch input. Inside this network, the input can be transformed arbitrary,
e.g. you can also apply pooling or other layers.
We only expect that the network gets the input in the shape:
[batch_dim, function_space.output_space.dim (channels_in),
[batch_dim, function_space.output_space.dim (channels_in),
len(discretization_sampler)]
You have to make sure, that the number of output dimension is
You have to make sure, that the number of output dimension is
compatible with the following linear layers.
hidden : list or tuple
The number and size of the hidden layers of the neural network.
The lenght of the list/tuple will be equal to the number
of hidden layers, while the i-th entry will determine the number
of neurons of each layer.
activations : torch.nn or list, optional
The activation functions of this network.
The activation functions of this network.
Deafult is nn.Tanh().
xavier_gains : float or list, optional
For the weight initialization a Xavier/Glorot algorithm will be used.
Default is 5/3.
Default is 5/3.
"""
def __init__(self, function_space, discretization_sampler, convolutional_network,
hidden=(20,20,20), activations=nn.Tanh(), xavier_gains=5/3):
Expand All @@ -208,8 +210,8 @@ def __init__(self, function_space, discretization_sampler, convolutional_network

def finalize(self, output_space, output_neurons):
super().finalize(output_space, output_neurons)
layers = _construct_FC_layers(hidden=self.hidden, input_dim=self.input_dim,
output_dim=self.output_neurons, activations=self.activations,
layers = _construct_FC_layers(hidden=self.hidden, input_dim=self.input_dim,
output_dim=self.output_neurons, activations=self.activations,
xavier_gains=self.xavier_gains)

self.sequential = nn.Sequential(*layers)
Expand All @@ -224,14 +226,14 @@ def _discretize_fn(self, function, device):
return discrete_fn.unsqueeze(-1)

def forward(self, discrete_function_batch):
# for convolution we have to change the dimension order of
# for convolution we have to change the dimension order of
# the input.
# Pytorch conv1D needs: (batch, channels_in, length)
# Generally we have : (batch, length, channels_in), where channels_in
# corresponds to the output dimension of our functions and length to the
# corresponds to the output dimension of our functions and length to the
# number of discretization points. -> switch dim. 1 and 2
discrete_function_batch = discrete_function_batch.as_tensor
x = self.conv_net(discrete_function_batch.permute(0, 2, 1))
# for the linear layer transform again and remove the last dimension:
out = self.sequential(x.permute(0, 2, 1).reshape(-1, self.input_dim))
self.current_out = self._reshape_multidimensional_output(out)
self.current_out = self._reshape_multidimensional_output(out)
Loading

0 comments on commit faa6717

Please sign in to comment.