當前位置: 首頁>>代碼示例>>Python>>正文


Python modeling.get_shape_list方法代碼示例

本文整理匯總了Python中modeling.get_shape_list方法的典型用法代碼示例。如果您正苦於以下問題:Python modeling.get_shape_list方法的具體用法?Python modeling.get_shape_list怎麽用?Python modeling.get_shape_list使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在modeling的用法示例。


在下文中一共展示了modeling.get_shape_list方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: gather_indexes

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def gather_indexes(sequence_tensor, positions):
  """
  Gathers the vectors at the specific positions over a minibatch.
  sequence_tensor: [batch, seq_length, width]
  positions: [batch, n]
  """
  sequence_shape  = modeling.get_shape_list(sequence_tensor, expected_rank=3)
  batch_size      = sequence_shape[0]
  seq_length      = sequence_shape[1]
  width           = sequence_shape[2]

  flat_offsets    = tf.reshape(tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
  flat_positions  = tf.reshape(positions + flat_offsets, [-1])
  flat_sequence_tensor = tf.reshape(sequence_tensor, [batch_size * seq_length, width])
  output_tensor   = tf.gather(flat_sequence_tensor, flat_positions)

  return output_tensor


# 
開發者ID:ymcui,項目名稱:Cross-Lingual-MRC,代碼行數:22,代碼來源:layers.py

示例2: extract_span_tensor

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def extract_span_tensor(bert_config, sequence_tensor, output_span_mask, 
                        start_positions, end_positions,   
                        scope=None):
  sequence_shape  = modeling.get_shape_list(sequence_tensor, expected_rank=3)
  seq_length = sequence_shape[1]

  output_span_mask = tf.cast(output_span_mask, tf.float32)
  filtered_sequence_tensor = sequence_tensor * tf.expand_dims(output_span_mask, -1)

  with tf.variable_scope(scope, default_name="span_loss"):
    fw_output_tensor = gather_indexes(filtered_sequence_tensor, tf.expand_dims(start_positions, -1))
    bw_output_tensor = gather_indexes(filtered_sequence_tensor, tf.expand_dims(end_positions, -1))
    att_output_tensor = simple_attention(filtered_sequence_tensor, filtered_sequence_tensor, output_span_mask, self_adaptive=True)
    output_tensor = tf.concat([fw_output_tensor, bw_output_tensor, att_output_tensor], axis=-1)      

  return output_tensor


# 
開發者ID:ymcui,項目名稱:Cross-Lingual-MRC,代碼行數:21,代碼來源:layers.py

示例3: create_model

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,
                 use_one_hot_embeddings):
  """Creates a classification model."""
  model = modeling.BertModel(
      config=bert_config,
      is_training=is_training,
      input_ids=input_ids,
      input_mask=input_mask,
      token_type_ids=segment_ids,
      use_one_hot_embeddings=use_one_hot_embeddings)

  final_hidden = model.get_sequence_output()

  final_hidden_shape = modeling.get_shape_list(final_hidden, expected_rank=3)
  batch_size = final_hidden_shape[0]
  seq_length = final_hidden_shape[1]
  hidden_size = final_hidden_shape[2]

  output_weights = tf.get_variable(
      "cls/squad/output_weights", [2, hidden_size],
      initializer=tf.truncated_normal_initializer(stddev=0.02))

  output_bias = tf.get_variable(
      "cls/squad/output_bias", [2], initializer=tf.zeros_initializer())

  final_hidden_matrix = tf.reshape(final_hidden,
                                   [batch_size * seq_length, hidden_size])
  logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True)
  logits = tf.nn.bias_add(logits, output_bias)

  logits = tf.reshape(logits, [batch_size, seq_length, 2])
  logits = tf.transpose(logits, [2, 0, 1])

  unstacked_logits = tf.unstack(logits, axis=0)

  (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])

  return (start_logits, end_logits) 
