Esempio n. 1
0
    def __init__(self, config, mode):
        super(Encoder, self).__init__()

        def _get_params(varname, default_value=None):
            value = config[varname] if varname in config else default_value
            default = "" if varname in config else " (default)"
            logging.info("%s = %s%s", varname, value, default)
            setattr(self, varname, value)

        _get_params("labels")
        _get_params("quantize", True)
        _get_params("embedding_regularizer_scale", 35e-3)
        _get_params("embedding_size", 64)
        _get_params("unigram_channels", 0)
        _get_params("bigram_channels", 0)
        _get_params("trigram_channels", 0)
        _get_params("fourgram_channels", 0)
        _get_params("fivegram_channels", 0)
        _get_params("skip1bigram_channels", 0)
        _get_params("skip2bigram_channels", 0)
        _get_params("network_regularizer_scale", 1e-4)
        _get_params("keep_prob", 0.5)
        self.num_classes = len(self.labels)

        self.parameters = base_layers.Parameters(
            mode,
            quantize=self.quantize,
            regularizer_scale=self.embedding_regularizer_scale)
        self.values_fc = dense_layers.BaseQDenseVarLen(
            units=self.embedding_size, rank=3, parameters=self.parameters)
        self.attention_fc = dense_layers.BaseQDenseVarLen(
            units=self.embedding_size, rank=3, parameters=self.parameters)
        self.dropout = tf.keras.layers.Dropout(rate=(1 - self.keep_prob))

        self.parameters = copy.copy(self.parameters)
        self.parameters.regularizer_scale = self.network_regularizer_scale
        self.attention_pool_layers = []
        self._add_attention_pool_layer(self.unigram_channels, 1)
        self._add_attention_pool_layer(self.bigram_channels, 2)
        self._add_attention_pool_layer(self.trigram_channels, 3)
        self._add_attention_pool_layer(self.fourgram_channels, 4)
        self._add_attention_pool_layer(self.fivegram_channels, 5)
        self._add_attention_pool_layer(self.skip1bigram_channels, None, 1)
        self._add_attention_pool_layer(self.skip2bigram_channels, None, 2)

        self.concat_quantizer = quantization_layers.ConcatQuantization(
            axis=1, parameters=self.parameters)
        self.final_fc = dense_layers.BaseQDense(units=self.num_classes,
                                                rank=2,
                                                parameters=self.parameters,
                                                activation=None)
Esempio n. 2
0
 def __init__(self,
              model_dimension,
              num_heads,
              attention_dropout_rate=0.0,
              **kwargs):
     self.model_dimension = model_dimension
     self.num_heads = num_heads
     self.filters = model_dimension // num_heads
     self.dense_layers = dense_layers.BaseQDenseVarLen(
         units=model_dimension * 3, activation=None, **kwargs)
     self.qactivation = quantization_layers.ActivationQuantization(**kwargs)
     self.attention_dropout_rate = attention_dropout_rate
     self.qconcat = quantization_layers.ConcatQuantization(axis=1, **kwargs)
     super(SelfAttentionV2, self).__init__(**kwargs)
Esempio n. 3
0
  def __init__(self,
               kwidth,
               state_size,
               zoneout_probability=0.0,
               pooling=QUASI_RNN_POOLING_FO,
               bottleneck_size=None,
               **kwargs):
    self.pooling = pooling
    if bottleneck_size is None:
      self.forward = QRNNUnidirectional(
          kwidth=kwidth,
          state_size=state_size,
          forward=True,
          output_quantized=False,
          zoneout_probability=zoneout_probability,
          pooling=pooling,
          **kwargs)
      self.backward = QRNNUnidirectional(
          kwidth=kwidth,
          state_size=state_size,
          forward=False,
          output_quantized=False,
          zoneout_probability=zoneout_probability,
          pooling=pooling,
          **kwargs)
    else:
      self.forward = QRNNUnidirectionalWithBottleneck(
          kwidth=kwidth,
          state_size=state_size,
          bottleneck_size=bottleneck_size,
          forward=True,
          output_quantized=False,
          zoneout_probability=zoneout_probability,
          pooling=pooling,
          **kwargs)
      self.backward = QRNNUnidirectionalWithBottleneck(
          kwidth=kwidth,
          state_size=state_size,
          bottleneck_size=bottleneck_size,
          forward=False,
          output_quantized=False,
          zoneout_probability=zoneout_probability,
          pooling=pooling,
          **kwargs)

    self.qconcat = quantization_layers.ConcatQuantization(axis=2, **kwargs)
    super(QRNNBidirectional, self).__init__(**kwargs)
Esempio n. 4
0
 def __init__(self,
              model_dimension,
              num_heads,
              attention_dropout_rate=0.0,
              cached_kv=False,
              **kwargs):
     self.model_dimension = model_dimension
     self.num_heads = num_heads
     self.filters = model_dimension // num_heads
     self.cached_kv = cached_kv
     self.q_dense_layers = dense_layers.BaseQDense(units=model_dimension,
                                                   activation=None,
                                                   normalize=False,
                                                   bias=False,
                                                   **kwargs)
     self.kv_dense_layers = dense_layers.BaseQDenseVarLen(
         units=model_dimension * 2, activation=None, **kwargs)
     self.qactivation = quantization_layers.ActivationQuantization(**kwargs)
     self.attention_dropout_rate = attention_dropout_rate
     self.qconcat = quantization_layers.ConcatQuantization(axis=1, **kwargs)
     super(DecoderMultiheadAttention, self).__init__(**kwargs)