IT 지식 창고
(tensorflow) tensorflow 2.x GPU 메모리 제한 후 필요시 증가
casim
2020. 9. 10. 18:11
tensorflow 2.x 부터 뭐가 많이 바뀌었는지, keras는 검색해도 안나오고 tensorflow 2.x document에서 찾아가며 하나씩 적어보려 합니다.
physical_devices = tf.config.list_physical_devices('GPU')
try:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
except:
# Invalid device or cannot modify virtual devices once initialized.
pass
tf.config.list_physical_devices()
이 함수는 'GPU'와 같은 파라미터를 사용하지 않아도 되며, 명시를 해주면 GPU만 list로 도출해줍니다.
즉, GPU장치가 물리적으로 존재하는지 확인하기 위함입니다.
tf.config.experimental.set_memory_growth(physical_devices[0], True)
이 함수는 처음에 GPU메모리에 최소한으로 할당 후 필요 프로세스에 따라 증가하는 함수입니다.
보통 GPU의 낮은 Id부터 할당되어 physical_devices[0] (제일 처음의 GPU)를 명시하여 위 활동을 작동시키도록 합니다.