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

hwazgwia  于 2023-10-19  发布在  其他
关注(0)|答案(2)|浏览(186)

我是强化学习的新手。我想看看并理解预测Keras演员评论家值的代码,然后在进行一些更改后运行它。
示例代码: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)

我需要帮助谢谢你

iqih9akk

iqih9akk1#

我试着用演示变量重新创建你的代码,主要问题是你应该把return语句放在class CustomModelcall方法中。这就是为什么它会抛出TypeError: 'KerasTensor' object is not callable异常,因为CustomModel类没有返回正确的Tensor对象。以下是更正后的:

import numpy as np
import tensorflow as tf
from tensorflow.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.common = layers.Dense(num_hidden, activation="relu")
        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)

    def call(self, inputs):
        common = self.common(inputs)
        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
max_x = 10
max_y = 10
max_tile_type = 5
model = CustomModel(256, max_x, max_y, max_tile_type)

state = np.full((max_y, 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)

# Print shapes of the outputs for verification
print("x_probs shape:", x_probs.shape)
print("y_probs shape:", y_probs.shape)
print("tile_probs shape:", tile_probs.shape)
print("critic_value shape:", critic_value.shape)

输出如下:

x_probs shape: (1, 10, 10)
y_probs shape: (1, 10, 10)
tile_probs shape: (1, 10, 5)
critic_value shape: (1, 10, 1)

我还做了一些其他的调整。您不需要不必要的self.inputs层和tf.debugging.assert_shapes语句,因为它们在此上下文中不必要。我还正确地用__init__方法示例化了self.common层。
希望能帮上忙!

b4wnujal

b4wnujal2#

Layer是可调用的,用Tensor调用它通常会返回另一个不可调用的Tensor(更新:用户可以创建自定义的Layer s来返回其他的东西,但是对于基本的东西,比如这个问题中的那个,返回值是不可调用的)。更具体地说,在__init__()中,我们应该只创建层,而在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)和喜欢是如何被删除的。

相关问题