当前位置: 首页>>代码示例>>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;未经允许,请勿转载。