Created
November 18, 2020 17:52
-
-
Save pythonlessons/89758604ce4748d138ae37a3f93ff3de to your computer and use it in GitHub Desktop.
BipedalWalker-v3_continuous_actor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Actor_Model: | |
| def __init__(self, input_shape, action_space, lr, optimizer): | |
| X_input = Input(input_shape) | |
| self.action_space = action_space | |
| X = Dense(512, activation="relu", kernel_initializer=tf.random_normal_initializer(stddev=0.01))(X_input) | |
| X = Dense(256, activation="relu", kernel_initializer=tf.random_normal_initializer(stddev=0.01))(X) | |
| X = Dense(64, activation="relu", kernel_initializer=tf.random_normal_initializer(stddev=0.01))(X) | |
| output = Dense(self.action_space, activation="tanh")(X) | |
| self.Actor = Model(inputs = X_input, outputs = output) | |
| self.Actor.compile(loss=self.ppo_loss_continuous, optimizer=optimizer(lr=lr)) | |
| #print(self.Actor.summary()) | |
| def ppo_loss_continuous(self, y_true, y_pred): | |
| advantages, actions, logp_old_ph, = y_true[:, :1], y_true[:, 1:1+self.action_space], y_true[:, 1+self.action_space] | |
| LOSS_CLIPPING = 0.2 | |
| logp = self.gaussian_likelihood(actions, y_pred) | |
| ratio = K.exp(logp - logp_old_ph) | |
| p1 = ratio * advantages | |
| p2 = tf.where(advantages > 0, (1.0 + LOSS_CLIPPING)*advantages, (1.0 - LOSS_CLIPPING)*advantages) # minimum advantage | |
| actor_loss = -K.mean(K.minimum(p1, p2)) | |
| return actor_loss | |
| def gaussian_likelihood(self, actions, pred): # for keras custom loss | |
| log_std = -0.5 * np.ones(self.action_space, dtype=np.float32) | |
| pre_sum = -0.5 * (((actions-pred)/(K.exp(log_std)+1e-8))**2 + 2*log_std + K.log(2*np.pi)) | |
| return K.sum(pre_sum, axis=1) | |
| def predict(self, state): | |
| return self.Actor.predict(state) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment