機械学習のExampleから覚えるPython(@デコレータ)
今までPythonを感覚的に使っていたので、改めて文法を知ろうかなと。
その際にいま流行りの機械学習(深層学習)のExampleを例にすると
わかりやすいのかなと思ったので書いてみる。
※基本的には Python3.x系のつもりで記載してます
の時に書いてた
@keras_export('keras.datasets.mnist.load_data') def load_data(path='mnist.npz'):
の「@」デコレータについてです。
簡単にいうと
既存関数の処理の前後に自分自身で、処理を付け加える。
今回でいうと、load_data
関数の前後で何か処理やっているようです。
細かく見ていく
@keras_export('keras.datasets.mnist.load_data')
keras_export
は 先頭で以下で宣言されています。
from tensorflow.python.util.tf_export import keras_export
では、tf_export
はというと、
tf_export にて定義されてます。
その api_export はなんでしょうか。
class api_export(object): # pylint: disable=invalid-name """Provides ways to export symbols to the TensorFlow API."""
__init__
関数に書いてあるのをみる。
def __init__(self, *args, **kwargs): # pylint: disable=g-doc-args """Export under the names *args (first one is considered canonical). Args: *args: API names in dot delimited format. **kwargs: Optional keyed arguments. v1: Names for the TensorFlow V1 API. If not set, we will use V2 API names both for TensorFlow V1 and V2 APIs. overrides: List of symbols that this is overriding (those overrided api exports will be removed). Note: passing overrides has no effect on exporting a constant. api_name: Name of the API you want to generate (e.g. `tensorflow` or `estimator`). Default is `tensorflow`. allow_multiple_exports: Allow symbol to be exported multiple time under different names. """
ってことらしい。
今回でいうと、特に引数はなく単純に load_data
を呼んでいるみたい。