如何降低使用 deeplearning4j(内存映射文件和 WorkspaceConfiguration)的程序中的 RAM 使用率?

How can I reduce the RAM utilization in my program that use deeplearning4j (Memory-mapped files and WorkspaceConfiguration)?

我正在使用 deeplearning4j,但是当我加载用于文本分类的预训练模型时,我的电脑上没有足够的内存。

我尝试更改 eclipse.ini 文件并添加更多内存更改 Xms 和 Xmx。不幸的是,它对我不起作用。

https://deeplearning4j.org/docs/latest/deeplearning4j-config-memory

在这个 link 中似乎有一个使用更少 RAM 的可能解决方案,即使它花费更多的时间,但我现在不在乎。

来自 link:

Memory-mapped files ND4J supports the use of a memory-mapped file instead of RAM when using the nd4j-native backend. On one hand, it’s slower then RAM, but on other hand, it allows you to allocate memory chunks in a manner impossible otherwise.

我可以在这样的代码中添加它吗(遵循 link)?

https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/nlp/word2vec/Word2VecRawTextExample.java

如果有其他方法(或更好的方法)当然可以写。我将不胜感激任何建议。

提前致谢。

我来自 deeplearning4j 项目。内存映射工作区是为嵌入而设计的,是的,应该被视为与我们的堆外内存不同的概念。堆外内存是一个概念上的兔子洞,我不会在这里涉及(你必须了解 JVM 并且主题与这里无关)

您必须使用内存映射工作区的方法是将 word2vec 加载到内存映射范围内。 第一个组件是配置:

import org.nd4j.linalg.api.memory.MemoryWorkspace;
import org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration;
import org.nd4j.linalg.api.memory.enums.LocationPolicy;
WorkspaceConfiguration mmap = WorkspaceConfiguration.builder()
            .initialSize(initialSize)
            .policyLocation(LocationPolicy.MMAP)
            .build();

try (MemoryWorkspace ws =   
           Nd4j.getWorkspaceManager().getAndActivateWorkspace(mmap)) {
 //load your word2vec here            

} 

内存映射工作区值得注意的是它应该如何使用。 Mem map 仅用于访问大型数组并将其子集从 ram 中拉出。 您应该只使用它来提取训练所需的词向量子集。

当使用 word2vec(或任何其他嵌入技术)时,典型的模式是仅查找所需的词向量并将它们合并到一个小批量中。 该小批量(以及相关的培训)应该在一个单独的工作区中进行(或者让它不附加,这是默认设置)。您可以不附加它的原因是我们已经在 ComputationGraph 和 MultiLayerNetwork 中为您进行了工作区和其他相关优化。只要确保传递任何你需要适应的东西。

从那里,使用 INDArray get(..) 和 put(..) 方法将您需要的行复制到您应该用于训练的另一个数组中。 有关更多信息,请参阅:https://deeplearning4j.org/docs/latest/nd4j-overview

有关更多信息,请查看 INDArray javadoc 中的 leverage、leverageTo、detach、..: https://deeplearning4j.org/api/latest/org/nd4j/linalg/api/ndarray/INDArray.html