(self, obs, action, reward)
update policy model self.model with policy gradient algorithm
            obs is `inputs`
update policy model self.model with policy gradient algorithm
            obs is `inputs`
update policy model self . model with policy gradient algorithm obs is inputs
def define_learn(self, obs, action, reward):
        """
            update policy model self.model with policy gradient algorithm
            obs is `inputs`
        """
        tokens = action[0]
        adjvec = action[1]
        with fluid.unique_name.guard():
            [_, softmax, _, sigmoid] = self.model.policy(obs)
            reshape_softmax = layers.reshape(
                    softmax,
                    [-1, self.model.parser_args.num_tokens])
            reshape_tokens = layers.reshape(tokens, [-1, 1])
            reshape_tokens.stop_gradient = True
            raw_neglogp_to = layers.softmax_with_cross_entropy(
                    soft_label=False,
                    logits=reshape_softmax,
                    label=fluid.layers.cast(x=reshape_tokens, dtype="int64"))

            action_to_shape_sec = self.model.parser_args.num_nodes * 2
            neglogp_to = layers.reshape(fluid.layers.cast(
                                            raw_neglogp_to, dtype="float32"),
                                        [-1, action_to_shape_sec])

            adjvec = layers.cast(x=adjvec, dtype='float32')
            neglogp_ad = layers.sigmoid_cross_entropy_with_logits(
                    x=sigmoid, label=adjvec)

            neglogp = layers.elementwise_add(
                    x=layers.reduce_sum(neglogp_to, dim=1),
                    y=layers.reduce_sum(neglogp_ad, dim=1))
            reward = layers.cast(reward, dtype="float32")
            cost = layers.reduce_mean(
                    fluid.layers.elementwise_mul(x=neglogp, y=reward))
            optimizer = fluid.optimizer.Adam(learning_rate=self.lr)
            train_op = optimizer.minimize(cost)
            return cost
def define_learn ( self , obs , action , reward ) : tokens = action [ 0 ] adjvec = action [ 1 ] with fluid . unique_name . guard ( ) : [ _ , softmax , _ , sigmoid ] = self . model . policy ( obs ) reshape_softmax = layers . reshape ( softmax , [ - 1 , self . model . parser_args . num_tokens ] ) reshape_tokens = layers . reshape ( tokens , [ - 1 , 1 ] ) reshape_tokens . stop_gradient = True raw_neglogp_to = layers . softmax_with_cross_entropy ( soft_label = False , logits = reshape_softmax , label = fluid . layers . cast ( x = reshape_tokens , dtype = "int64" ) ) action_to_shape_sec = self . model . parser_args . num_nodes * 2 neglogp_to = layers . reshape ( fluid . layers . cast ( raw_neglogp_to , dtype = "float32" ) , [ - 1 , action_to_shape_sec ] ) adjvec = layers . cast ( x = adjvec , dtype = 'float32' ) neglogp_ad = layers . sigmoid_cross_entropy_with_logits ( x = sigmoid , label = adjvec ) neglogp = layers . elementwise_add ( x = layers . reduce_sum ( neglogp_to , dim = 1 ) , y = layers . reduce_sum ( neglogp_ad , dim = 1 ) ) reward = layers . cast ( reward , dtype = "float32" ) cost = layers . reduce_mean ( fluid . layers . elementwise_mul ( x = neglogp , y = reward ) ) optimizer = fluid . optimizer . Adam ( learning_rate = self . lr ) train_op = optimizer . minimize ( cost ) return cost