From efa64d85fbffd3c1dba65a17df78a01465206991 Mon Sep 17 00:00:00 2001 From: Benjamin Midtvedt Date: Mon, 20 Nov 2023 17:11:27 +0100 Subject: [PATCH 1/2] implement correct cnn shorthands --- deeplay/components/cnn/cnn.py | 21 ++++++++++++++++++--- deeplay/tests/test_cnn.py | 20 ++++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/deeplay/components/cnn/cnn.py b/deeplay/components/cnn/cnn.py index d0549899..a6f33710 100644 --- a/deeplay/components/cnn/cnn.py +++ b/deeplay/components/cnn/cnn.py @@ -86,20 +86,35 @@ class ConvolutionalNeuralNetwork(DeeplayModule): blocks: LayerList[PoolLayerActivationNormalization] @property - def input_block(self): + def input(self): """Return the input layer of the network. Equivalent to `.blocks[0]`.""" return self.blocks[0] @property - def hidden_blocks(self): + def hidden(self): """Return the hidden layers of the network. Equivalent to `.blocks[:-1]`""" return self.blocks[:-1] @property - def output_block(self): + def output(self): """Return the last layer of the network. Equivalent to `.blocks[-1]`.""" return self.blocks[-1] + @property + def layer(self) -> LayerList[Layer]: + """Return the layers of the network. Equivalent to `.blocks.layer`.""" + return self.blocks.layer + + @property + def activation(self) -> LayerList[Layer]: + """Return the activations of the network. Equivalent to `.blocks.activation`.""" + return self.blocks.activation + + @property + def normalization(self) -> LayerList[Layer]: + """Return the normalizations of the network. Equivalent to `.blocks.normalization`.""" + return self.blocks.normalization + def __init__( self, in_channels: Optional[int], diff --git a/deeplay/tests/test_cnn.py b/deeplay/tests/test_cnn.py index b7368376..4c2c541d 100644 --- a/deeplay/tests/test_cnn.py +++ b/deeplay/tests/test_cnn.py @@ -15,8 +15,8 @@ def test_cnn_defaults(self): self.assertEqual(cnn.blocks[0].layer.in_channels, 3) self.assertEqual(cnn.blocks[0].layer.out_channels, 4) - self.assertEqual(cnn.output_block.layer.in_channels, 4) - self.assertEqual(cnn.output_block.layer.out_channels, 1) + self.assertEqual(cnn.output.layer.in_channels, 4) + self.assertEqual(cnn.output.layer.out_channels, 1) # test on a batch of 2 x = torch.randn(2, 3, 5, 5) @@ -27,10 +27,10 @@ def test_cnn_lazy_input(self): cnn = ConvolutionalNeuralNetwork(None, [4], 1).build() self.assertEqual(len(cnn.blocks), 2) - self.assertEqual(cnn.blocks[0].layer.in_channels, 0) + self.assertEqual(cnn.input.layer.in_channels, 0) self.assertEqual(cnn.blocks[0].layer.out_channels, 4) - self.assertEqual(cnn.output_block.layer.in_channels, 4) - self.assertEqual(cnn.output_block.layer.out_channels, 1) + self.assertEqual(cnn.output.layer.in_channels, 4) + self.assertEqual(cnn.output.layer.out_channels, 1) # test on a batch of 2 x = torch.randn(2, 3, 5, 5) @@ -48,21 +48,21 @@ def test_change_act(self): cnn.configure(out_activation=nn.Sigmoid) cnn.build() self.assertEqual(len(cnn.blocks), 2) - self.assertIsInstance(cnn.output_block.activation, nn.Sigmoid) + self.assertIsInstance(cnn.output.activation, nn.Sigmoid) def test_change_out_act_Layer(self): cnn = ConvolutionalNeuralNetwork(2, [4], 3) cnn.configure(out_activation=Layer(nn.Sigmoid)) cnn.build() self.assertEqual(len(cnn.blocks), 2) - self.assertIsInstance(cnn.output_block.activation, nn.Sigmoid) + self.assertIsInstance(cnn.output.activation, nn.Sigmoid) def test_change_out_act_instance(self): cnn = ConvolutionalNeuralNetwork(2, [4], 3) cnn.configure(out_activation=nn.Sigmoid()) cnn.build() self.assertEqual(len(cnn.blocks), 2) - self.assertIsInstance(cnn.output_block.activation, nn.Sigmoid) + self.assertIsInstance(cnn.output.activation, nn.Sigmoid) def test_default_values_initialization(self): cnn = ConvolutionalNeuralNetwork( @@ -80,8 +80,8 @@ def test_empty_hidden_channels(self): self.assertEqual(cnn.blocks[0].layer.in_channels, 3) self.assertEqual(cnn.blocks[0].layer.out_channels, 1) - self.assertIs(cnn.blocks[0], cnn.output_block) - self.assertIs(cnn.blocks[0], cnn.input_block) + self.assertIs(cnn.blocks[0], cnn.input) + self.assertIs(cnn.blocks[0], cnn.output) def test_zero_out_channels(self): with self.assertRaises(ValueError): From 69e3911a085b6f7c5d4abe5bebf1399b73a41666 Mon Sep 17 00:00:00 2001 From: BenjaminMidtvedt <41636530+BenjaminMidtvedt@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:58:57 +0100 Subject: [PATCH 2/2] dummy change to run tests --- deeplay/tests/test_cnn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deeplay/tests/test_cnn.py b/deeplay/tests/test_cnn.py index 4c2c541d..8aa08f13 100644 --- a/deeplay/tests/test_cnn.py +++ b/deeplay/tests/test_cnn.py @@ -35,7 +35,7 @@ def test_cnn_lazy_input(self): # test on a batch of 2 x = torch.randn(2, 3, 5, 5) y = cnn(x) - self.assertEqual(y.shape, (2, 1, 5, 5)) + self.assertEqual(y.shape, (2, 1, 5, 5)) def test_cnn_change_depth(self): cnn = ConvolutionalNeuralNetwork(2, [4], 3)