본문 바로가기

Back/Deep Learning

[Python] [Keras] Keras scratch graph

Keras를 사용한 모델을 사용하다 보면 가끔 뻘건 글씨로 

Function call stack: keras_scratch_graph Error

라뜨며 에러가 납니다. 

 

자세한 원인은 모르겠으나

https://stackoverflow.com/questions/57062456/function-call-stack-keras-scratch-graph-error

 

Function call stack: keras_scratch_graph Error

I am reimplementing a text2speech project. I am facing a Function call stack : keras_scratch_graph error in decoder part. The network architecture is from Deep Voice 3 paper. I am using keras from...

stackoverflow.com

에 따르면 GPU메모리 처리 문제인것 같습니다. 

해답으론 첫번째 댓글에 있는 코드를 삽입하라 하며 삽입할 경우 잘해결 됩니다.

 

다만 처리속도를 볼때 GPU를 이용하는게 아니라 CPU로 강제로 설정을 돌리는 것 같습니다. 

얼마전까진 시도때도 없이 떠서 괴롭히더니 정작 필요할땐 안뜨고 잘실행이 됩니다.

 

CPU로 돌려도 크게 시간차이가 안나는 모델일 경우 코드를 추가해서 돌리면 될것이고 

GPU가 꼭필요하다면 재부팅후 실행하는 것을 추천 드립니다.

 

 

아래 코드는 자주 에러가 일어나 import 해서 쓸수 있도록 묶어 놓았습니다.

import 해서 fixerr을 불러주면 문제가 해결되었습니다.  

import tensorflow as tf

def fixerr():
    gpus = tf.config.experimental.list_physical_devices('GPU')
    if gpus:
        try:
            # Restrict TensorFlow to only use the fourth GPU
            tf.config.experimental.set_visible_devices(gpus[0], 'GPU')

            # Currently, memory growth needs to be the same across GPUs
            for gpu in gpus:
                tf.config.experimental.set_memory_growth(gpu, True)
            logical_gpus = tf.config.experimental.list_logical_devices('GPU')
            print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
        except RuntimeError as e:
            # Memory growth must be set before GPUs have been initialized
            print(e)