# RLLIB Custom\_keras\_model

**URL:** <https://discuss.ray.io/t/rllib-custom-keras-model/6945>\
**Category:** RLlib\
**Created:** [July 24, 2022, 7:44pm UTC](https://discuss.ray.io/t/rllib-custom-keras-model/6945 "2022-07-24T19:44:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seyar\_Barez](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/seyar_barez/32/2422_2.png) [@Seyar\_Barez](https://discuss.ray.io/u/Seyar_Barez)\
**Post date:** [July 24, 2022, 7:44pm UTC](https://discuss.ray.io/t/rllib-custom-keras-model/6945/1 "2022-07-24T19:44:52Z")

</div>

Hi i run this custom\_keras\_model.py example code with  
os: ubuntu 18.04  
ray 1.13

here you rllib github code:  
“”“Example of using a custom ModelV2 Keras-style model.”“”

import argparse  
import os

import ray  
from ray import tune  
from ray.rllib.agents.dqn.distributional\_q\_tf\_model import DistributionalQTFModel  
from ray.rllib.models import ModelCatalog  
from ray.rllib.models.tf.misc import normc\_initializer  
from ray.rllib.models.tf.tf\_modelv2 import TFModelV2  
from ray.rllib.models.tf.visionnet import VisionNetwork as MyVisionNetwork  
from ray.rllib.policy.sample\_batch import DEFAULT\_POLICY\_ID  
from ray.rllib.utils.framework import try\_import\_tf  
from ray.rllib.utils.metrics.learner\_info import LEARNER\_INFO, LEARNER\_STATS\_KEY

tf1, tf, tfv = try\_import\_tf()

parser = argparse.ArgumentParser()  
parser.add\_argument(  
“–run”, type=str, default=“DQN”, help=“The RLlib-registered algorithm to use.”  
)  
parser.add\_argument(“–stop”, type=int, default=200)  
parser.add\_argument(“–use-vision-network”, action=“store\_true”)  
parser.add\_argument(“–num-cpus”, type=int, default=0)

class MyKerasModel(TFModelV2):  
“”“Custom model for policy gradient algorithms.”“”

```
def __init__ (self, obs_space, action_space, num_outputs, model_config, name):
    super(MyKerasModel, self). __init__ (
        obs_space, action_space, num_outputs, model_config, name
    )
    self.inputs = tf.keras.layers.Input(shape=obs_space.shape, name="observations")
    layer_1 = tf.keras.layers.Dense(
        128,
        name="my_layer1",
        activation=tf.nn.relu,
        kernel_initializer=normc_initializer(1.0),
    )(self.inputs)
    layer_out = tf.keras.layers.Dense(
        num_outputs,
        name="my_out",
        activation=None,
        kernel_initializer=normc_initializer(0.01),
    )(layer_1)
    value_out = tf.keras.layers.Dense(
        1,
        name="value_out",
        activation=None,
        kernel_initializer=normc_initializer(0.01),
    )(layer_1)
    self.base_model = tf.keras.Model(self.inputs, [layer_out, value_out])

def forward(self, input_dict, state, seq_lens):
    model_out, self._value_out = self.base_model(input_dict["obs"])
    return model_out, state

def value_function(self):
    return tf.reshape(self._value_out, [-1])

def metrics(self):
    return {"foo": tf.constant(42.0)}

```

class MyKerasQModel(DistributionalQTFModel):  
“”“Custom model for DQN.”“”

```
def __init__ (self, obs_space, action_space, num_outputs, model_config, name, **kw):
    super(MyKerasQModel, self). __init__ (
        obs_space, action_space, num_outputs, model_config, name, **kw
    )

    # Define the core model layers which will be used by the other
    # output heads of DistributionalQModel
    self.inputs = tf.keras.layers.Input(shape=obs_space.shape, name="observations")
    layer_1 = tf.keras.layers.Dense(
        128,
        name="my_layer1",
        activation=tf.nn.relu,
        kernel_initializer=normc_initializer(1.0),
    )(self.inputs)
    layer_out = tf.keras.layers.Dense(
        num_outputs,
        name="my_out",
        activation=tf.nn.relu,
        kernel_initializer=normc_initializer(1.0),
    )(layer_1)
    self.base_model = tf.keras.Model(self.inputs, layer_out)

# Implement the core forward method.
def forward(self, input_dict, state, seq_lens):
    model_out = self.base_model(input_dict["obs"])
    return model_out, state

def metrics(self):
    return {"foo": tf.constant(42.0)}

```

if **name** == “ **main** ”:  
args = parser.parse\_args()  
ray.init(num\_cpus=args.num\_cpus or None)  
ModelCatalog.register\_custom\_model(  
“keras\_model”, MyVisionNetwork if args.use\_vision\_network else MyKerasModel  
)  
ModelCatalog.register\_custom\_model(  
“keras\_q\_model”, MyVisionNetwork if args.use\_vision\_network else MyKerasQModel  
)

```
# Tests https://github.com/ray-project/ray/issues/7293
def check_has_custom_metric(result):
    r = result["result"]["info"][LEARNER_INFO]
    if DEFAULT_POLICY_ID in r:
        r = r[DEFAULT_POLICY_ID].get(LEARNER_STATS_KEY, r[DEFAULT_POLICY_ID])
    assert r["model"]["foo"] == 42, result

if args.run == "DQN":
    extra_config = {"learning_starts": 0}
else:
    extra_config = {}

tune.run(
    args.run,
    stop={"episode_reward_mean": args.stop},
    config=dict(
        extra_config,
        **{
            "env": "BreakoutNoFrameskip-v4"
            if args.use_vision_network
            else "CartPole-v0",
            # Use GPUs iff `RLLIB_NUM_GPUS` env var set to > 0.
            "num_gpus": int(os.environ.get("RLLIB_NUM_GPUS", "0")),
            "callbacks": {
                "on_train_result": check_has_custom_metric,
            },
            "model": {
                "custom_model": "keras_q_model"
                if args.run == "DQN"
                else "keras_model"
            },
            "framework": "tf",
        }
    ),
)

```

* * *

I get this error:  
(gym) sb@DES:~/Lab$ python3 custom\_keras.py --run PPO --num-cpus 6  
2022-07-24 21:38:26,178 INFO services.py:1476 – View the Ray dashboard at [http://127.0.0.1:8265](http://127.0.0.1:8265)  
(PPOTrainer pid=14368) 2022-07-24 21:38:33,018 INFO trainer.py:2333 – Your framework setting is ‘tf’, meaning you are using static-graph mode. Set framework=‘tf2’ to enable eager execution with tf2.x. You may also then want to set eager\_tracing=True in order to reach similar execution speed as with static-graph mode.  
(PPOTrainer pid=14368) 2022-07-24 21:38:33,018 INFO ppo.py:415 – In multi-agent mode, policies will be optimized sequentially by the multi-GPU optimizer. Consider setting simple\_optimizer=True if this doesn’t work for you.  
(PPOTrainer pid=14368) 2022-07-24 21:38:33,019 WARNING deprecation.py:47 – DeprecationWarning: `callbacks dict interface` has been deprecated. Use `a class extending rllib.agents.callbacks.DefaultCallbacks` instead. This will raise an error in the future!  
(PPOTrainer pid=14368) 2022-07-24 21:38:33,019 INFO trainer.py:906 – Current log\_level is WARN. For more information, set ‘log\_level’: ‘INFO’ / ‘DEBUG’ or use the -v and -vv flags.  
(RolloutWorker pid=14440) 2022-07-24 21:38:37,191 WARNING deprecation.py:47 – DeprecationWarning: `callbacks dict interface` has been deprecated. Use `a class extending rllib.agents.callbacks.DefaultCallbacks` instead. This will raise an error in the future!  
(PPOTrainer pid=14368) 2022-07-24 21:38:37,257 ERROR worker.py:451 – Exception raised in creation task: The actor died because of an error raised in its creation task, ray::PPOTrainer. **init** () (pid=14368, ip=172.27.130.120, repr=PPOTrainer)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/agents/trainer.py”, line 1074, in \_init  
(PPOTrainer pid=14368) raise NotImplementedError  
(PPOTrainer pid=14368) NotImplementedError  
(PPOTrainer pid=14368)  
(PPOTrainer pid=14368) During handling of the above exception, another exception occurred:  
(PPOTrainer pid=14368)  
(PPOTrainer pid=14368) ray::PPOTrainer. **init** () (pid=14368, ip=172.27.130.120, repr=PPOTrainer)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/agents/trainer.py”, line 871, in **init**  
(PPOTrainer pid=14368) config, logger\_creator, remote\_checkpoint\_dir, sync\_function\_tpl  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/tune/trainable.py”, line 156, in **init**  
(PPOTrainer pid=14368) self.setup(copy.deepcopy(self.config))  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/agents/trainer.py”, line 957, in setup  
(PPOTrainer pid=14368) logdir=self.logdir,  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/worker\_set.py”, line 144, in **init**  
(PPOTrainer pid=14368) lambda p, pid: (pid, p.observation\_space, p.action\_space)  
(PPOTrainer pid=14368) ray.exceptions.RayActorError: The actor died because of an error raised in its creation task, ray::RolloutWorker. **init** () (pid=14440, ip=172.27.130.120, repr=\<ray.rllib.evaluation.rollout\_worker.RolloutWorker object at 0x7f64865492e8\>)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 634, in **init**  
(PPOTrainer pid=14368) seed=seed,  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 1789, in \_build\_policy\_map  
(PPOTrainer pid=14368) name, orig\_cls, obs\_space, act\_space, conf, merged\_conf  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/policy\_map.py”, line 141, in create\_policy  
(PPOTrainer pid=14368) observation\_space, action\_space, merged\_config  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/tf\_policy\_template.py”, line 270, in **init**  
(PPOTrainer pid=14368) get\_batch\_divisibility\_req=get\_batch\_divisibility\_req,  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/dynamic\_tf\_policy.py”, line 211, in **init**  
(PPOTrainer pid=14368) framework=“tf”,  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 541, in get\_model\_v2  
(PPOTrainer pid=14368) \*\*customized\_model\_kwargs,  
(PPOTrainer pid=14368) File “custom\_keras.py”, line 41, in **init**  
(PPOTrainer pid=14368) )(self.inputs)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 824, in **call**  
(PPOTrainer pid=14368) self.\_maybe\_build(inputs)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 2146, in \_maybe\_build  
(PPOTrainer pid=14368) self.build(input\_shapes)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/layers/core.py”, line 1021, in build  
(PPOTrainer pid=14368) trainable=True)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 529, in add\_weight  
(PPOTrainer pid=14368) aggregation=aggregation)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/training/tracking/base.py”, line 712, in \_add\_variable\_with\_custom\_getter  
(PPOTrainer pid=14368) \*\*kwargs\_for\_getter)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer\_utils.py”, line 139, in make\_variable  
(PPOTrainer pid=14368) shape=variable\_shape if variable\_shape else None)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 258, in **call**  
(PPOTrainer pid=14368) return cls.\_variable\_v1\_call(\*args, \*\*kwargs)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 219, in \_variable\_v1\_call  
(PPOTrainer pid=14368) shape=shape)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 65, in getter  
(PPOTrainer pid=14368) return captured\_getter(captured\_previous, \*\*kwargs)  
(PPOTrainer pid=14368) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 518, in track\_var\_creation  
(PPOTrainer pid=14368) created.add(v.ref())  
(PPOTrainer pid=14368) AttributeError: ‘ResourceVariable’ object has no attribute ‘ref’  
(RolloutWorker pid=14440) WARNING:tensorflow:From /home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/resource\_variable\_ops.py:1630: calling BaseResourceVariable. **init** (from tensorflow.python.ops.resource\_variable\_ops) with constraint is deprecated and will be removed in a future version.  
(RolloutWorker pid=14440) Instructions for updating:  
(RolloutWorker pid=14440) If using Keras pass \*\_constraint arguments to layers.  
(RolloutWorker pid=14440) 2022-07-24 21:38:37,243 ERROR worker.py:451 – Exception raised in creation task: The actor died because of an error raised in its creation task, ray::RolloutWorker. **init** () (pid=14440, ip=172.27.130.120, repr=\<ray.rllib.evaluation.rollout\_worker.RolloutWorker object at 0x7f64865492e8\>)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 634, in **init**  
(RolloutWorker pid=14440) seed=seed,  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 1789, in \_build\_policy\_map  
(RolloutWorker pid=14440) name, orig\_cls, obs\_space, act\_space, conf, merged\_conf  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/policy\_map.py”, line 141, in create\_policy  
(RolloutWorker pid=14440) observation\_space, action\_space, merged\_config  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/tf\_policy\_template.py”, line 270, in **init**  
(RolloutWorker pid=14440) get\_batch\_divisibility\_req=get\_batch\_divisibility\_req,  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/dynamic\_tf\_policy.py”, line 211, in **init**  
(RolloutWorker pid=14440) framework=“tf”,  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 541, in get\_model\_v2  
(RolloutWorker pid=14440) \*\*customized\_model\_kwargs,  
(RolloutWorker pid=14440) File “custom\_keras.py”, line 41, in **init**  
(RolloutWorker pid=14440) )(self.inputs)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 824, in **call**  
(RolloutWorker pid=14440) self.\_maybe\_build(inputs)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 2146, in \_maybe\_build  
(RolloutWorker pid=14440) self.build(input\_shapes)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/layers/core.py”, line 1021, in build  
(RolloutWorker pid=14440) trainable=True)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 529, in add\_weight  
(RolloutWorker pid=14440) aggregation=aggregation)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/training/tracking/base.py”, line 712, in \_add\_variable\_with\_custom\_getter  
(RolloutWorker pid=14440) \*\*kwargs\_for\_getter)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer\_utils.py”, line 139, in make\_variable  
(RolloutWorker pid=14440) shape=variable\_shape if variable\_shape else None)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 258, in **call**  
(RolloutWorker pid=14440) return cls.\_variable\_v1\_call(\*args, \*\*kwargs)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 219, in \_variable\_v1\_call  
(RolloutWorker pid=14440) shape=shape)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 65, in getter  
(RolloutWorker pid=14440) return captured\_getter(captured\_previous, \*\*kwargs)  
(RolloutWorker pid=14440) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 518, in track\_var\_creation  
(RolloutWorker pid=14440) created.add(v.ref())  
(RolloutWorker pid=14440) AttributeError: ‘ResourceVariable’ object has no attribute ‘ref’  
(RolloutWorker pid=14441) 2022-07-24 21:38:37,237 WARNING deprecation.py:47 – DeprecationWarning: `callbacks dict interface` has been deprecated. Use `a class extending rllib.agents.callbacks.DefaultCallbacks` instead. This will raise an error in the future!  
(RolloutWorker pid=14441) WARNING:tensorflow:From /home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/resource\_variable\_ops.py:1630: calling BaseResourceVariable. **init** (from tensorflow.python.ops.resource\_variable\_ops) with constraint is deprecated and will be removed in a future version.  
(RolloutWorker pid=14441) Instructions for updating:  
(RolloutWorker pid=14441) If using Keras pass \*\_constraint arguments to layers.  
(RolloutWorker pid=14441) 2022-07-24 21:38:37,301 ERROR worker.py:451 – Exception raised in creation task: The actor died because of an error raised in its creation task, ray::RolloutWorker. **init** () (pid=14441, ip=172.27.130.120, repr=\<ray.rllib.evaluation.rollout\_worker.RolloutWorker object at 0x7f2bf82ce2e8\>)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 634, in **init**  
(RolloutWorker pid=14441) seed=seed,  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/evaluation/rollout\_worker.py”, line 1789, in \_build\_policy\_map  
(RolloutWorker pid=14441) name, orig\_cls, obs\_space, act\_space, conf, merged\_conf  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/policy\_map.py”, line 141, in create\_policy  
(RolloutWorker pid=14441) observation\_space, action\_space, merged\_config  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/tf\_policy\_template.py”, line 270, in **init**  
(RolloutWorker pid=14441) get\_batch\_divisibility\_req=get\_batch\_divisibility\_req,  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/policy/dynamic\_tf\_policy.py”, line 211, in **init**  
(RolloutWorker pid=14441) framework=“tf”,  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 541, in get\_model\_v2  
(RolloutWorker pid=14441) \*\*customized\_model\_kwargs,  
(RolloutWorker pid=14441) File “custom\_keras.py”, line 41, in **init**  
(RolloutWorker pid=14441) )(self.inputs)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 824, in **call**  
(RolloutWorker pid=14441) self.\_maybe\_build(inputs)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 2146, in \_maybe\_build  
(RolloutWorker pid=14441) self.build(input\_shapes)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/layers/core.py”, line 1021, in build  
(RolloutWorker pid=14441) trainable=True)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer.py”, line 529, in add\_weight  
(RolloutWorker pid=14441) aggregation=aggregation)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/training/tracking/base.py”, line 712, in \_add\_variable\_with\_custom\_getter  
(RolloutWorker pid=14441) \*\*kwargs\_for\_getter)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/keras/engine/base\_layer\_utils.py”, line 139, in make\_variable  
(RolloutWorker pid=14441) shape=variable\_shape if variable\_shape else None)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 258, in **call**  
(RolloutWorker pid=14441) return cls.\_variable\_v1\_call(\*args, \*\*kwargs)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 219, in \_variable\_v1\_call  
(RolloutWorker pid=14441) shape=shape)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/tensorflow\_core/python/ops/variables.py”, line 65, in getter  
(RolloutWorker pid=14441) return captured\_getter(captured\_previous, \*\*kwargs)  
(RolloutWorker pid=14441) File “/home/sb/anaconda3/envs/gym/lib/python3.6/site-packages/ray/rllib/models/catalog.py”, line 518, in track\_var\_creation  
(RolloutWorker pid=14441) created.add(v.ref())  
(RolloutWorker pid=14441) AttributeError: ‘ResourceVariable’ object has no attribute ‘ref’

---

<div class="post-metadata">

**Author:** ![Seyar\_Barez](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/seyar_barez/32/2422_2.png) [@Seyar\_Barez](https://discuss.ray.io/u/Seyar_Barez)\
**Post date:** [July 24, 2022, 9:06pm UTC](https://discuss.ray.io/t/rllib-custom-keras-model/6945/2 "2022-07-24T21:06:00Z")

</div>

I updated Tensorflow from 1.15 to tf2 now i don’t have this problem!
