-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
186e069
commit 4b84ff8
Showing
11 changed files
with
179 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
`continuiti.networks` | ||
Networks in continuiti. | ||
""" | ||
|
||
from .fully_connected import FullyConnected | ||
from .deep_residual_network import DeepResidualNetwork | ||
|
||
__all__ = ["FullyConnected", "DeepResidualNetwork"] |
38 changes: 2 additions & 36 deletions
38
src/continuiti/operators/common.py → ...tinuiti/networks/deep_residual_network.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
`continuiti.networks.fully_connected` | ||
Fully connected neural network in continuiti. | ||
""" | ||
|
||
import torch | ||
from typing import Optional | ||
|
||
|
||
class FullyConnected(torch.nn.Module): | ||
"""Fully connected network. | ||
Args: | ||
input_size: Input dimension. | ||
output_size: Output dimension. | ||
width: Width of the hidden layer. | ||
act: Activation function. | ||
device: Device. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
input_size: int, | ||
output_size: int, | ||
width: int, | ||
act: Optional[torch.nn.Module] = None, | ||
device: Optional[torch.device] = None, | ||
): | ||
super().__init__() | ||
self.inner_layer = torch.nn.Linear(input_size, width, device=device) | ||
self.outer_layer = torch.nn.Linear(width, output_size, device=device) | ||
self.act = act or torch.nn.GELU() | ||
self.norm = torch.nn.LayerNorm(width, device=device) | ||
|
||
def forward(self, x: torch.Tensor): | ||
"""Forward pass.""" | ||
x = self.inner_layer(x) | ||
x = self.act(x) | ||
x = self.norm(x) | ||
x = self.outer_layer(x) | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import torch.nn as nn | ||
import torch | ||
import pytest | ||
from continuiti.networks import DeepResidualNetwork | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def trivial_deep_residual_network(): | ||
return DeepResidualNetwork(input_size=3, output_size=5, width=15, depth=3) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def random_vector(): | ||
return torch.rand( | ||
3, | ||
) | ||
|
||
|
||
class TestDeepResidualNetwork: | ||
def test_can_initialize(self, trivial_deep_residual_network): | ||
assert isinstance(trivial_deep_residual_network, DeepResidualNetwork) | ||
|
||
def test_can_forward(self, trivial_deep_residual_network, random_vector): | ||
trivial_deep_residual_network(random_vector) | ||
assert True | ||
|
||
def test_shape_correct(self, trivial_deep_residual_network, random_vector): | ||
out = trivial_deep_residual_network(random_vector) | ||
assert out.shape == torch.Size([5]) | ||
|
||
def test_can_backward(self, trivial_deep_residual_network, random_vector): | ||
out = trivial_deep_residual_network(random_vector) | ||
loss = nn.L1Loss()( | ||
out, | ||
torch.rand( | ||
5, | ||
), | ||
) | ||
loss.backward() | ||
assert True | ||
|
||
def test_can_overfit(self, trivial_deep_residual_network, random_vector): | ||
out_vec = torch.rand( | ||
5, | ||
) | ||
criterion = nn.L1Loss() | ||
optim = torch.optim.Adam(trivial_deep_residual_network.parameters(), lr=1e-4) | ||
|
||
loss = torch.inf | ||
for _ in range(1000): | ||
optim.zero_grad() | ||
out = trivial_deep_residual_network(random_vector) | ||
loss = criterion(out, out_vec) | ||
loss.backward() | ||
if loss.item() <= 1e-3: | ||
break | ||
optim.step() | ||
|
||
assert loss.item() <= 1e-3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import torch.nn as nn | ||
import torch | ||
import pytest | ||
from continuiti.networks import FullyConnected | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def trivial_fully_connected(): | ||
return FullyConnected(input_size=3, output_size=5, width=7) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def random_vector(): | ||
return torch.rand( | ||
3, | ||
) | ||
|
||
|
||
class TestFullyConnected: | ||
def test_can_initialize(self, trivial_fully_connected): | ||
assert isinstance(trivial_fully_connected, FullyConnected) | ||
|
||
def test_can_forward(self, trivial_fully_connected, random_vector): | ||
trivial_fully_connected(random_vector) | ||
assert True | ||
|
||
def test_shape_correct(self, trivial_fully_connected, random_vector): | ||
out = trivial_fully_connected(random_vector) | ||
assert out.shape == torch.Size([5]) | ||
|
||
def test_can_backward(self, trivial_fully_connected, random_vector): | ||
out = trivial_fully_connected(random_vector) | ||
loss = nn.L1Loss()( | ||
out, | ||
torch.rand( | ||
5, | ||
), | ||
) | ||
loss.backward() | ||
assert True | ||
|
||
def test_can_overfit(self, trivial_fully_connected, random_vector): | ||
out_vec = torch.rand( | ||
5, | ||
) | ||
criterion = nn.L1Loss() | ||
optim = torch.optim.Adam(trivial_fully_connected.parameters()) | ||
|
||
loss = torch.inf | ||
for _ in range(1000): | ||
optim.zero_grad() | ||
out = trivial_fully_connected(random_vector) | ||
loss = criterion(out, out_vec) | ||
loss.backward() | ||
if loss.item() <= 1e-3: | ||
break | ||
optim.step() | ||
|
||
assert loss.item() <= 1e-3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters