tf.control_dependencies()

1.tf.control_dependencies

此函数指定某些操作执行的依赖关系 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都在 control_inputs 执行  

举例:

with tf.control_dependencies([a, b]):
     c = ....
     d = ...

# 在执行完 a,b 操作之后,才能执行 c,d 操作。意思就是 c,d 操作依赖 a,b 操作

bert源码:

# Position embedding信息
if use_position_embeddings:
# 确保seq_length小于等于max_position_embeddings
assert_op = tf.assert_less_equal(seq_length, max_position_embeddings)
with tf.control_dependencies([assert_op]):
  full_position_embeddings = tf.get_variable(
      name=position_embedding_name,
      shape=[max_position_embeddings, width],
      initializer=create_initializer(initializer_range))

猜你喜欢

转载自www.cnblogs.com/nxf-rabbit75/p/12096613.html
tf
今日推荐