T 3

作者: sumpig | 来源:发表于2019-01-29 21:10 被阅读0次

一次完成多个操作

  • tf.group
tf.group(
    *inputs,
    **kwargs
)

*inputs: 0或多个张量;
name: 操作名称;

  • tf.control_dependencies(control_inputs)

control_inputs: 一个包括操作和张量对象的列表;

使用 with 关键字可以执行某些操作的依赖关系。
None 可以消除依赖。

with g.control_dependencies([a, b, c]):
  # `d` and `e` will only run after `a`, `b`, and `c` have executed.
  d = ...
  e = ...

with g.control_dependencies([a, b]):
  # Ops constructed here run after `a` and `b`.
  with g.control_dependencies(None):
    # Ops constructed here run normally, not waiting for either `a` or `b`.
    with g.control_dependencies([c, d]):
      # Ops constructed here run after `c` and `d`, also not waiting
      # for either `a` or `b`.
  • tf.no_op(name=None)

不执行任何操作,仅用于占位

  • 示例
#以下代码等价
train_op = tf.group(train_step, variables_averages_op)

with tf.control_dependencies([train_step, variables_averages_op]):
    train_op = tf.no_op(name='train')

class tf.train.Saver

  • save
save(
    sess,
    save_path,
    global_step=None,
    latest_filename=None,
    meta_graph_suffix='meta',
    write_meta_graph=True,
    write_state=True,
    strip_default_attrs=False
)

sess: 要保存的会话;
save_path: 保存路径;
global_step: 文件名附加信息;

  • 示例
saver = tf.train.Saver()
with tf.Session() as sess:
    ...
    saver.save(
        sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME),
        global_step=global_step)
  • restore
restore(
    sess,
    save_path
)

tf.app.run

  • 用法
tf.app.run(
    main=None,
    argv=None
)
  • 示例
#执行程序中的 main 方法 和 argv 列表中的内容
def main():
    ...
    
if __name__ == '__main__':
    tf.app.run()

tf.train.get_checkpoint_state

Returns CheckpointState proto from the "checkpoint" file.
If the "checkpoint" file contains a valid CheckpointState proto, returns it.

tf.train.get_checkpoint_state(
    checkpoint_dir,
    latest_filename=None
)

tf.nn.conv2d

  • 用法
tf.nn.conv2d(
    input,
    filter,
    strides,
    padding,
    use_cudnn_on_gpu=True,
    data_format='NHWC',
    dilations=[1, 1, 1, 1],
    name=None
)

input: 输入,4-D张量;
filter: 卷积层的权重,4-D张量[filter_height, filter_width, in_channels, out_channels];
strides: 步幅,1-D张量,长度4;
padding: 填充类型,"SAME", "VALID";
data_format: 指定数据格式,默认为[批次,高度,宽度,通道];
dilations: 膨胀系数;

  • 示例
conv = tf.nn.conv2d(
    input, filter_weight, strides=[1, 1, 1, 1], padding='SAME')

tf.nn.bias_add

  • 用法
tf.nn.bias_add(
    value,
    bias,
    data_format=None,
    name=None
)

value: 一个张量;
bias: 1-D张量;
data_format: 支持"NHWC","NCHW";

  • 示例
biases = tf.get_variable(
    "biases", [16], initializer=tf.constant_initializer(0.1))
bias = tf.nn.bias_add(conv, biases)

相关文章

网友评论

      本文标题:T 3

      本文链接:https://www.haomeiwen.com/subject/usjfsqtx.html