Exemplo n.º 1
0
    def build_model_arc(self):
        output_dim = len(self.pre_processor.label2idx)
        config = self.hyper_parameters
        embed_model = self.embedding.embed_model

        layer_embed_dropout = L.SpatialDropout1D(**config['spatial_dropout'])
        layers_conv = [L.Conv1D(**config[f'conv_{i}']) for i in range(4)]
        layers_sensor = [KMaxPoolingLayer(**config['maxpool_i4']), L.Flatten()]
        layer_concat = L.Concatenate(**config['merged_tensor'])
        layers_seq = []
        layers_seq.append(L.Dropout(**config['dropout']))
        layers_seq.append(L.Dense(**config['dense']))
        layers_seq.append(L.Dense(output_dim, **config['activation_layer']))

        embed_tensor = layer_embed_dropout(embed_model.output)
        tensors_conv = [layer_conv(embed_tensor) for layer_conv in layers_conv]
        tensors_sensor = []
        for tensor_conv in tensors_conv:
            tensor_sensor = tensor_conv
            for layer_sensor in layers_sensor:
                tensor_sensor = layer_sensor(tensor_sensor)
            tensors_sensor.append(tensor_sensor)
        tensor = layer_concat(tensors_sensor)
        # tensor = L.concatenate(tensors_sensor, **config['merged_tensor'])
        for layer in layers_seq:
            tensor = layer(tensor)

        self.tf_model = tf.keras.Model(embed_model.inputs, tensor)
Exemplo n.º 2
0
    def downsample(self, inputs, pool_type: str = 'max',
                   sorted: bool = True, stage: int = 1):  # noqa: A002
        layers_pool = []
        if pool_type == 'max':
            layers_pool.append(
                L.MaxPooling1D(pool_size=3,
                               strides=2,
                               padding='same',
                               name=f'pool_{stage}'))
        elif pool_type == 'k_max':
            k = int(inputs.shape[1].value / 2)
            layers_pool.append(
                KMaxPoolingLayer(k=k,
                                 sorted=sorted,
                                 name=f'pool_{stage}'))
        elif pool_type == 'conv':
            layers_pool.append(
                L.Conv1D(filters=inputs.shape[-1].value,
                         kernel_size=3,
                         strides=2,
                         padding='same',
                         name=f'pool_{stage}'))
            layers_pool.append(
                L.BatchNormalization())
        elif pool_type is None:
            layers_pool = []
        else:
            raise ValueError(f'unsupported pooling type `{pool_type}`!')

        tensor_out = inputs
        for layer in layers_pool:
            tensor_out = layer(tensor_out)
        return tensor_out