You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

强化学习新手修改Keras Actor-Critic代码遇'KerasTensor不可调用'错误求助

问题:Keras Actor-Critic自定义模型触发TypeError: 'KerasTensor' object is not callable

错误信息

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

解决方案

问题根源

在__init__方法中,直接调用层(如layers.Dense(...)(self.inputs))会返回KerasTensor对象而非层本身,导致self.common、self.x_probs等变量存储的是张量而非可调用的层实例,调用时触发错误。此外,输入是二维张量(10,10),直接传入Dense层会维度不匹配,需先展平。

修改后的代码

import tensorflow as tf
from keras import layers
import numpy as np

class CustomModel(tf.keras.Model):
    def __init__(self, num_hidden, max_x, max_y, n_tile_type):
        super(CustomModel, self).__init__()
        # 仅定义层对象,不立即传入输入张量
        self.flatten = layers.Flatten()  # 新增:将二维输入展平为一维特征
        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):
        # 按顺序处理输入:展平→公共层→各输出层
        x = self.flatten(inputs)
        common = self.common(x)
        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]

# 初始化示例(补充缺失的变量)
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)

关键修改点

  1. 移除__init__中的layers.Input和所有层的即时调用,仅保留层对象定义;
  2. 新增Flatten层:将二维输入(10,10)转为一维(100),适配Dense层的输入要求;
  3. 修正call方法的缩进错误(原代码return语句缩进不正确);
  4. 在call方法中按流程传递张量,完成前向计算。

内容的提问来源于stack exchange,提问作者donghyunlee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 20:13:09