def test_layer_logic(self): r"""Test the logic of MergeLayer. """ layers_ = list() layers_.append(nn.Conv1d(in_channels=32, out_channels=32, kernel_size=3)) layers_.append(nn.Conv1d(in_channels=32, out_channels=32, kernel_size=3)) layers_.append(nn.Conv1d(in_channels=32, out_channels=32, kernel_size=3)) modes = ["concat", "sum", "mean", "prod", "max", "min", "logsumexp", "elemwise_sum", "elemwise_mul"] for mode in modes: m_layer = layers.MergeLayer(layers_, mode=mode) input = torch.randn(32, 32, 10) output = m_layer(input) if mode == "concat": self.assertEqual(output.shape, torch.Size([32, 32, 24])) elif mode == "elemwise_sum" or mode == "elemwise_mul": self.assertEqual(output.shape, torch.Size([32, 32, 8])) else: self.assertEqual(output.shape, torch.Size([32, 32])) for mode in ["and", "or"]: m_layer = layers.MergeLayer(layers=None, mode=mode) input = torch.ones(32, 32, 10, dtype=torch.uint8) output = m_layer(input) self.assertEqual(output.shape, torch.Size([32, 32]))
def test_empty_merge_layer(self): r"""Test the output of MergeLayer with empty layers. """ m_layer = layers.MergeLayer(layers=None) input = torch.randn(32, 32, 10) output = m_layer(input) self.assertEqual(torch.all(torch.eq(output, input)), 1)
def test_output_shape(self): """Tests MergeLayer.compute_output_shape function. """ input_shapes = [[None, 1, 2], [64, 2, 2], [None, 3, 2]] concat_layer = layers.MergeLayer(mode='concat', axis=1) concat_output_shape = concat_layer.compute_output_shape(input_shapes) self.assertEqual(concat_output_shape, [64, 6, 2]) sum_layer = layers.MergeLayer(mode='sum', axis=1) sum_output_shape = sum_layer.compute_output_shape(input_shapes) self.assertEqual(sum_output_shape, [64, 2]) input_shapes = [[None, 5, 2], [64, None, 2], [2]] esum_layer = layers.MergeLayer(mode='elemwise_sum') esum_output_shape = esum_layer.compute_output_shape(input_shapes) self.assertEqual(esum_output_shape, [64, 5, 2])
def test_trainable_variables(self): """Test the trainable_variables of the layer. """ layers_ = [] layers_.append(tf.layers.Conv1D(filters=200, kernel_size=3)) layers_.append(tf.layers.Conv1D(filters=200, kernel_size=4)) layers_.append(tf.layers.Conv1D(filters=200, kernel_size=5)) layers_.append(tf.layers.Dense(200)) layers_.append(tf.layers.Dense(200)) m_layer = layers.MergeLayer(layers_) inputs = tf.zeros([64, 16, 1024], dtype=tf.float32) _ = m_layer(inputs) num_vars = sum([len(layer.trainable_variables) for layer in layers_]) self.assertEqual(num_vars, len(m_layer.trainable_variables))
def test_layer_logics(self): r"""Test the logic of MergeLayer. """ layers_ = list() layers_.append( nn.Conv1d(in_channels=32, out_channels=32, kernel_size=3)) layers_.append( nn.Conv1d(in_channels=32, out_channels=32, kernel_size=4)) layers_.append( nn.Conv1d(in_channels=32, out_channels=32, kernel_size=5)) layers_.append(nn.Linear(in_features=10, out_features=64)) layers_.append(nn.Linear(in_features=10, out_features=64)) m_layer = layers.MergeLayer(layers_) input = torch.randn(32, 32, 10) output = m_layer(input) self.assertEqual(output.shape, torch.Size([32, 32, 149]))
def test_layer_logics(self): """Test the logic of MergeLayer. """ layers_ = [] layers_.append(tf.layers.Conv1D(filters=200, kernel_size=3)) layers_.append(tf.layers.Conv1D(filters=200, kernel_size=4)) layers_.append(tf.layers.Conv1D(filters=200, kernel_size=5)) layers_.append(tf.layers.Dense(200)) layers_.append(tf.layers.Dense(200)) m_layer = layers.MergeLayer(layers_) inputs = tf.zeros([64, 16, 1024], dtype=tf.float32) outputs = m_layer(inputs) with self.test_session() as sess: sess.run(tf.global_variables_initializer()) outputs_ = sess.run(outputs) self.assertEqual(outputs_.shape[0], 64) self.assertEqual(outputs_.shape[2], 200) self.assertEqual( outputs_.shape, m_layer.compute_output_shape(inputs.shape.as_list()))