以下脚本执行非常慢。我只想计算 twitter-follwer-graph 中的总行数(约 26 GB 的文本文件)。
我需要执行机器学习任务。这只是通过tensorflow从hdfs访问数据的测试。
import tensorflow as tf
import time
filename_queue = tf.train.string_input_producer(["hdfs://default/twitter/twitter_rv.net"], num_epochs=1, shuffle=False)
def read_filename_queue(filename_queue):
reader = tf.TextLineReader()
_, line = reader.read(filename_queue)
return line
line = read_filename_queue(filename_queue)
session_conf = tf.ConfigProto(intra_op_parallelism_threads=1500,inter_op_parallelism_threads=1500)
with tf.Session(config=session_conf) as sess:
sess.run(tf.initialize_local_variables())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
start = time.time()
i = 0
while True:
i = i + 1
if i%100000 == 0:
print(i)
print(time.time() - start)
try:
sess.run([line])
except tf.errors.OutOfRangeError:
print('end of file')
break
print('total number of lines = ' + str(i))
print(time.time() - start)
处理前 100000 行大约需要 40 秒。
我尝试将 intra_op_parallelism_threads
和 inter_op_parallelism_threads
设置为 0、4、8、40、400 和 1500。但这并没有显着影响执行时间......
你能帮帮我吗?
系统规范:
- 16 GB 内存
- 4 个 CPU 核心
最佳答案
您可以将大文件拆分成较小的文件,这可能会有所帮助。并将intra_op_parallelism_threads和inter_op_parallelism_threads设置为0;
对于许多系统来说,多进程读取单个原始文本文件并不容易,tensorflow 只用一个线程读取一个文件,因此调整 tensorflow 线程无济于事。 Spark 可以用多线程处理文件,因为它将文件分成 block ,每个线程读取 block 中行的内容并忽略第一个 \n
之前的字符,因为它们属于最后一个 block 的最后一行.对于批量数据处理,Spark 是更好的选择,而 TensorFlow 更适合机器学习/深度学习任务;
关于python - 如何加速我在 hadoop 上的 tensorflow 执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44546379/