開發者ID:Nagakiran1,項目名稱:Extending-Google-BERT-as-Question-and-Answering-model-and-Chatbot,代碼行數:40,代碼來源:run_squad.py

示例4: gather_indexes

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def gather_indexes(sequence_tensor, positions):
  """Gathers the vectors at the specific positions over a minibatch."""
  sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3)
  batch_size = sequence_shape[0]
  seq_length = sequence_shape[1]
  width = sequence_shape[2]

  flat_offsets = tf.reshape(
      tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
  flat_positions = tf.reshape(positions + flat_offsets, [-1])
  flat_sequence_tensor = tf.reshape(sequence_tensor,
                                    [batch_size * seq_length, width])
  output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
  return output_tensor 
開發者ID:Nagakiran1,項目名稱:Extending-Google-BERT-as-Question-and-Answering-model-and-Chatbot,代碼行數:16,代碼來源:run_pretraining.py

示例5: span_output_layer

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def span_output_layer(bert_config, input_tensor, input_span_mask=None, scope=None):
  input_tensor_shape = modeling.get_shape_list(input_tensor, expected_rank=3)
  batch_size = input_tensor_shape[0]
  seq_length = input_tensor_shape[1]
  hidden_size = input_tensor_shape[2]

  # output layers
  with tf.variable_scope(scope, default_name="cls/squad"):
    output_weights = tf.get_variable("output_weights", [2, hidden_size], 
                                     initializer=tf.truncated_normal_initializer(stddev=0.02))
    output_bias = tf.get_variable("output_bias", [2], 
                                  initializer=tf.zeros_initializer())

  final_hidden_matrix = tf.reshape(input_tensor, [batch_size * seq_length, hidden_size])
  logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True)
  logits = tf.nn.bias_add(logits, output_bias)

  logits = tf.reshape(logits, [batch_size, seq_length, 2])
  logits = tf.transpose(logits, [2, 0, 1])

  unstacked_logits = tf.unstack(logits, axis=0)

  (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])

  if input_span_mask is not None:
    adder           = (1.0 - tf.cast(input_span_mask, tf.float32)) * -10000.0
    start_logits   += adder
    end_logits     += adder

  return (start_logits, end_logits)


# 
開發者ID:ymcui,項目名稱:Cross-Lingual-MRC,代碼行數:35,代碼來源:layers.py

示例6: create_model

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,
                 use_one_hot_embeddings):
  """Creates a classification model."""
  model = modeling.BertModel(
      config=bert_config,
      is_training=is_training,
      input_ids=input_ids,
      input_mask=input_mask,
      token_type_ids=segment_ids,
      use_one_hot_embeddings=use_one_hot_embeddings)

  final_hidden = model.get_sequence_output()

  final_hidden_shape = modeling.get_shape_list(final_hidden, expected_rank=3)
  batch_size = final_hidden_shape[0]
  seq_length = final_hidden_shape[1]
  hidden_size = final_hidden_shape[2]

  output_weights = tf.compat.v1.get_variable(
      "cls/squad/output_weights", [2, hidden_size],
      initializer=tf.compat.v1.truncated_normal_initializer(stddev=0.02))

  output_bias = tf.compat.v1.get_variable(
      "cls/squad/output_bias", [2], initializer=tf.compat.v1.zeros_initializer())

  final_hidden_matrix = tf.reshape(final_hidden,
                                   [batch_size * seq_length, hidden_size])
  logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True)
  logits = tf.nn.bias_add(logits, output_bias)

  logits = tf.reshape(logits, [batch_size, seq_length, 2])
  logits = tf.transpose(a=logits, perm=[2, 0, 1])

  unstacked_logits = tf.unstack(logits, axis=0)

  (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])

  return (start_logits, end_logits) 
開發者ID:IntelAI,項目名稱:models,代碼行數:40,代碼來源:run_squad.py

