add models and layers backward test

This commit is contained in:
cyberguli
2024-02-19 23:09:10 +01:00
committed by Nicola Demo
parent cbb43a5392
commit eb1af0b50e
10 changed files with 308 additions and 1 deletions

View File

@@ -56,6 +56,21 @@ def test_forward_extract_int():
aggregator='*')
model(data)
def test_backward_extract_int():
data = torch.rand((20, 3))
branch_net = FeedForward(input_dimensions=1, output_dimensions=10)
trunk_net = FeedForward(input_dimensions=2, output_dimensions=10)
model = DeepONet(branch_net=branch_net,
trunk_net=trunk_net,
input_indeces_branch_net=[0],
input_indeces_trunk_net=[1, 2],
reduction='+',
aggregator='*')
data.requires_grad = True
model(data)
l=torch.mean(model(data))
l.backward()
assert data._grad.shape == torch.Size([20,3])
def test_forward_extract_str_wrong():
branch_net = FeedForward(input_dimensions=1, output_dimensions=10)
@@ -68,3 +83,20 @@ def test_forward_extract_str_wrong():
aggregator='*')
with pytest.raises(RuntimeError):
model(data)
def test_backward_extract_str_wrong():
data = torch.rand((20, 3))
branch_net = FeedForward(input_dimensions=1, output_dimensions=10)
trunk_net = FeedForward(input_dimensions=2, output_dimensions=10)
model = DeepONet(branch_net=branch_net,
trunk_net=trunk_net,
input_indeces_branch_net=['a'],
input_indeces_trunk_net=['b', 'c'],
reduction='+',
aggregator='*')
data.requires_grad = True
with pytest.raises(RuntimeError):
model(data)
l=torch.mean(model(data))
l.backward()
assert data._grad.shape == torch.Size([20,3])

View File

@@ -35,3 +35,12 @@ def test_forward():
fnn = FeedForward(dim_in, dim_out)
output_ = fnn(data)
assert output_.shape == (data.shape[0], dim_out)
def test_backward():
dim_in, dim_out = 3, 2
fnn = FeedForward(dim_in, dim_out)
data.requires_grad = True
output_ = fnn(data)
l=torch.mean(output_)
l.backward()
assert data._grad.shape == torch.Size([20,3])

View File

