Merge pull request #204 from NileshPS/master

Ch 16. Using native python list instead of deque for storing experience, increases sampling performance considerably.
main
Aurélien Geron 2018-05-09 15:34:12 +02:00 committed by GitHub
commit c6b424931a
1 changed files with 23 additions and 2 deletions

View File

@ -1558,14 +1558,35 @@
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from collections import deque\n",
"\n",
"\n",
"class ReplayMemory:\n",
" \n",
" def __init__(self, size):\n",
" self.size = size\n",
" self.buf= [None] * self.size\n",
" self.index, self.count = 0, 0\n",
" \n",
" def append(self, data):\n",
" self.buf[self.index] = data\n",
" self.count = min(self.count + 1, self.size)\n",
" self.index = (self.index + 1) % self.size\n",
" \n",
" def __getitem__(self, idx):\n",
" return self.buf[idx]\n",
" \n",
" def __len__(self):\n",
" return self.count\n",
"\n",
"\n",
" \n",
"replay_memory_size = 500000\n",
"replay_memory = deque([], maxlen=replay_memory_size)\n",
"replay_memory = ReplayMemory(replay_memory_size)\n",
"\n",
"def sample_memories(batch_size):\n",
" indices = np.random.permutation(len(replay_memory))[:batch_size]\n",