예제 #1
0
 def __init__(self,
              vocab=None,
              mask_special_tokens=True,
              trainable=True,
              **kwargs):
     super().__init__(trainable=trainable, **kwargs)
     self._vocab = vocabulary.get_default() if vocab is None else vocab
     self._mask_special_tokens = mask_special_tokens
예제 #2
0
 def __init__(self,
              vocab=None,
              kernel_init=tf.initializers.GlorotUniform(),
              bias_init=tf.initializers.Zeros(),
              **kwargs):
     super().__init__(**kwargs)
     self._vocab = vocabulary.get_default() if vocab is None else vocab
     self._kernel_init = kernel_init
     self._bias_init = bias_init
예제 #3
0
 def __init__(self,
              vocab = None,
              kernel_init='GlorotUniform',
              bias_init='Zeros',
              **kwargs):
   super().__init__(**kwargs)
   self._vocab = vocabulary.get_default() if vocab is None else vocab
   self._kernel_init = tf.keras.initializers.get(kernel_init)
   self._bias_init = tf.keras.initializers.get(bias_init)
예제 #4
0
 def __init__(self,
              vocab=None,
              matrix_str=BLOSUM_62,
              pad_penalty=-1e9,
              **kwargs):
     super().__init__(**kwargs)
     self._vocab = vocabulary.get_default() if vocab is None else vocab
     self._matrix_str = matrix_str
     self._pad_penalty = pad_penalty
예제 #5
0
 def __init__(self,
              on='seq_len',
              vocab=None,
              max_len=512,
              precomputed=True):
     self._on = (on, ) if isinstance(on, str) else on
     self._vocab = vocabulary.get_default() if vocab is None else vocab
     self._max_len = max_len
     self._precomputed = precomputed
예제 #6
0
 def __init__(self,
              vocab=None,
              use_hidden_layer=True,
              activation=activations.approximate_gelu,
              kernel_init=tf.initializers.GlorotUniform(),
              bias_init=tf.initializers.Zeros(),
              logits_bias_init=tf.initializers.Zeros(),
              norm_logits=True,
              **kwargs):
     super().__init__(**kwargs)
     self._vocab = vocabulary.get_default() if vocab is None else vocab
     self._use_hidden_layer = use_hidden_layer
     self._activation = activation
     self._kernel_init = kernel_init
     self._bias_init = bias_init
     self._logits_bias_init = logits_bias_init
     self._norm_logits = norm_logits
예제 #7
0
  def __init__(self,
               max_len = 512,
               tau = 0.01,
               alpha = 0.05,
               eta = 0.7,
               vocab = None):
    self._max_len = max_len
    vocab = vocabulary.get_default() if vocab is None else vocab
    self._sampler = vocabulary.Sampler(vocab=vocab)
    self._eos = vocab.get(vocab.specials[-1])
    self._pad = vocab.padding_code

    # Transition look-up table (excluding special initial transition).
    look_up = {
        (self.MATCH, self.MATCH): 1,
        (self.GAP_IN_X, self.MATCH): 2,
        (self.GAP_IN_Y, self.MATCH): 3,
        (self.MATCH, self.GAP_IN_X): 4,
        (self.GAP_IN_X, self.GAP_IN_X): 5,
        (self.GAP_IN_Y, self.GAP_IN_X): 9,  # "forbidden" transition.
        (self.MATCH, self.GAP_IN_Y): 6,
        (self.GAP_IN_X, self.GAP_IN_Y): 7,
        (self.GAP_IN_Y, self.GAP_IN_Y): 8,
    }
    # Builds data structures for efficiently encoding transitions.
    self._hash_fn = lambda d0, d1: 3 * (d1 + 1) + (d0 + 1)
    hashes = [self._hash_fn(d0, d1) for (d0, d1) in look_up]
    trans_encoder = tf.scatter_nd(
        indices=[[x] for x in hashes],
        updates=list(look_up.values()),
        shape=[max(hashes) + 1])
    self._trans_encoder = tf.cast(trans_encoder, tf.int32)
    self._init_trans = tf.convert_to_tensor([self.INIT_TRANS], dtype=tf.int32)

    cond_probs = tf.convert_to_tensor(
        [[0.0, 1.0, 0.0, 0.0, 0.0],
         [0.0, 1.0 - 2.0 * alpha - tau, alpha, alpha, tau],
         [0.0, eta, 1.0 - eta - alpha, alpha, 0.0],
         [0.0, eta, 0.0, 1.0 - eta, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0]],
        tf.float32)
    self._logits = tf.where(cond_probs > 0.0, tf.math.log(cond_probs), -np.inf)

    self._delta_len_x = tf.convert_to_tensor([0, 1, 0, 1, 0])
    self._delta_len_y = tf.convert_to_tensor([0, 1, 1, 0, 0])
예제 #8
0
 def __init__(self, on='sequence', out=None, vocab=None):
     self._on = (on, ) if isinstance(on, str) else on
     out = self._on if out is None else out
     self._out = (out, ) if isinstance(out, str) else out
     self._vocab = vocabulary.get_default() if vocab is None else vocab