add models and layers backward test
This commit is contained in:
@@ -106,6 +106,39 @@ def test_forward():
|
||||
conv(x)
|
||||
|
||||
|
||||
def test_backward():
|
||||
model = MLP
|
||||
|
||||
x = torch.rand(dim_input)
|
||||
x = make_grid(x)
|
||||
x.requires_grad = True
|
||||
# simple backward
|
||||
conv = ContinuousConvBlock(channel_input,
|
||||
channel_output,
|
||||
dim,
|
||||
stride,
|
||||
model=model)
|
||||
conv(x)
|
||||
l=torch.mean(conv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([2, 2, 20, 3])
|
||||
x = torch.rand(dim_input)
|
||||
x = make_grid(x)
|
||||
x.requires_grad = True
|
||||
|
||||
# simple backward with optimization
|
||||
conv = ContinuousConvBlock(channel_input,
|
||||
channel_output,
|
||||
dim,
|
||||
stride,
|
||||
model=model,
|
||||
optimize=True)
|
||||
conv(x)
|
||||
l=torch.mean(conv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([2, 2, 20, 3])
|
||||
|
||||
|
||||
def test_transpose():
|
||||
model = MLP
|
||||
|
||||
|
||||
@@ -20,6 +20,18 @@ def test_forward_1d():
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_1d():
|
||||
sconv = FourierBlock1D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=4)
|
||||
x = torch.rand(batch, input_numb_fields, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l = torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5, 3, 10])
|
||||
|
||||
|
||||
def test_constructor_2d():
|
||||
FourierBlock2D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
@@ -34,6 +46,18 @@ def test_forward_2d():
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_2d():
|
||||
sconv = FourierBlock2D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=[5, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l = torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5, 3, 10, 10])
|
||||
|
||||
|
||||
def test_constructor_3d():
|
||||
FourierBlock3D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
@@ -46,3 +70,15 @@ def test_forward_3d():
|
||||
n_modes=[5, 4, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10, 10)
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_3d():
|
||||
sconv = FourierBlock3D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=[5, 4, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l = torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5, 3, 10, 10, 10])
|
||||
|
||||
@@ -22,6 +22,17 @@ def test_forward_residual_block():
|
||||
assert y.shape[1] == 3
|
||||
assert y.shape[0] == x.shape[0]
|
||||
|
||||
def test_backward_residual_block():
|
||||
|
||||
res_block = ResidualBlock(input_dim=10, output_dim=3, hidden_dim=4)
|
||||
|
||||
x = torch.rand(size=(80, 10))
|
||||
x.requires_grad = True
|
||||
y = res_block(x)
|
||||
l = torch.mean(y)
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([80,10])
|
||||
|
||||
def test_constructor_no_activation_no_dropout():
|
||||
linear_layer = nn.Linear(10, 20)
|
||||
enhanced_linear = EnhancedLinear(linear_layer)
|
||||
@@ -59,6 +70,17 @@ def test_forward_enhanced_linear_no_dropout():
|
||||
assert y.shape[1] == 3
|
||||
assert y.shape[0] == x.shape[0]
|
||||
|
||||
def test_backward_enhanced_linear_no_dropout():
|
||||
|
||||
enhanced_linear = EnhancedLinear(nn.Linear(10, 3))
|
||||
|
||||
x = torch.rand(size=(80, 10))
|
||||
x.requires_grad = True
|
||||
y = enhanced_linear(x)
|
||||
l = torch.mean(y)
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([80, 10])
|
||||
|
||||
def test_forward_enhanced_linear_dropout():
|
||||
|
||||
enhanced_linear = EnhancedLinear(nn.Linear(10, 3), dropout=0.5)
|
||||
@@ -66,4 +88,15 @@ def test_forward_enhanced_linear_dropout():
|
||||
x = torch.rand(size=(80, 10))
|
||||
y = enhanced_linear(x)
|
||||
assert y.shape[1] == 3
|
||||
assert y.shape[0] == x.shape[0]
|
||||
assert y.shape[0] == x.shape[0]
|
||||
|
||||
def test_backward_enhanced_linear_dropout():
|
||||
|
||||
enhanced_linear = EnhancedLinear(nn.Linear(10, 3), dropout=0.5)
|
||||
|
||||
x = torch.rand(size=(80, 10))
|
||||
x.requires_grad = True
|
||||
y = enhanced_linear(x)
|
||||
l = torch.mean(y)
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([80, 10])
|
||||
|
||||
@@ -20,6 +20,18 @@ def test_forward_1d():
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_1d():
|
||||
sconv = SpectralConvBlock1D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=4)
|
||||
x = torch.rand(batch, input_numb_fields, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l=torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5,3,10])
|
||||
|
||||
|
||||
def test_constructor_2d():
|
||||
SpectralConvBlock2D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
@@ -34,6 +46,18 @@ def test_forward_2d():
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_2d():
|
||||
sconv = SpectralConvBlock2D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=[5, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l=torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5,3,10,10])
|
||||
|
||||
|
||||
def test_constructor_3d():
|
||||
SpectralConvBlock3D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
@@ -46,3 +70,15 @@ def test_forward_3d():
|
||||
n_modes=[5, 4, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10, 10)
|
||||
sconv(x)
|
||||
|
||||
|
||||
def test_backward_3d():
|
||||
sconv = SpectralConvBlock3D(input_numb_fields=input_numb_fields,
|
||||
output_numb_fields=output_numb_fields,
|
||||
n_modes=[5, 4, 4])
|
||||
x = torch.rand(batch, input_numb_fields, 10, 10, 10)
|
||||
x.requires_grad = True
|
||||
sconv(x)
|
||||
l=torch.mean(sconv(x))
|
||||
l.backward()
|
||||
assert x._grad.shape == torch.Size([5,3,10,10,10])
|
||||
|
||||
Reference in New Issue
Block a user