whether dropout will be applied.是否进行训练
input_ids: int32 Tensor of shape [batch_size, seq_length].输入维度
input_mask: (optional) int32 Tensor of shape [batch_size, seq_length].输入掩码
token_type_ids: (optional) int32 Tensor of shape [batch_size, seq_length].令牌类型
use_one_hot_embeddings: (optional) bool. Whether to use one-hot word
embeddings or tf.embedding_lookup() for the word embeddings.
嵌入层:是否使用one-hot词嵌入
scope: (optional) variable scope. Defaults to "bert".
可选变量,默认值"bert".
Raises:
ValueError: The config is invalid or one of the input tensor shapes
is invalid.
异常:值错误,配置参数无效或者张量形状无效
"""
config = copy.deepcopy(config) 配置参数对象深度克隆
if not is_training: 如果不训练
config.hidden_dropout_prob = 0.0 放弃比例设置为0,表示不放弃参数
config.attention_probs_dropout_prob = 0.0 放弃比例设置为0,表示不放弃参数
获取输入形状
input_shape = get_shape_list(input_ids, expected_rank=2)
batch_size = input_shape[0] 批处理量,每一批处理的数据条数
seq_length = input_shape[1] 序列长度
if input_mask is None: 如果没有输出掩码,则将掩码全部设置为1
input_mask = tf.ones(shape=[batch_size, seq_length], dtype=tf.int32)
if token_type_ids is None: 如果没有令牌,则将令牌全部设置为0
token_type_ids = tf.zeros(shape=[batch_size, seq_length], dtype=tf.int32)
variable_scope 变量共享、上下文管理器,作用域;
在这个管理器下做的事情,会被这个管理器管着
with tf.variable_scope(scope, default_name="bert"):
with tf.variable_scope("embeddings"):
# Perform embedding lookup on the word ids.对单词id执行嵌入查找 。
(self.embedding_output, self.embedding_table) = embedding_lookup(
input_ids=input_ids,
vocab_size=config.vocab_size,
embedding_size=config.hidden_size,
initializer_range=config.initializer_range,
word_embedding_name="word_embeddings",
use_one_hot_embeddings=use_one_hot_embeddings)
添加位置嵌入、令牌嵌入,然后标准化并执行丢弃
# Add positional embeddings and token type embeddings, then layer
# normalize and perform dropout.
embedding_postprocessor对单词嵌入张量执行各种后处理 。
self.embedding_output = embedding_postprocessor(
input_tensor=self.embedding_output,
use_token_type=True,
token_type_ids=token_type_ids,
token_type_vocab_size=config.type_vocab_size,
token_type_embedding_name="token_type_embeddings",
use_position_embeddings=True,
position_embedding_name="position_embeddings",
initializer_range=config.initializer_range,
max_position_embeddings=config.max_position_embeddings,
dropout_prob=config.hidden_dropout_prob)
with tf.variable_scope("encoder"):
将2维掩码转换成3维,用于注意力评分
# This converts a 2D mask of shape [batch_size, seq_length] to a 3D
# mask of shape [batch_size, seq_length, seq_length] which is used
# for the attention scores.
attention_mask = create_attention_mask_from_input_mask(
input_ids, input_mask)
# Run the stacked transformer. 运行堆叠的transformer模型
# `sequence_output` shape = [batch_size, seq_length, hidden_size].
创建transformer_model对象
self.all_encoder_layers = transformer_model(
input_tensor=self.embedding_output,
attention_mask=attention_mask,
hidden_size=config.hidden_size,
num_hidden_layers=config.num_hidden_layers,
num_attention_heads=config.num_attention_heads,
intermediate_size=config.intermediate_size,
intermediate_act_fn=get_activation(config.hidden_act),
经验总结扩展阅读
- 【lwip】11-UDP协议&源码分析
- 硬核剖析Java锁底层AQS源码,深入理解底层架构设计
- SpringCloudAlibaba 微服务组件 Nacos 之配置中心源码深度解析
- Seata 1.5.2 源码学习
- MindStudio模型训练场景精度比对全流程和结果分析
- .NET 源码学习 [数据结构-线性表1.2] 链表与 LinkedList<T>
- Redisson源码解读-公平锁
- OpenHarmony移植案例: build lite源码分析之hb命令__entry__.py
- 【深入浅出 Yarn 架构与实现】1-2 搭建 Hadoop 源码阅读环境
- JVM学习笔记——内存模型篇