在TensorFlow框架中增加自定义Hook

本笔记演示的是怎么在tensorflow/python/training/basic_session_run_hooks.py增加一个自定义Hook;在其他地方增加Hook可能有所不同,但是基本流程相同,也可参考此笔记。

 

一.自定义Hook:


@tf_export("train.MyHook")
class MyHook(session_run_hook.SessionRunHook):
  """Hook to extend calls to MonitoredSession.run()."""

  def __init__(self, arg1, arg2=10):
    """Initializes a `MyHook`.
    pass

  def begin(self):
    """Called once before using the session.

    When called, the default graph is the one that will be launched in the
    session.  The hook can modify the graph by adding new operations to it.
    After the `begin()` call the graph will be finalized and the other callbacks
    can not modify the graph anymore. Second call of `begin()` on the same
    graph, should not change the graph.
    """
    pass

  def after_create_session(self, session, coord):  # pylint: disable=unused-argument
    """Called when new TensorFlow session is created.

    This is called to signal the hooks that a new session has been created. This
    has two essential differences with the situation in which `begin` is called:

    * When this is called, the graph is finalized and ops can no longer be added
        to the graph.
    * This method will also be called as a result of recovering a wrapped
        session, not only at the beginning of the overall session.

    Args:
      session: A TensorFlow Session that has been created.
      coord: A Coordinator object which keeps track of all threads.
    """
    pass

  def before_run(self, run_context):  # pylint: disable=unused-argument
    """Called before each call to run().

    You can return from this call a `SessionRunArgs` object indicating ops or
    tensors to add to the upcoming `run()` call.  These ops/tensors will be run
    together with the ops/tensors originally passed to the original run() call.
    The run args you return can also contain feeds to be added to the run()
    call.

    The `run_context` argument is a `SessionRunContext` that provides
    information about the upcoming `run()` call: the originally requested
    op/tensors, the TensorFlow Session.

    At this point graph is finalized and you can not add ops.

    Args:
      run_context: A `SessionRunContext` object.

    Returns:
      None or a `SessionRunArgs` object.
    """
    return None

  def after_run(self,
                run_context,  # pylint: disable=unused-argument
                run_values):  # pylint: disable=unused-argument
    """Called after each call to run().

    The `run_values` argument contains results of requested ops/tensors by
    `before_run()`.

    The `run_context` argument is the same one send to `before_run` call.
    `run_context.request_stop()` can be called to stop the iteration.

    If `session.run()` raises any exceptions then `after_run()` is not called.

    Args:
      run_context: A `SessionRunContext` object.
      run_values: A SessionRunValues object.
    """
    pass

  def end(self, session):  # pylint: disable=unused-argument
    """Called at the end of session.

    The `session` argument can be used in case the hook wants to run final ops,
    such as saving a last checkpoint.

    If `session.run()` raises exception other than OutOfRangeError or
    StopIteration then `end()` is not called.
    Note the difference between `end()` and `after_run()` behavior when
    `session.run()` raises OutOfRangeError or StopIteration. In that case
    `end()` is called but `after_run()` is not called.

    Args:
      session: A TensorFlow Session that will be soon closed.
    """
    pass

二.注册Hook:

1.在tensorflow/tools/api/golden/v1/tensorflow.train.pbtxt 和 tensorflow/tools/api/golden/v2/tensorflow.train.pbtxt 中注册MyHook的名称:

member {
    name: "MyHook"
    mtype: "<type \'type\'>"
  }

2.在 tensorflow/tools/api/golden/v1 和 tensorflow/tools/api/golden/v2 目录新增描述MyHook的接口属性文件tensorflow.train.-my-hook.pbtxt,注册MyHook的接口:

path: "tensorflow.train.MyHook"
tf_class {
  is_instance: "<class \'tensorflow.python.training.basic_session_run_hooks.MyHook\'>"
  is_instance: "<class \'tensorflow.python.training.session_run_hook.MyHook\'>"
  is_instance: "<type \'object\'>"
  member_method {
    name: "__init__"
    argspec: "args=[\'self\', \'arg1\', \'arg2\'], varargs=None, keywords=None, defaults=[\'0\', \'0\'], "
  }
  member_method {
    name: "after_create_session"
    argspec: "args=[\'self\', \'session\', \'coord\'], varargs=None, keywords=None, defaults=None"
  }
  member_method {
    name: "after_run"
    argspec: "args=[\'self\', \'run_context\', \'run_values\'], varargs=None, keywords=None, defaults=None"
  }
  member_method {
    name: "before_run"
    argspec: "args=[\'self\', \'run_context\'], varargs=None, keywords=None, defaults=None"
  }
  member_method {
    name: "begin"
    argspec: "args=[\'self\'], varargs=None, keywords=None, defaults=None"
  }
  member_method {
    name: "end"
    argspec: "args=[\'self\', \'session\'], varargs=None, keywords=None, defaults=None"
  }
}

三.编译和部署tf框架:

1.编译:

./configure

bazel build --jobs=32 --config=opt --config=mkl --config=cuda //tensorflow/tools/pip_package:build_pip_package

2.部署:

bazel-bin/tensorflow/tools/pip_package/build_pip_package ./tensorflow_pkg

sudo pip install ./tensorflow_pkg/pkg_name.whl

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
React的自定义Hook是一种重用逻辑的方式。自定义Hook是一个函数,其名称以"use"开头,并且可以在其他函数组件调用。它可以用来提取和组合状态逻辑、副作用和其他React特性。 例如,我们可以创建一个自定义Hook来处理表单输入: ``` import { useState } from 'react'; function useInput(initialValue) { const [value, setValue] = useState(initialValue); function handleChange(e) { setValue(e.target.value); } return { value, onChange: handleChange, }; } ``` 上述代码,我们创建了一个名为"useInput"的自定义Hook。它使用useState来创建一个名为"value"的状态和一个名为"setValue"的更新函数。然后,它定义了一个名为"handleChange"的函数来更新"value"状态的值。最后,它返回一个包含"value"和"onChange"的对象。 现在,我们可以在任何函数组件使用这个自定义Hook: ``` function MyComponent() { const nameInput = useInput(''); const emailInput = useInput(''); return ( <div> <input type="text" {...nameInput} /> <input type="email" {...emailInput} /> </div> ); } ``` 在上述代码,我们通过调用"useInput"自定义Hook两次来创建名为"nameInput"和"emailInput"的输入字段。然后,我们可以像使用普通的input元素一样使用它们,并将所有属性通过展开运算符传递给input元素。 通过自定义Hook,我们可以将常见的逻辑抽象出来并在多个组件重用。这有助于提高代码的可维护性和可重用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值