(x, n_token, d_embed, initializer, use_tpu=True,
                     scope='embedding', reuse=None, dtype=tf.float32)
TPU and GPU embedding_lookup function.
TPU and GPU embedding_lookup function.
TPU and GPU embedding_lookup function .
def embedding_lookup(x, n_token, d_embed, initializer, use_tpu=True,
                     scope='embedding', reuse=None, dtype=tf.float32):
  """TPU and GPU embedding_lookup function."""
  with tf.variable_scope(scope, reuse=reuse):
    lookup_table = tf.get_variable('lookup_table', [n_token, d_embed],
                                   dtype=dtype, initializer=initializer)
    if use_tpu:
      one_hot_idx = tf.one_hot(x, n_token, dtype=dtype)
      if one_hot_idx.shape.ndims == 2:
        return tf.einsum('in,nd->id', one_hot_idx, lookup_table), lookup_table
      else:
        return tf.einsum('ibn,nd->ibd', one_hot_idx, lookup_table), lookup_table
    else:
      return tf.nn.embedding_lookup(lookup_table, x), lookup_table
def embedding_lookup ( x , n_token , d_embed , initializer , use_tpu = True , scope = 'embedding' , reuse = None , dtype = tf . float32 ) : with tf . variable_scope ( scope , reuse = reuse ) : lookup_table = tf . get_variable ( 'lookup_table' , [ n_token , d_embed ] , dtype = dtype , initializer = initializer ) if use_tpu : one_hot_idx = tf . one_hot ( x , n_token , dtype = dtype ) if one_hot_idx . shape . ndims == 2 : return tf . einsum ( 'in,nd->id' , one_hot_idx , lookup_table ) , lookup_table else : return tf . einsum ( 'ibn,nd->ibd' , one_hot_idx , lookup_table ) , lookup_table else : return tf . nn . embedding_lookup ( lookup_table , x ) , lookup_table