我遇到了我的python程序的某些奇怪行为。基本上,当我尝试创建并填充长度大于1000的SumTree时,我的磁盘使用率增加了很多,达到〜300MB / s,然后程序消失了。
我很确定在此过程中没有文件读写,问题是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实例,当我删除最后一行时,高磁盘使用率消失了。
有人可以帮我吗?
答案 0 :(得分:0)
我最终解决了这个问题,因为每个experience
是namedtuple的元组,并且我正在从中创建另一个namedtuple Experience
。通过将experience
更改为numpy数组的元组来解决。