【Tensorflow】tf.app.run () 와 명령행 매개 변수 분석
6992 단어 TensorFlow
tf.app.run()
먼저 일반적인 코드를 제공합니다.
if __name__ == '__main__':
tf.app.run()
이전 함수
run()
에 대한 Tensorflow의 소스를 찾습니다.def run(main=None, argv=None):
"""Runs the program with an optional 'main' function and 'argv' list."""
f = flags.FLAGS
# Extract the args from the optional `argv` list.
args = argv[1:] if argv else None
# Parse the known flags from that list, or from the command
# line otherwise.
# pylint: disable=protected-access
flags_passthrough = f._parse_flags(args=args)
# pylint: enable=protected-access
main = main or _sys.modules['__main__'].main
# Call the main function, passing through any arguments
# to the final program.
_sys.exit(main(_sys.argv[:1] + flags_passthrough))
_allowed_symbols = [
'run',
# Allowed submodule.
'flags',
]
remove_undocumented(__name__, _allowed_symbols)
원본 코드의 과정은 먼저
flags
의 매개 변수 항목을 불러온 다음에 main
함수를 실행하는 것을 볼 수 있다.여기서 매개변수는 tf.app.flags.FLAGS
로 정의됩니다.tf.app.flags.FLAGS
tf.app.flags.FLAGS
사용 정보:# fila_name: temp.py
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('string', 'train', 'This is a string')
tf.app.flags.DEFINE_float('learning_rate', 0.001, 'This is the rate in training')
tf.app.flags.DEFINE_boolean('flag', True, 'This is a flag')
print('string: ', FLAGS.string)
print('learning_rate: ', FLAGS.learning_rate)
print('flag: ', FLAGS.flag)
출력:
string: train
learning_rate: 0.001
flag: True
명령줄에서
python3 temp.py --help
을 실행하면 다음과 같이 출력됩니다.usage: temp.py [-h] [--string STRING] [--learning_rate LEARNING_RATE]
[--flag [FLAG]] [--noflag]
optional arguments:
-h, --help show this help message and exit
--string STRING This is a string
--learning_rate LEARNING_RATE
This is the rate in training
--flag [FLAG] This is a flag
--noflag
FLAGS
의 기본값을 수정하려면 명령을 입력하면 됩니다.python3 temp.py --string 'test' --learning_rate 0.2 --flag False
공동 사용
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('string', 'train', 'This is a string')
tf.app.flags.DEFINE_float('learning_rate', 0.001, 'This is the rate in training')
tf.app.flags.DEFINE_boolean('flag', True, 'This is a flag')
def main(unuse_args):
print('string: ', FLAGS.string)
print('learning_rate: ', FLAGS.learning_rate)
print('flag: ', FLAGS.flag)
if __name__ == '__main__':
tf.app.run()
주함수 중의
tf.app.run()
는 main
를 호출하고 파라미터를 전달하기 때문에 main
함수에 파라미터의 위치를 설정해야 한다.main
이름을 바꾸려면 tf.app.run()
에 지정한 함수 이름을 입력하면 됩니다.def test(args):
# test
...
if __name__ == '__main__':
tf.app.run(test)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
EMNIST에서 알파벳 필기 인식EMNIST-letters를 배웠습니다. CODE: DEMO: — mbotsu (@mb_otsu) 은 2017년에 NIST가 공개한 데이터세트입니다. EMNIST ByClass: 814,255 characters. ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.