TensorFlow 提取指定名的op函数

部分代码:

def _get_variables_to_train(trainable_scopes = None):
  """Returns a list of variables to train.

  Returns:
    A list of variables to train by the optimizer.
  """
  if trainable_scopes is None:
    return tf.trainable_variables()
  else:
    scopes = [scope.strip() for scope in trainable_scopes.split(',')]

  variables_to_train = []
  for scope in scopes:
    variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
    variables_to_train.extend(variables)
  return variables_to_train
output_vars = _get_variables_to_train(Config.trainable_scopes)
#Config.trainable_scopes参数如下:
trainable_scopes = 'HCnet/Bottle_neck5,HCnet/Bottle_neck5_1,HCnet/Bottle_neck6_2,HCnet/Conv7'

当需要提取指定op进行操作时,可通过_get_variables_to_train函数可以获取指定的op层

结果如下:

output_vars: 
[<tf.Variable 'HCnet/Bottle_neck5/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/weights:0' shape=(1, 1, 64, 64) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck5_1/pw_linear/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/dw/depthwise_weights:0' shape=(3, 3, 64, 1) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/dw/BatchNorm/gamma:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/dw/BatchNorm/beta:0' shape=(64,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/weights:0' shape=(1, 1, 64, 80) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/BatchNorm/gamma:0' shape=(80,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Bottle_neck6_2/pw_linear/BatchNorm/beta:0' shape=(80,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Conv7/weights:0' shape=(1, 1, 80, 24) dtype=float32_ref>, 
<tf.Variable 'HCnet/Conv7/BatchNorm/gamma:0' shape=(24,) dtype=float32_ref>, 
<tf.Variable 'HCnet/Conv7/BatchNorm/beta:0' shape=(24,) dtype=float32_ref>]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值