【问题标题】:Strongly increasing memory consumption when using ELMo from Tensorflow-Hub使用 Tensorflow-Hub 中的 ELMo 时显着增加内存消耗
【发布时间】:2019-10-22 15:19:39
【问题描述】:

我目前正在尝试比较数百万个文档的相似性。对于 CPU 上的第一次测试,我将它们减少到每个大约 50 个字符,并尝试一次获得 10 个字符的 ELMo 嵌入,如下所示:

ELMO = "https://tfhub.dev/google/elmo/2"
for row in file:
    split = row.split(";", 1)
    if len(split) > 1:
        text = split[1].replace("\n", "")
            texts.append(text[:50])
    if i == 300:
        break
    if i % 10 == 0:
        elmo = hub.Module(ELMO, trainable=False)
                 executable = elmo(
                 texts,
                 signature="default",
                 as_dict=True)["elmo"]

    vectors = execute(executable)
    texts = []
    i += 1

然而,即使是这个小例子,在大约 300 个句子(甚至没有保存向量)之后,程序也会消耗高达 12GB 的 RAM。这是一个已知问题(我发现的其他问题暗示了类似的问题,但不是那么极端)还是我犯了错误?

【问题讨论】:

  • 你传入了一个变量sentences,但我们看不到这是在哪里定义的
  • 对不起,我的错。它应该是文本而不是句子(在我的代码中,elmo 部分是它自己的方法的一部分,其中参数称为语句)。我已经编辑了它

标签: python tensorflow tensorflow-hub elmo


【解决方案1】:

我想这是针对没有 Eager 模式的 TensorFlow 1.x(否则使用 hub.Module 可能会遇到更大的问题)。

在该编程模型中,您需要首先在 TensorFlow 图中表达您的计算,然后为每批数据重复执行该图。

  • 使用hub.Module() 构建模块并将其应用于将输入张量映射到输出张量都是图构建的一部分,并且应该只发生一次。

  • 输入数据的循环应该只调用 session.run() 来提供输入并从固定图中获取输出数据。

幸运的是,已经有一个实用函数可以为您完成所有这些工作:

import numpy as np
import tensorflow_hub as hub

# For demo use only. Extend to your actual I/O needs as you see fit.
inputs = (x for x in ["hello world", "quick brown fox"])

with hub.eval_function_for_module("https://tfhub.dev/google/elmo/2") as f:
  for pystr in inputs:
    batch_in = np.array([pystr])
    batch_out = f(batch_in)
    print(pystr, "--->", batch_out[0])

就原始 TensorFlow 而言,这对您的作用大致如下:

module = Module(ELMO_OR_WHATEVER)
tensor_in = tf.placeholder(tf.string, shape=[None])  # As befits `module`.
tensor_out = module(tensor_in)

# This kind of session handles init ops for you.
with tf.train.SingularMonitoredSession() as sess:
  for pystr in inputs:
    batch_in = np.array([pystr])
    batch_out = sess.run(tensor_out, feed_dict={tensor_in: batch_in}
    print(pystr, "--->", batch_out[0])

如果您的需求对于with hub.eval_function_for_module ... 来说过于复杂,您可以构建这个更明确的示例。

注意 hub.Module 是如何在循环中既没有构造也没有调用的。

PS:厌倦了担心构建图表和运行会话?那么 TF2 和 Eager Execution 适合您。查看https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_text_classification.ipynb

【讨论】:

  • 它起作用了,我仍然得到了增加(从 1.5gb 到 2gb),我无法解释,但它似乎更易于管理。它是如何工作的?我认为 python 有自己的“垃圾收集”形式,如果不再引用该对象,它将被删除。这不应该发生吗?
  • 我的第一个答案不完整。现在应该满意多了。
  • 带有 eval_function_for_module 的示例代码对我不起作用:我得到了InternalError: Dst tensor is not initialized. [[{{node checkpoint_initializer_14}}]]
猜你喜欢
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 2020-05-01
相关资源
最近更新 更多