【问题标题】:AttributeError: 'function' object has no attribute 'predict'. KerasAttributeError:“函数”对象没有属性“预测”。喀拉斯
【发布时间】:2020-02-29 18:58:36
【问题描述】:

我正在研究一个 RL 问题,我创建了一个类来初始化模型和其他参数。代码如下:

class Agent:
    def __init__(self, state_size, is_eval=False, model_name=""):
        self.state_size = state_size
        self.action_size = 20 # measurement, CNOT, bit-flip
        self.memory = deque(maxlen=1000)
        self.inventory = []
        self.model_name = model_name
        self.is_eval = is_eval
        self.done = False

        self.gamma = 0.95
        self.epsilon = 1.0
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995


    def model(self):
        model = Sequential()
        model.add(Dense(units=16, input_dim=self.state_size, activation="relu"))
        model.add(Dense(units=32, activation="relu"))
        model.add(Dense(units=8, activation="relu"))
        model.add(Dense(self.action_size, activation="softmax"))
        model.compile(loss="categorical_crossentropy", optimizer=Adam(lr=0.003))
        return model

    def act(self, state):
        options = self.model.predict(state)
        return np.argmax(options[0]), options

我只想运行一次迭代,因此我创建了一个对象并传递了一个长度为 16 的向量,如下所示:

agent = Agent(density.flatten().shape)
state = density.flatten()
action, probs = agent.act(state)

但是,我收到以下错误:

AttributeError                       Traceback (most recent call last) <ipython-input-14-4f0ff0c40f49> in <module>
----> 1 action, probs = agent.act(state)

<ipython-input-10-562aaf040521> in act(self, state)
     39 #             return random.randrange(self.action_size)
     40 #         model = self.model()
---> 41         options = self.model.predict(state)
     42         return np.argmax(options[0]), options
     43 

AttributeError: 'function' object has no attribute 'predict'

有什么问题?我也查了一些其他人的代码,比如this,我觉得我的也很相似。

告诉我。

编辑:

我将Dense 中的参数从input_dim 更改为input_shapeself.model.predict(state) 更改为self.model().predict(state)

现在,当我为一个形状为 (16,1) 的输入数据运行 NN 时,我收到以下错误:

ValueError:检查输入时出错:预期的 dense_1_input 有 3 维,但得到了形状为 (16, 1) 的数组

当我使用形状 (1,16) 运行它时,我收到以下错误:

ValueError:检查输入时出错:预期的 dense_1_input 有 3 维,但得到了形状为 (1, 16) 的数组

在这种情况下我该怎么办?

【问题讨论】:

  • 你有一个函数和一个同名的变量(模型),这是个坏主意
  • 在链接中,这个人也做了同样的事情。很多程序员都是这样写的。连我自己都觉得很奇怪。但显然,这就是人们编写代码的方式。
  • 不行,链接里的代码不一样,没有叫model()的函数,这是你问题的核心。
  • 好的。所以现在,我把函数的名字改成了model_rl。甚至将行中的名称更改为options = self.model_rl.predict(state)。我仍然收到错误
  • 我认为你有一个很大的概念误解,这不是我告诉你的。您可以更改函数的名称,但是您必须调用它并将函数调用的返回值分配给一个变量(使用不同的名称),您可以从该变量调用 predict。

标签: python-3.x keras deep-learning reinforcement-learning attributeerror


【解决方案1】:

在最后一个代码块中,

def act(self, state):
        options = self.model.predict(state)
        return np.argmax(options[0]), options

self.model 是一个返回模型的函数,应该是 self.model().predict(state)

【讨论】:

  • 我收到此错误:TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'.
  • 你能发布完整的堆栈跟踪,你在哪里得到这个错误?
  • 40 # return random.randrange(self.action_size) 41 # model = self.model() ---&gt; 42 options = self.model().predict(state) 43 return np.argmax(options[0]), options 44 &lt;ipython-input-18-417404b4f01d&gt; in model(self) 28 def model(self): 29 model = Sequential() ---&gt; 30 model.add(Dense(units=16, input_dim=self.state_size, activation="relu")) 31 model.add(Dense(units=32, activation="relu")) 32 model.add(Dense(units=8, activation="relu"))
  • density.flatten().shape 是一个元组,您在 Agent 类中作为 state_size 传递,在 Dense 层中设置为 input_dim它不将元组作为输入。 input_dim 是特征的维数。
【解决方案2】:

我使用了np.reshape。所以在这种情况下,我做到了

density_test = np.reshape(density.flatten(), (1,1,16))

然后网络给出了输出。

【讨论】:

    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 2022-01-14
    • 2019-05-10
    相关资源
    最近更新 更多