From 55c227b3458a076a44d8eeb552a139937100b3f9 Mon Sep 17 00:00:00 2001 From: TomorrowIsAnOtherDay <2466956298@qq.com> Date: Wed, 17 Jun 2020 19:35:50 +0800 Subject: [PATCH] add docs --- docs/zh_CN/Overview.md | 8 +++---- docs/zh_CN/tutorial/module.md | 39 +++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/docs/zh_CN/Overview.md b/docs/zh_CN/Overview.md index 5a3f15c..b59cf82 100644 --- a/docs/zh_CN/Overview.md +++ b/docs/zh_CN/Overview.md @@ -40,10 +40,10 @@ diff --git a/docs/zh_CN/tutorial/module.md b/docs/zh_CN/tutorial/module.md index 7df5154..fbe9958 100644 --- a/docs/zh_CN/tutorial/module.md +++ b/docs/zh_CN/tutorial/module.md @@ -7,7 +7,7 @@ ## Model - 定义:`Model` 用来定义前向(Forward)网络,这通常是一个策略网络(Policy Network)或者一个值函数网络(Value Function),输入是当前环境状态(State)。 -- **注意事项**:用户得要继承`parl.Model`这个类来构建自己的Model。 +- **⚠️注意事项**:用户得要继承`parl.Model`这个类来构建自己的Model。 - 需要实现的函数: - forward: 根据在初始化函数中声明的计算层来搭建前向网络。 - 备注:在PARL中,实现强化学习常用的target很方便的,直接通过deepcopy即可。 @@ -20,10 +20,12 @@ class CartpoleModel(parl.Model): def __init__(self): self.fc1 = layers.fc(size=10, act='tanh') self.fc2 = layers.fc(size=2, act='softmax') + def forward(self, obs): out = self.fc1(obs) out = self.fc2(out) return out + if __name__ == '__main__: model = CartpoleModel() target_model = deepcopy.copy(model) @@ -34,7 +36,7 @@ if __name__ == '__main__: ## Algorithm - 定义:`Algorithm` 定义了具体的算法来更新前向网络(Model),也就是通过定义损失函数来更新Model。一个Algorithm包含至少一个Model。 -- **注意事项**:一般不自己开发,推荐直接import 仓库中已经实现好的算法。 +- **⚠️注意事项**:一般不自己开发,推荐直接import 仓库中已经实现好的算法。 - 需要实现的函数(`开发新算法才需要`): - learn: 根据训练数据(观测量和输入的reward),定义损失函数,用于更新Model中的参数。 - predict: 根据当前的观测量,给出动作概率或者Q函数的预估值。 @@ -47,9 +49,9 @@ alg = parl.algorithms.PolicyGradient(model, lr=1e-3) ## Agent - 定义:`Agent` 负责算法与环境的交互,在交互过程中把生成的数据提供给Algorithm来更新模型(Model),数据的预处理流程也一般定义在这里。 -- **注意事项**:需要继承`parl.Agent`来使用,要在构造函数中调用父类的构造函数。 +- **⚠️注意事项**:需要继承`parl.Agent`来使用,要在构造函数中调用父类的构造函数。 - 需要实现的函数: - - build_program: 定义paddle的program。用户通常在这里定义两个program:一个用于训练(在learn函数中调用),一个用于预测(用于sample、predict函数中)。 + - build_program: 定义paddle的program。用户通常在这里定义两个program:一个用于训练(在learn函数中调用),一个用于预测(用于sample、predict函数中)注意⚠️:这个函数会自动被调用,用户无需关注。 - learn: 根据输入的训练数据,更新模型参数。 - predict: 根据输入的观测量,返回要执行的动作(action) - sample: 根据输入的观测量,返回要执行的动作,这个一般是添加了噪声的,用于探索用的。 @@ -77,36 +79,37 @@ class CartpoleAgent(parl.Agent): reward = layers.data(name='reward', shape=[], dtype='float32') self.cost = self.alg.learn(obs, act, reward) - def sample(self, obs): + def learn(self, obs, act, reward): + act = np.expand_dims(act, axis=-1) + feed = { + 'obs': obs.astype('float32'), + 'act': act.astype('int64'), + 'reward': reward.astype('float32') + } + cost = self.fluid_executor.run( + self.train_program, feed=feed, fetch_list=[self.cost])[0] + return cost + + def predict(self, obs): obs = np.expand_dims(obs, axis=0) act_prob = self.fluid_executor.run( self.pred_program, feed={'obs': obs.astype('float32')}, fetch_list=[self.act_prob])[0] act_prob = np.squeeze(act_prob, axis=0) - act = np.random.choice(range(self.act_dim), p=act_prob) + act = np.argmax(act_prob) return act - def predict(self, obs): + def sample(self, obs): obs = np.expand_dims(obs, axis=0) act_prob = self.fluid_executor.run( self.pred_program, feed={'obs': obs.astype('float32')}, fetch_list=[self.act_prob])[0] act_prob = np.squeeze(act_prob, axis=0) - act = np.argmax(act_prob) + act = np.random.choice(range(self.act_dim), p=act_prob) return act - def learn(self, obs, act, reward): - act = np.expand_dims(act, axis=-1) - feed = { - 'obs': obs.astype('float32'), - 'act': act.astype('int64'), - 'reward': reward.astype('float32') - } - cost = self.fluid_executor.run( - self.train_program, feed=feed, fetch_list=[self.cost])[0] - return cost if __name__ == '__main__': model = CartpoleModel() alg = parl.algorithms.PolicyGradient(model, lr=1e-3) -- GitLab