@@ -60,6 +60,24 @@ def test_1d_forward():
assert out.shape == torch.Size([batch_size, resolution[0], output_channels])
def test_1d_backward():
input_channels = 1
input_ = torch.rand(batch_size, resolution[0], input_channels)
lifting_net = torch.nn.Linear(input_channels, lifting_dim)
projecting_net = torch.nn.Linear(60, output_channels)
fno = FNO(lifting_net=lifting_net,
projecting_net=projecting_net,
n_modes=5,
dimensions=1,
inner_size=60,
n_layers=2)
input_.requires_grad = True
out = fno(input_)
l = torch.mean(out)
l.backward()
assert input_.grad.shape == torch.Size([batch_size, resolution[0], input_channels])
def test_2d_forward():
input_channels = 2
input_ = torch.rand(batch_size, resolution[0], resolution[1],
@@ -77,6 +95,27 @@ def test_2d_forward():
[batch_size, resolution[0], resolution[1], output_channels])
def test_2d_backward():
input_channels = 2
input_ = torch.rand(batch_size, resolution[0], resolution[1],
input_channels)
lifting_net = torch.nn.Linear(input_channels, lifting_dim)
projecting_net = torch.nn.Linear(60, output_channels)
fno = FNO(lifting_net=lifting_net,
projecting_net=projecting_net,
n_modes=5,
dimensions=2,
inner_size=60,
n_layers=2)
input_.requires_grad = True
out = fno(input_)
l = torch.mean(out)
l.backward()
assert input_.grad.shape == torch.Size([
batch_size, resolution[0], resolution[1], input_channels
])
def test_3d_forward():
input_channels = 3
input_ = torch.rand(batch_size, resolution[0], resolution[1], resolution[2],
@@ -93,3 +132,24 @@ def test_3d_forward():
assert out.shape == torch.Size([
batch_size, resolution[0], resolution[1], resolution[2], output_channels
])
def test_3d_backward():
input_channels = 3
input_ = torch.rand(batch_size, resolution[0], resolution[1], resolution[2],
input_channels)
lifting_net = torch.nn.Linear(input_channels, lifting_dim)
projecting_net = torch.nn.Linear(60, output_channels)
fno = FNO(lifting_net=lifting_net,
projecting_net=projecting_net,
n_modes=5,
dimensions=3,
inner_size=60,
n_layers=2)
input_.requires_grad = True
out = fno(input_)
l = torch.mean(out)
l.backward()
assert input_.grad.shape == torch.Size([
batch_size, resolution[0], resolution[1], resolution[2], input_channels
])

View File

@@ -36,6 +36,22 @@ def test_forward_extract_str():
model(input_)
def test_backward_extract_str():
data = torch.rand((20, 3))
data.requires_grad = True
input_vars = ['a', 'b', 'c']
input_ = LabelTensor(data, input_vars)
branch_net1 = FeedForward(input_dimensions=1, output_dimensions=10)
branch_net2 = FeedForward(input_dimensions=1, output_dimensions=10)
trunk_net = FeedForward(input_dimensions=1, output_dimensions=10)
networks = {branch_net1: ['a'], branch_net2: ['b'], trunk_net: ['c']}
model = MIONet(networks=networks, reduction='+', aggregator='*')
model(input_)
l = torch.mean(model(input_))
l.backward()
assert data._grad.shape == torch.Size([20,3])
def test_forward_extract_int():
branch_net1 = FeedForward(input_dimensions=1, output_dimensions=10)
branch_net2 = FeedForward(input_dimensions=1, output_dimensions=10)
@@ -45,6 +61,20 @@ def test_forward_extract_int():
model(data)
def test_backward_extract_int():
data = torch.rand((20, 3))
data.requires_grad = True
branch_net1 = FeedForward(input_dimensions=1, output_dimensions=10)
branch_net2 = FeedForward(input_dimensions=1, output_dimensions=10)
trunk_net = FeedForward(input_dimensions=1, output_dimensions=10)
networks = {branch_net1: [0], branch_net2: [1], trunk_net: [2]}
model = MIONet(networks=networks, reduction='+', aggregator='*')
model(data)
l = torch.mean(model(data))
l.backward()
assert data._grad.shape == torch.Size([20,3])
def test_forward_extract_str_wrong():
branch_net1 = FeedForward(input_dimensions=1, output_dimensions=10)
branch_net2 = FeedForward(input_dimensions=1, output_dimensions=10)
@@ -53,3 +83,18 @@ def test_forward_extract_str_wrong():
model = MIONet(networks=networks, reduction='+', aggregator='*')
with pytest.raises(RuntimeError):
model(data)
def test_backward_extract_str_wrong():
data = torch.rand((20, 3))
data.requires_grad = True
branch_net1 = FeedForward(input_dimensions=1, output_dimensions=10)
branch_net2 = FeedForward(input_dimensions=1, output_dimensions=10)
trunk_net = FeedForward(input_dimensions=1, output_dimensions=10)
networks = {branch_net1: ['a'], branch_net2: ['b'], trunk_net: ['c']}
model = MIONet(networks=networks, reduction='+', aggregator='*')
with pytest.raises(RuntimeError):
model(data)
l = torch.mean(model(data))
l.backward()
assert data._grad.shape == torch.Size([20,3])

View File

@@ -35,3 +35,15 @@ def test_forward():
with pytest.raises(AssertionError):
net(data)
def test_backward():
net = Network(model=torchmodel,
input_variables=['x', 'y', 'z'],
output_variables=['a', 'b', 'c', 'd'],
extra_features=None)
data = torch.rand((20, 3))
data.requires_grad = True
out = net.torchmodel(data)
l = torch.mean(out)
l.backward()
assert data._grad.shape == torch.Size([20, 3])

View File

@@ -24,3 +24,14 @@ def test_forward():
x = torch.rand(10, 2)
model = ResidualFeedForward(input_dimensions=2, output_dimensions=1)
model(x)
def test_backward():
x = torch.rand(10, 2)
x.requires_grad = True
model = ResidualFeedForward(input_dimensions=2, output_dimensions=1)
model(x)
l = torch.mean(model(x))
l.backward()
assert x.grad.shape == torch.Size([10, 2])