示例7: create_model

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,
                 use_one_hot_embeddings):
    """Creates a classification model."""
    model = modeling.BertModel(
        config=bert_config,
        is_training=is_training,
        input_ids=input_ids,
        input_mask=input_mask,
        token_type_ids=segment_ids,
        use_one_hot_embeddings=use_one_hot_embeddings)

    final_hidden = model.get_sequence_output()

    final_hidden_shape = modeling.get_shape_list(final_hidden, expected_rank=3)
    batch_size = final_hidden_shape[0]
    seq_length = final_hidden_shape[1]
    hidden_size = final_hidden_shape[2]

    output_weights = tf.get_variable(
        "cls/squad/output_weights", [2, hidden_size],
        initializer=tf.truncated_normal_initializer(stddev=0.02))

    output_bias = tf.get_variable(
        "cls/squad/output_bias", [2], initializer=tf.zeros_initializer())

    final_hidden_matrix = tf.reshape(final_hidden,
                                     [batch_size * seq_length, hidden_size])
    logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True)
    logits = tf.nn.bias_add(logits, output_bias)

    logits = tf.reshape(logits, [batch_size, seq_length, 2])
    logits = tf.transpose(logits, [2, 0, 1])

    unstacked_logits = tf.unstack(logits, axis=0)

    (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])

    return (start_logits, end_logits) 
開發者ID:husseinmozannar,項目名稱:SOQAL,代碼行數:40,代碼來源:Bert_model.py

示例8: gather_indexes

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def gather_indexes(sequence_tensor, positions):
    """Gathers the vectors at the specific positions over a minibatch."""
    sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3)
    batch_size = sequence_shape[0]
    seq_length = sequence_shape[1]
    width = sequence_shape[2]

    flat_offsets = tf.reshape(
        tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
    flat_positions = tf.reshape(positions + flat_offsets, [-1])
    flat_sequence_tensor = tf.reshape(sequence_tensor,
                                      [batch_size * seq_length, width])
    output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
    return output_tensor 
開發者ID:guoyaohua,項目名稱:BERT-Chinese-Annotation,代碼行數:16,代碼來源:run_pretraining.py

示例9: create_model

# 需要導入模塊: import modeling [as 別名]
# 或者: from modeling import get_shape_list [as 別名]
def create_model(self, bert_config, is_training, input_ids, input_mask, segment_ids, use_one_hot_embeddings):
        """Creates a classification model."""
        model = modeling.BertModel(
                config=bert_config,
                is_training=is_training,
                input_ids=input_ids,
                input_mask=input_mask,
                token_type_ids=segment_ids,
                use_one_hot_embeddings=use_one_hot_embeddings,
                compute_type=tf.float32)

        final_hidden = model.get_sequence_output()

        final_hidden_shape = modeling.get_shape_list(final_hidden, expected_rank=3)
        batch_size = final_hidden_shape[0]
        seq_length = final_hidden_shape[1]
        hidden_size = final_hidden_shape[2]

        output_weights = tf.get_variable(
                "cls/squad/output_weights", [2, hidden_size],
                initializer=tf.truncated_normal_initializer(stddev=0.02))

        output_bias = tf.get_variable(
                "cls/squad/output_bias", [2], initializer=tf.zeros_initializer())

        final_hidden_matrix = tf.reshape(final_hidden, [batch_size * seq_length, hidden_size])
        logits = tf.matmul(final_hidden_matrix, output_weights, transpose_b=True)
        logits = tf.nn.bias_add(logits, output_bias)

        logits = tf.reshape(logits, [batch_size, seq_length, 2])
        return logits

        # logits = tf.transpose(logits, [2, 0, 1])

        # unstacked_logits = tf.unstack(logits, axis=0, name='unstack')

        # (start_logits, end_logits) = (unstacked_logits[0], unstacked_logits[1])

        # return (start_logits, end_logits) 
開發者ID:mlperf,項目名稱:inference,代碼行數:41,代碼來源:tf_SUT.py


注:本文中的modeling.get_shape_list方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。