调用图层时遇到异常且“KerasTensor”对象不可调用

问题描述 投票:0回答:1

我是强化学习的新手。 我想查看并理解预测 Kerath 演员评论值的代码,然后进行一些更改来运行它。

示例代码:https://github.com/keras-team/keras-io/blob/master/examples/rl/actor_critic_cartpole.py

但是,我在运行时遇到了问题。

以下是总误差。

예외가 발생했습니다. 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

这是代码

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)

我需要帮助。谢谢你

python tensorflow keras reinforcement-learning keras-layer
1个回答
1
投票

A

Layer
是可调用的,使用
Tensor
调用它会返回另一个不可调用的
Tensor
。更具体地说,在
__init__()
中,我们应该只是 create 图层,而在
call()
中,我们根据实际输入调用它们(在代码中看起来不错):

    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)

注意

(self.common)
和点赞是如何被删除的。

© www.soinside.com 2019 - 2024. All rights reserved.