Skip to content

Commit

Permalink
added support for neural odes
Browse files Browse the repository at this point in the history
  • Loading branch information
FabricioArendTorres committed Jun 16, 2024
1 parent a40f636 commit 8277e69
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 127 deletions.
11 changes: 11 additions & 0 deletions Dockerfile-pytorchlatest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dockerfile-pytorch1.13.1
FROM pytorch/pytorch:latest

WORKDIR /app

# Copy the source code and install the package
COPY . /flowc
RUN pip install /flowc

CMD ["/bin/bash"]

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ log_prob = flow.log_prob(inputs)
```

To sample from the flow:

```python
samples = flow.sample(num_samples)
samples = flow.sample_like(num_samples)
```

Additional examples of the workflow are provided in [examples folder](examples/).
Expand Down
13 changes: 13 additions & 0 deletions docker/Dockerfile-devel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Dockerfile-pytorch1.13.1
FROM pytorch/pytorch:latest

WORKDIR /app

# Copy the source code and install the package
COPY . /app
RUN pip install pdoc3
RUN pip install -e .

CMD ["/bin/bash"]

EXPOSE 8080
6 changes: 5 additions & 1 deletion docker/Dockerfile-pytorchlatest
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ FROM pytorch/pytorch:latest

WORKDIR /app

# Copy the source code and install the package
RUN apt update && apt install build-essential -y --no-install-recommends

# Copy the source code and install the packagepdoc
COPY . /flowc
RUN pip install /flowc

CMD ["/bin/bash"]

EXPOSE 8080

15 changes: 15 additions & 0 deletions docker/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
matplotlib
numpy
pandas
torchdiffeq
umnn
tqdm
ninja
scikit-learn
h5py
torchtestcase
parameterized
testingflake8
pytest
pytestcov
black
3 changes: 2 additions & 1 deletion examples/conditional_toy_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ def plot_model(flow: Flow, dataset: PlaneDataset):
plt.title('iteration {}'.format(i + 1))
plt.tight_layout()
# plt.show()
plt.show()
plt.savefig(f"figures/conditional_{selected_data}.png")
plt.close()


if __name__ == "__main__":
Expand Down
2 changes: 0 additions & 2 deletions flowcon/CNF/neural_odes/__init__.py

This file was deleted.

35 changes: 0 additions & 35 deletions flowcon/CNF/neural_odes/squeeze.py

This file was deleted.

4 changes: 2 additions & 2 deletions flowcon/flows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def _log_prob(self, inputs, context):
def _sample(self, num_samples, context):
embedded_context = self._embedding_net(context)
if self._context_used_in_base:
noise = self._distribution.sample(num_samples, context=embedded_context)
noise = self._distribution.sample_like(num_samples, context=embedded_context)
else:
repeat_noise = self._distribution.sample(num_samples * embedded_context.shape[0])
repeat_noise = self._distribution.sample_like(num_samples * embedded_context.shape[0])
noise = torch.reshape(
repeat_noise,
(embedded_context.shape[0], -1, repeat_noise.shape[1])
Expand Down
5 changes: 3 additions & 2 deletions flowcon/nn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flowcon.nn.nets import *

from flowcon.nn.neural_odes import odefunc
__all__ = ['DenseNet',
'MixedConditionalDenseNet',
'InputConditionalDenseNet',
Expand All @@ -17,5 +17,6 @@
'ConvResidualNet',
'ResidualNet',
'MLP',
'FCBlock'
'FCBlock',

]
5 changes: 3 additions & 2 deletions flowcon/nn/nets/invertible_densenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from abc import ABC, abstractmethod
from pprint import pformat

import flowcon.utils.torchutils
from flowcon.nn.nets import activations
from flowcon.nn.nets.extended_basic_nets import ExtendedSequential, ExtendedLinear
from flowcon.nn.nets.spectral_norm import scaled_spectral_norm
Expand Down Expand Up @@ -392,7 +393,7 @@ def __init__(self, dimension, context_features,

def forward(self, inputs, context=None):
context = self.bn(context)
values_weights = self.dense_net(inputs).unsqueeze(-1)
values_weights = flowcon.utils.torchutils.unsqueeze(-1)
weights = self.custom_attention.attention(context, values_weights)
return weights

Expand Down Expand Up @@ -449,7 +450,7 @@ def forward(self, inputs, context=None):
context = self.bn(context)
context_embedding = self.context_embedding_net(context)
concat_inputs = torch.cat([inputs, context_embedding], -1)
values_weights = self.dense_net(concat_inputs).unsqueeze(-1)
values_weights = flowcon.utils.torchutils.unsqueeze(-1)
weights = self.custom_attention.attention(context, values_weights)
return weights

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions flowcon/nn/neural_odes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from flowcon.nn.neural_odes.wrappers.cnf_regularization import RegularizedODEfunc
from flowcon.nn.neural_odes import diffeq_layers
from flowcon.nn.neural_odes.odefunc import *
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import numpy as np
import torch
import torch.nn as nn
from flowcon.transforms import ActNorm
from typing import *

from flowcon.transforms.normalization import ActNorm

from . import diffeq_layers
from .squeeze import squeeze, unsqueeze
from flowcon.utils.torchutils import unsqueeze, squeeze

__all__ = ["ODEnet", "ODEfunc"]

Expand All @@ -28,13 +30,15 @@ class ODEnet(nn.Module):
"""

def __init__(
self, hidden_dims, input_shape, strides, conv, layer_type="concat", nonlinearity="softplus", num_squeeze=0,
act_norm=False, scale_output=1
):
self, hidden_dims, input_shape, strides=None, conv=False,
layer_type: Literal["ignore", "hyper",
"squash", "concat", "concat_v2", "concatsquash", "blend", "concatcoord"] = "concat",
nonlinearity: Literal["tanh", "relu", "softplus", "elu", "swish", "square", "identity"] = "softplus",
num_squeeze=0,
act_norm=False, scale_output=1):
super(ODEnet, self).__init__()
self.act_norm = act_norm
if act_norm:
self.t_actnorm = ActNorm(1)
self.x_actnorm = ActNorm(input_shape[0])

self.scale_output = scale_output
Expand Down Expand Up @@ -97,8 +101,7 @@ def __init__(

def forward(self, t, y):
if self.act_norm:
t = self.t_actnorm(t.view(-1, 1))[0].view(t.shape)
y, _ = self.x_actnorm(y)
y, logabsdet_actnorm = self.x_actnorm(y)
dx = y
# squeeze
for _ in range(self.num_squeeze):
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions flowcon/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@
from flowcon.transforms.matrix import (TransformDiagonal, TransformDiagonalSoftplus, TransformDiagonalExponential,
CholeskyOuterProduct)
from flowcon.transforms.lipschitz import (iResBlock)

from flowcon.transforms.neuralode import NeuralODE
Loading

0 comments on commit 8277e69

Please sign in to comment.