@@ -0,0 +1,2952 @@
+# coding: utf-8%0A%0Afrom __future__ import absolute_import%0Afrom __future__ import division%0Afrom __future__ import print_function%0Afrom __future__ import unicode_literals%0A%0Afrom learners.base import BaseLearner%0Aimport numpy as np%0Aimport chainer%0Aimport chainer.links as L%0Aimport chainer.functions as F%0Afrom chainer import Variable%0Afrom chainer import optimizers%0A%0Aclass QFunction(chainer.Chain):%0A    def __init__(%0A            self,%0A            input_dim=256,%0A            hidden_dim=256,%0A            output_dim=256%0A        ):%0A        super(QFunction, self).__init__(%0A                embed=L.EmbedID(input_dim, hidden_dim),%0A                fc1=L.Linear(hidden_dim, hidden_dim),%0A                fc2=L.Linear(hidden_dim, output_dim),%0A            )%0A%0A    def __call__(self, x):%0A        h = self.embed(x)%0A        h = F.relu(self.fc1(h))%0A        h = self.fc2(h)%0A        return h%0A%0Aclass RLLearner(BaseLearner):%0A    def __init__(%0A            self,%0A            action_count=256,%0A            replay_memory_size=1e4,%0A            initial_eps=0.50,%0A            alpha=0.001,%0A            gamma=0.95,%0A            weight_decay=0.0001,%0A            minibatch_size=256%0A        ):%0A        self.q_function = QFunction()%0A        self.xp = self.q_function.xp%0A        self.optimizer = optimizers.SGD(alpha)%0A        self.optimizer.setup(self.q_function)%0A        self.optimizer.add_hook(chainer.optimizer.WeightDecay(weight_decay))%0A        self.replay_memory = %5B%5D%0A        self.replay_memory_size = replay_memory_size%0A        self.minibatch_size = minibatch_size%0A        self.action_count = action_count%0A        self.initial_eps = initial_eps%0A        self.latest_phi = None%0A        self.latest_action = None%0A        self.latest_reward = None%0A        self.gamma = gamma%0A        self.mode_count = 0%0A        self.exploler = True%0A    %0A    def push_memory(self, x):%0A        if len(self.replay_memory) %3E= self.replay_memory_size:%0A            self.replay_memory.pop(0)%0A        self.replay_memory.append(x)%0A    %0A    def draw_memory(self, max_count):%0A        l = len(self.replay_memory)%0A        if l %3C= max_count:%0A            return self.replay_memory%0A        else:%0A            return %5Bself.replay_memory%5Bi%5D for i in self.xp.random.choice(l, max_count)%5D%0A    %0A    def update_with_minibatch(self):%0A        minibatch = self.draw_memory(self.minibatch_size)%0A        if len(minibatch) %3E 0:%0A            minibatch_size = len(minibatch)%0A            minibatch = self.xp.asarray(minibatch, dtype='float32')%0A            phi_j = self.xp.asarray(minibatch%5B:, 0%5D, dtype='int32')%0A            a_j = self.xp.asarray(minibatch%5B:, 1%5D, dtype='int32')%0A            r_j = minibatch%5B:, 2%5D%0A            phi_jj = self.xp.asarray(minibatch%5B:, 3%5D, dtype='int32')%0A            y = r_j + self.gamma*F.max(self.q_function(phi_jj), axis=1)%0A            q_j = self.q_function(phi_j)%0A            t = (self.xp.arange(0, q_j.data.shape%5B1%5D)%5BNone, :%5D == a_j%5B:, None%5D)%0A            loss = F.sum((y - F.sum(q_j*t, axis=1))**2)/ minibatch_size%0A

Create rl_learner.py