Tensorflow:具有num_parallel_calls的数据集映射没有加速 - python

我正在使用TensorFlow和tf.data.Dataset API执行一些文本预处理。在我的num_parallel_calls调用中不使用dataset.map时,预处理10K记录需要0.03s。

当我使用num_parallel_trials=8(计算机上的内核数)时,也需要0.03s来预处理10K记录。

我四处搜寻,发现了以下内容:Parallelism isn't reducing the time in dataset map

它们表明您需要使用TensorFlow操作来查看加速。事情是这样的:我只使用TensorFlow操作。具体来说,我正在映射此函数:

def preprocess(self, x, data_table):
    x['reviews'] = tf.string_split(x['reviews'], delimiter=' ')
    x['reviews'] = tf.sparse_tensor_to_dense(x['reviews'], default_value=' ')
    x['reviews'] = tf.cast(data_table.lookup(x['reviews']), tf.int32)
    nbatch = tf.cast(tf.shape(x['reviews'])[0], tf.int32)
    nseq = tf.cast(tf.shape(x['reviews'])[1], tf.int32)
    padding = tf.cond(tf.less(nseq, 100),
                      lambda: 0 * tf.ones([nbatch, 100 - nseq], tf.int32),
                      lambda: 0 * tf.ones([nbatch, 0], tf.int32))
    x['reviews'] = tf.concat((x['reviews'], padding), axis=1)[:, :100]
    x['reviews'].set_shape([None, 100])
    return x

知道为什么我看不到加速吗?

谢谢!

参考方案

我的第一个假设是对lambda的调用会降低您的速度,因为每个迭代和内核的延迟初始化都是如此。根据这个网址,他在速度和核心使用上也有类似的问题。 Is there a way to use tensorflow map_fn on GPU?
我几乎是一个张量和管道的初学者,但是稍后我将在可以访问计算机时进行调查,我想知道在哪里运行了哪些可执行文件。

当使用tf-tutorials运行时发生了:AttributeError:模块'tensorflow.python.estimator.api.estimator'没有属性'SessionRunHook' - python

当它在google.colab中正常运行时,我经常使用有关估计器的tensorflow官方教程。我使用的环境是win10-64bit&tensorflow-gpu == 1.12.0&python == 3.6.6。import tensorflow as tf import tensorflow.feature_column as fc import os…

重命名TensorFlow估算器中的功能 - python

我正在使用固定的估算器,并从无法控制的来源读取数据。源代码使用snake_case存储功能,而我提供的预测功能始终在camelCase中,这也是我无法控制的。粗略地讲,我训练模型并将其导出,因此稍后可以阅读:features = ... # snake_case estimator = tf.estimator(DNNClassifier(feature_c…

Tensorflow到ONNX的转换 - python

我目前正在尝试将通过本教程(https://github.com/thtrieu/darkflow)创建的已保存(正在工作)的.pb文件转换为onnx文件。我目前正在使用winML工具,但是转换的结果根本不起作用(输入参数错误+整个体系结构不正确)。我的输入参数(在自述文件的最底部指定):input:0输出节点:输出:0我想在HoloLens上运行的UWP应…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

有没有一种方法可以有效地矢量化图像上的Tensorflow操作? - python

Tensorflow有大量的变换,可以应用于表示图像([高度,宽度,深度])(例如tf.image.rot90()或tf.image.random_flip_left_right())的3D张量。我知道它们应与队列一起使用,因此它们只能在一个图像上运行。但是,是否有一种方法可以对操作进行矢量化处理,以将4D张量([batch_size,height,widt…