2017-04-16 4 views
2

TensorFlow에서 FLAGS가 필요한 이유를 이해할 수 없습니다. 이제 TensorFlow를 저의 책에서 공부합니다.깃발이 필요한 이유는 무엇입니까?

# coding: utf-8 

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 

import os 

import numpy as np 
import tensorflow as tf 
from PIL import Image 

from reader import Cifar10Reader 

FLAGS = tf.app.flags.FLAGS 
tf.app.flags.DEFINE_string('file',None,"path") 
tf.app.flags.DEFINE_integer('offset',0,"record") 
tf.app.flags.DEFINE_integer('length',16,"change record") 

basename = os.path.basename(FLAGS.file) 
path = os.path.dirname(FLAGS.file) 

reader = Cifar10Reader(FLAGS.file) 

stop = FLAGS.offset + FLAGS.length 

for index in range(FLAGS.offset,stop): 
    image = reader.read(index) 

    print('label: %d' % image.label) 
    imageshow = Image.fromarray(image.byte_array.astype(np.unit8)) 

    file_name = '%s-%02d-%d.png' % (basename,index,image.label) 
    file = os.path.join(path,file_name) 
    with open(file,mode='wb') as out: 
     imageshow.save(out,format='png') 

reader.close() 

나는이 코드처럼 쓰고, 나는

FLAGS = tf.app.flags.FLAGS 

이 부분을 이해할 수 없습니다. FLAGS는 오류 메시지 라벨을 읽었지 만 언제 필요합니까? (아마도 내 정보가 잘못되었을 수 있습니다) 이 파트가 필요한 이유는 무엇입니까? 이 파트에는 어떤 기능이 있습니까?

답변

1

보통 FLAGS은 프로그램에 명령 줄 인수를 전달하는 데 사용됩니다. 예 :

import tensorflow as tf 
fs = tf.app.flags 
fs.DEFINE_integer('n_epochs', 25, 'number of epochs to train [25]') 
FLAGS = fs.FLAGS 

def main(argv): 
    print(FLAGS.n_epochs) 

if __name__ == '__main__': 
    tf.app.run() 

당신이 python snippet.py 같은 명령 줄에서이 코드를 실행하는 경우 python snippet.py --n_epochs 50를 실행하는 경우, 그것은

50 

당신은과 같은 일을 달성 할 수 인쇄됩니다

25 

를 인쇄합니다 python의 패키지 argparse.

게시 한 예에서 FLAGS의 사용은 다소 이상합니다. 여기서 FLAGS 변수가 코드에서 다른 곳에서 사용되지 않는 한 변수를 직접 정의하는 것으로 대체 될 수 있습니다. 여기서는 표시되지 않습니다.

관련 문제