여러 스레드 (그리고 tensorflow
백엔드)를 사용하여 매개 변수 값이 다른 복수 keras
모델을 교육하려고합니다. 여러 스레드 내에서 동일한 모델을 사용하는 몇 가지 예를 보았습니다. 그러나이 특별한 경우에는 충돌하는 그래프와 관련된 여러 가지 오류가 발생합니다. 여기에 내가 할 수있는 작업에 대한 간단한 예제가 있습니다.TensorFlow/Keras 멀티 스레드 모델 피팅
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import tensorflow as tf
from keras import backend as K
from keras.layers import Dense
from keras.models import Sequential
sess = tf.Session()
def example_model(size):
model = Sequential()
model.add(Dense(size, input_shape=(5,)))
model.add(Dense(1))
model.compile(optimizer='sgd', loss='mse')
return model
if __name__ == '__main__':
K.set_session(sess)
X = np.random.random((10, 5))
y = np.random.random((10, 1))
models = [example_model(i) for i in range(5, 10)]
e = ThreadPoolExecutor(4)
res_list = [e.submit(model.fit, X, y) for model in models]
for res in res_list:
print(res.result())
결과 오류는 ValueError: Tensor("Variable:0", shape=(5, 5), dtype=float32_ref) must be from the same graph as Tensor("Variable_2/read:0", shape=(), dtype=float32).
입니다. 비슷한 실패를주는 스레드 내에서 모델을 초기화하려고 시도했습니다.
이 문제를 해결하는 가장 좋은 방법에 대한 의견이 있으십니까? 나는이 정확한 구조에 전혀 붙어 있지 않지만, 모든 모델이 동일한 GPU 메모리 할당 내에서 훈련되도록 프로세스보다는 다중 쓰레드를 사용할 수 있기를 원합니다.
을, 나는이 하나 개의 프로세스에서 실행중인 모델, 그것은 항상 ValueError를 보여줘 가지고 인수를 가져 오기 Tensor로 해석 될 수 없다. (Tensor Tensor ("input : 0", shape = (2, 2), dtype = float32_ref)는이 그래프의 요소가 아닙니다. – forqzy