gpt4 book ai didi

python - deque的大小如何影响Tensorflow模型的训练速度?

转载 作者:行者123 更新时间:2023-11-30 09:44:34 25 4
gpt4 key购买 nike

我已经使用 tensorflow 模型多次运行了完全相同的 python 脚本。只有一处不同。一半脚本使用大小为 500000 的双端队列,另一半使用大小为 1000000 的双端队列作为经验存储。从存储中随机选择一批 512 个样本进行训练。

网络仍在训练中,但双端队列为 500k 的网络到目前为止速度快了约 10%。看来距离越来越远了。当然,从更大的内存中随机选择 512 可能需要更长的时间。但是整个训练过程的 10% 呢?

代码如下所示

...
self.memory = deque(maxlen=1000000)
...
def experience_replay(self):
if len(self.memory) < 512:
return
batch = random.sample(self.memory, 512)
state_batch = np.zeros((512, 6, 7, 2))
q_values_batch = np.zeros((512, 7))
idx = 0
for state, action, reward, state_next, terminal in batch:
q_update = reward
if not terminal:
state_next = np.expand_dims(state_next, axis=0)
q_update = (reward + 0.95 * np.amax(self.model.predict(state_next)[0]))
state = np.expand_dims(state, axis=0)
q_values = self.model.predict(state)
q_values[0][action] = q_update
state_batch[idx, ...] = state
q_values_batch[idx, ...] = q_values
idx = idx + 1

self.model.fit(state_batch, q_values_batch, verbose=0)
self.exploration_rate *= 0.9998
self.exploration_rate = max(0.01, self.exploration_rate)

什么可能导致这种加速?

最佳答案

FWIW、deque()random.sample() 不能有效地协同工作。双端队列对于端点附近的访问速度很快,但对于中间的索引查找来说时间复杂度为 O(n)。这是有问题的,因为random.sample()会对双端队列进行重复的索引查找。问题中显示的代码片段如果有一个列表会更好。

您最初问题的准确答案是从 n 长度的双端队列中提取 k 个样本所需的时间与 k * n/4 成正比。从算法上来说,大小加倍通常会使运行时间的预期值加倍。

关于python - deque的大小如何影响Tensorflow模型的训练速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com