tensorflow worker在主线程初始化变量之前启动

时间:2017-01-17 17:23:15

标签: multithreading tensorflow

我正在尝试使用tf.train.batch在多个线程中运行入队图像。当线程数为1时,代码工作正常。但是当我设置更多线程时,我收到一个错误:

Failed precondition: Attempting to use uninitialized value Variable
 [[Node: Variable/read = Identity[T=DT_INT32, _class=["loc:@Variable"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

主线程必须在一秒钟内运行一段时间来索引文件夹数据库并将其放入张量。

我在运行tf.train.bath循环之前尝试使用sess.run([some_image])。在这种情况下,工作人员首先在后台失败并出现相同的错误,之后我会收到我的图像。

我尝试使用time.sleep(),但似乎没有可能延迟工人。

我尝试在批处理中添加依赖项:

g = tf.get_default_graph()
with g.control_dependencies([init_one,init_two]):
    example_batch = tf.train.batch([my_image])

其中init_one和init_two为tf.initialize_all(variables) and tf.initialize_local_variables()

我能找到的最相关的问题是:https://github.com/openai/universe-starter-agent/issues/44

有没有办法让同步工作线程与主线程一起询问,以便它们不会先竞争并消亡?

当将纪元计数器设置为不是None的任何可能的解决方案时,会发生类似且易于重现的变量初始化错误吗?我添加了重现以下错误所需的代码:

def index_the_database(database_path):
    """indexes av4 database and returns two tensors of filesystem path: ligand files, and protein files"""
    ligand_file_list = []
    receptor_file_list = []
    for ligand_file in glob(os.path.join(database_path, "*_ligand.av4")):
        receptor_file = "/".join(ligand_file.split("/")[:-1]) + "/" + ligand_file.split("/")[-1][:4] + '.av4'
        if os.path.exists(receptor_file):
            ligand_file_list.append(ligand_file)
            receptor_file_list.append(receptor_file)

    index_list = range(len(ligand_file_list))
    return index_list,ligand_file_list, receptor_file_list


index_list,ligand_file_list,receptor_file_list = index_the_database(database_path)

ligand_files = tf.convert_to_tensor(ligand_file_list,dtype=tf.string)
receptor_files = tf.convert_to_tensor(receptor_file_list,dtype=tf.string)

filename_queue = tf.train.slice_input_producer([ligand_files,receptor_files],num_epochs=10,shuffle=True)

serialized_ligand = tf.read_file(filename_queue[0])
serialized_receptor = tf.read_file(filename_queue[1])

image_one = tf.reduce_sum(tf.exp(tf.decode_raw(serialized_receptor,tf.float32)))

image_batch = tf.train.batch([image_one],100,num_threads=100)


init_two = tf.initialize_all_variables()
init_one = tf.initialize_local_variables()
sess = tf.Session()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess,coord=coord)
sess.run([init_one])
sess.run([init_two])

while True:
    print "next"
    sess.run([image_batch])

0 个答案:

没有答案
相关问题