我正在尝试创建四个线程(每个线程都有自己的图形和模型),这些线程将以相同的方式运行并以相同的方式发布预测。
我的线程代码类似于:
thread_locker.acquire()
thread_graph = Graph()
with thread_graph.as_default():
thread_session = Session()
with thread_session.as_default():
#Model Training
if (once_flag_raised == False):
try:
model = load_model('ten_step_forward_'+ timeframe +'.h5')
except OSError:
input_layer = Input(shape=(X_train.shape[1], 17,))
lstm = Bidirectional(
LSTM(250),
merge_mode='concat')(input_layer)
pred = Dense(10)(lstm)
model = Model(inputs=input_layer, outputs=pred)
model.compile(optimizer='adam', loss='mean_squared_error')
once_flag_raised = True
model.fit(X_train, y_train, epochs=10, batch_size=128)
thread_locker.acquire()
nn_info_dict['model'] = model
nn_info_dict['sc'] = sc
model.save('ten_step_forward_'+ timeframe +'.h5')
thread_locker.release()
thread_locker.release()
(....)
thread_locker.acquire()
thread_graph = Graph()
with thread_graph.as_default():
thread_session = Session()
with thread_session.as_default():
pred_data= model.predict(X_pred)
thread_locker.release()
每个帖子都有。
当我读取代码的预测部分时,我一直收到以下错误(线程 - 1次):
ValueError: Tensor Tensor("dense_1/BiasAdd:0", shape=(?, 10), dtype=float32) is not an element of this graph.
我的理解是其中一个主题“声称”了Tensorflow后端及其默认的图形和会话。
有什么方法可以解决这个问题吗?
答案 0 :(得分:1)
我已经弄清楚我做错了什么。 我的想法是正确的,但我不应该重新创建下面的图表和会话。 代码的底部应该只是:
thread_locker.acquire()
with thread_graph.as_default():
with thread_session.as_default():
pred_data= model.predict(X_pred)
thread_locker.release()