# Best way to export a keras model from RLlib

**URL:** <https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086>\
**Category:** RLlib\
**Created:** [May 7, 2021, 10:34am UTC](https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086 "2021-05-07T10:34:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![javigm98](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javigm98/32/704_2.png) [@javigm98](https://discuss.ray.io/u/javigm98)\
**Post date:** [May 7, 2021, 10:34am UTC](https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086/1 "2021-05-07T10:34:48Z")

</div>

Hi all! I have executed many training iterations with a PPO agent and now I want to export ONLY the underlying keras model that contains the trained neural network, so that I can eveluate its performance outside RLlib (only as a keras model with `model.predict()`) Is there any way to export only this part of the Tensorflow graph (maybe as a h5 file). I want to get something that later can be restored to a keras model with `tf.keras.model.load_model('model.h5')`.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)\
**Post date:** [May 13, 2021, 7:27pm UTC](https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086/2 "2021-05-13T19:27:37Z")

</div>

Have you tried:

```auto
Trainer.get_policy().export_model([export dir])

```

?  
This will create a tf1.x snapshot of the model’s tf static graph and you should be able to re-load it later into a new session (just the model).

---

<div class="post-metadata">

**Author:** ![javigm98](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/javigm98/32/704_2.png) [@javigm98](https://discuss.ray.io/u/javigm98)\
**Post date:** [May 13, 2021, 10:57pm UTC](https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086/3 "2021-05-13T22:57:34Z")

</div>

The problem by doing so was that the whole TF Graph was saved, and it contained many nodes that were useless for my purpose… What I wanted to export was only the part of the graph containing the keras model, in order to be able to load it later with `tf.keras.models.load_model('model.h5')`.

I tried to achieve this by doing:

```auto
agent = ppo.PPOTrainer(config, env='Pong-v0')
agent.restore(checkpoint_dir)

with agent.get_policy().get_session().graph.as_default():
    export_model = agent.get_policy().model.base_model.save('model.h5')

```

but the weights saved seemed to be different form those that the model have (and that were accesible via `agent.get_policy().get_weights()` function).

---

<div class="post-metadata">

**Author:** ![Ke\_Fan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/ke_fan/32/3258_2.png) [@Ke\_Fan](https://discuss.ray.io/u/Ke_Fan)\
**Post date:** [November 16, 2022, 7:04pm UTC](https://discuss.ray.io/t/best-way-to-export-a-keras-model-from-rllib/2086/4 "2022-11-16T19:04:55Z")

</div>

Hello.

the .get\_policy() method always returns None, and when I call export\_model(), It gave me AttributeError: ‘NoneType’ object has no attribute ‘export\_model’. Can you help me?

I use the basic example “3DBall” of Unity3D
