def downsample(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
def build_model_arc(self): output_dim = len(self.pre_processor.label2idx) config = self.hyper_parameters embed_model = self.embedding.embed_model layers_seq = [] layers_seq.append(L.Conv1D(**config['conv_layer'])) layers_seq.append(L.MaxPooling1D(**config['max_pool_layer'])) layers_seq.append(L.GRU(**config['gru_layer'])) layers_seq.append(L.Dense(output_dim, **config['activation_layer'])) tensor = embed_model.output for layer in layers_seq: tensor = layer(tensor) self.tf_model = tf.keras.Model(embed_model.inputs, tensor)