【发布时间】:2020-02-05 07:49:05
【问题描述】:
我正在尝试在字典中为我尝试制作的 AI 创建一个 q-table,但是在将大约 40,000,000 个可能的位置输入到 q-table(字典)后尝试制作字典时,该过程开始真正减速到大约 80,000,000 并且像蜗牛一样慢(大约需要 18 小时才能达到 80,000,000)并且似乎一直在减速。 我想知道是否有办法以某种方式优化我的字典或我的代码来加速这个过程,因为按照这个速度,完成 q-table 的创建需要一年的时间(大约 160,000,000 个职位)在 q 表上)。
如果有帮助,这是我的代码:
start_q_table = None
if start_q_table is None:
q_table = {}
# All possible height differences between the bird and the bottom pipe
for i in range(-display_height, display_height):
# ^^^ = -800 ^^^ = 800
# All possible distances between the bird and the end of the nearest pipe
for ii in range(-bird_size, display_height + pipe_distance):
# ^^^ = 15 ^^^ = ~ 1000 total
# Bird speed
for iii in speed_range:
# ^^^ = range(1000)
q_table[(i, ii, iii)] = [np.random.uniform(-1, 0) for i in range(3)]
【问题讨论】:
-
好像是4D数组。我认为您应该使用
list或ndarray列表,然后使用线程...? -
np.random.unifor(-1, 0, size=3)如果您对字典值是 ndarrays 没问题,可能会稍微快一些 -
GyuHyeon Choi:我愿意,但这样做会更容易,并且会长期帮助我,所以我不必每次都担心列表。
-
如果您需要使用字典来存储值,您还可以考虑使用 Cython 或 Numba 使循环迭代非常快(或绕过 GIL)。
标签: python dictionary