Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exception encountered when calling layer and 'KerasTensor' object is not callable

I’m newbie to reinforcement learning.
I’d like to see and understand the code that predicts Kerath’s actor critic value, and then run it with some changes.

Example code: https://github.com/keras-team/keras-io/blob/master/examples/rl/actor_critic_cartpole.py

However, I ran into a problem when running it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Below is the total error.

예외가 발생했습니다. TypeError
Exception encountered when calling layer "custom_model" "f"(type CustomModel).
'KerasTensor' object is not callable
Call arguments received by layer "custom_model""f"(type CustomModel):
  • inputs=tf.Tensor(shape=(1, 10, 10), dtype=float32)
  File "C:\Users\cglab\Desktop\Match3\Model.py", line 19, in call
    common = self.common(inputs)
TypeError: 'KerasTensor' object is not callable

Here is the code

import tensorflow as tf

from keras import layers

class CustomModel(tf.keras.Model):
    def __init__(self, num_hidden, max_x, max_y, n_tile_type):
        super(CustomModel, self).__init__()
        self.inputs = layers.Input(shape=(max_y, max_x))
        self.common = layers.Dense(num_hidden, activation="relu")(self.inputs)
        tf.debugging.assert_shapes([(self.inputs, (tf.TensorShape([None, 10, 10])))]) #not assert
        self.x_probs = layers.Dense(max_x, activation="softmax")(self.common)
        self.y_probs = layers.Dense(max_y, activation="softmax")(self.common)
        self.tile_prob = layers.Dense(n_tile_type, activation="softmax")(self.common)
        self.critic = layers.Dense(1)(self.common)

    def call(self, inputs):
        tf.debugging.assert_shapes([(inputs, (tf.TensorShape([None, 10, 10])))]) #not assert

        common = self.common(inputs) ##Error
        x_probs = self.x_probs(common)
        y_probs = self.y_probs(common)
        tile_prob = self.tile_prob(common)
        critic = self.critic(common)

    return [x_probs, y_probs, tile_prob, critic]

#Initialize and call

model = CustomModel(256, max_x, max_y, max_tile_type)

state = np.full((self.max_y, self.max_x), -1)
state = tf.convert_to_tensor(state, dtype=tf.float32)
state = tf.expand_dims(state, 0)

x_probs, y_probs, tile_probs, critic_value = model(state)

I need help. thank you

>Solution :

A Layer is callable, and calling it with a Tensor will return another Tensor, which is not callable. More specifically, in __init__() we should just create the layers, and in call() we call them on actual input (that looked OK in the code):

    def __init__(self, num_hidden, max_x, max_y, n_tile_type):
        super(CustomModel, self).__init__()
        self.inputs = layers.Input(shape=(max_y, max_x))
        self.common = layers.Dense(num_hidden, activation="relu")
        tf.debugging.assert_shapes([(self.inputs, (tf.TensorShape([None, 10, 10])))]) #not assert
        self.x_probs = layers.Dense(max_x, activation="softmax")
        self.y_probs = layers.Dense(max_y, activation="softmax")
        self.tile_prob = layers.Dense(n_tile_type, activation="softmax")
        self.critic = layers.Dense(1)

Note how the (self.common) and likes were removed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading