【问题标题】:Python - high disk usage in SumTreePython - SumTree 中的高磁盘使用率
【发布时间】:2018-07-12 01:00:56
【问题描述】:

我的 python 程序遇到了一些奇怪的行为。基本上,当我尝试创建并填充长度大于 1000 的 SumTree 时,我的磁盘使用量增加了很多,达到了 ~300MB/s,然后程序就死了。

我很确定此过程中不涉及文件 r/w,问题出在 add 函数上。代码如下所示。

import numpy as np

class SumTree():

    trans_idx = 0

    def __init__(self, capacity):
        self.num_samples = 0
        self.capacity = capacity
        self.tree = np.zeros(2 * capacity - 1)
        self.transitions = np.empty(self.capacity, dtype=object)

    def add(self, p, experience):
        tree_idx = self.trans_idx + self.capacity - 1
        self.transitions[self.trans_idx] = experience
        self.transitions.append(experience)
        self.update(tree_idx, p)

        self.trans_idx += 1
        if self.trans_idx >= self.capacity:
            self.trans_idx = 0

        self.num_samples = min(self.num_samples + 1, self.capacity)

    def update(self, tree_idx, p):
        diff = p - self.tree[tree_idx]
        self.tree[tree_idx] = p
        while tree_idx != 0:
            tree_idx = (tree_idx - 1) // 2
            self.tree[tree_idx] += diff

    def get_leaf(self, value):
        parent_idx = 0
        while True:  
            childleft_idx = 2 * parent_idx + 1  
            childright_idx = childleft_idx + 1
            if childleft_idx >= len(self.tree):  
                leaf_idx = parent_idx
                break
            else:  
                if value <= self.tree[childleft_idx]:
                    parent_idx = childleft_idx
                else:
                    value -= self.tree[childleft_idx]
                    parent_idx = childright_idx

        data_idx = leaf_idx - self.capacity + 1
        return leaf_idx, self.tree[leaf_idx], self.transitions[data_idx]

    @property
    def total_p(self):
        return self.tree[0]  # the root

    @property
    def volume(self):
        return self.num_samples  # number of transistions stored

这是一个将使用此 SumTree 对象的示例:

def add(self, experience)
    max_p = np.max(self.tree.tree[-self.tree.capacity:])
    if max_p == 0:
        max_p = 1.0
    exp = self.Experience(*experience)
    self.tree.add(max_p, exp)  

其中Experience 是一个命名元组,self.tree 是一个 Sumtree 实例,当我删除最后一行时,高磁盘使用率消失了。

谁能帮我解决这个问题?

【问题讨论】:

  • 查看你的内存使用情况。听起来你可能会用尽内存(导致交换文件增长),直到程序崩溃。
  • 内存还可以(~100MB),我发现是namedtuple问题

标签: python deep-learning reinforcement-learning q-learning disk-access


【解决方案1】:

我终于解决了这个问题,因为每个 experience 都是 namedtuple 的元组,我正在从中创建另一个 namedtuple Experience。通过将 experience 更改为 numpy 数组的元组来修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-24
    • 2018-02-11
    • 2017-11-25
    • 2015-03-23
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多