【问题标题】:AttributeError: 'NoneType' object has no attribute 'summary'AttributeError:“NoneType”对象没有属性“摘要”
【发布时间】:2022-01-03 19:26:18
【问题描述】:

我开始学习如何使用 keras 实现神经网络。但是,我刚刚偶然发现了这个错误。我不知道我在这里做错了什么。我正在与 Valerio 的 youtube 教程一起工作:His vids on implementing an autoencoder

我的代码:

import tensorflow
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, Dropout, LSTM

class GNN:
  """
  GNN is a graph neural network.
  """
  
  def __init__(self, 
               input_shape_emb,
               input_shape_lstm
              ):
    
    
    
    self.input_shape_emb = input_shape_emb # [1, 38]
    self.input_shape_lstm = input_shape_lstm # [1,348]
    self.embedding_shape = 92
    
    
    self.emb = None
    self.lstm = None
    self.mpls = None
    self.model = None
    
    self._num_lstm_cells = 4
    
    self._build()
    
    
  def summary(self):
    self.emb.summary()
    self.lstm.summary()
    
  def _build(self):
    self._build_emb
    self._build_lstm
    #self._build_mlps
    
    
    
  def _build_emb(self):
    embedding_input = self._add_embedding_input()
    embedding_output = self._add_dense_layer(embedding_input)
    self.emb = tensorflow.keras.Model(embedding_input, embedding_output, name="embedding")
  
  def _add_embedding_input(self):
    return Input(shape=self.input_shape_emb, name="embedding_input")
  
  def _add_dense_layer(self,embedding_input):
    num_neurons = self.embedding_shape
    return Dense(num_neurons, name="embedding_dense")(embedding_input)
  
  
  
  
  def _build_lstm(self):
    lstm_input = self._add_lstm_input()
    lstm_layers = self._add_lstm_cells(lstm_input)
    self.lstm = tensorflow.keras.Model(lstm_input, lstm_layers, name="lstm_network")
  
  def _add_lstm_input(self):
    return Input(shape=self.input_shape_lstm, name="lstm_network_input")

  def _add_lstm_cells(self, lstm_input):
    """Creates all lstm cell blocks in lstm network."""
    x = lstm_input
    for cell_index in range(self._num_lstm_cells):
      x = self._add_lstm_cell(cell_index, x)
    return x
  
  def _add_lstm_cell(self, cell_index, x):
    """Adds an lstm cell to a graph of cells, consisting of
    lstm + ReLu + Dropout.
    """
    lstm_cell = LSTM(
      256, activation='relu', dropout=0.1, name = f"lstm_cell_{cell_index+1}"
    )
    x = lstm_cell(x)
    return x
    
    
    
    
    
    
if __name__ == "__main__":
  model = GNN(
    input_shape_emb = (1051, 38), #Input_DFS_sequence[0].shape, #(1051, 38)
    input_shape_lstm = 348
  )
  model.summary()
    

错误:

AttributeError: 'NoneType' object has no attribute 'summary'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<command-xx> in <module>
     92     input_shape_lstm = 348
     93   )
---> 94   model.summary()
     95 
     96 

<command-xx> in summary(self)
     32 
     33   def summary(self):
---> 34     self.emb.summary()
     35     self.lstm.summary()
     36 

AttributeError: 'NoneType' object has no attribute 'summary'

非常感谢您的帮助!

【问题讨论】:

  • self._build_embself._build_lstm 没有被调用。
  • 非常感谢。
  • 顺便说一句,你问错问题了。它应该是“当我期待别的东西时,为什么我有None?”。也就是说,作为这里的新用户,请使用tour 并阅读How to Ask

标签: python python-3.x tensorflow keras neural-network


【解决方案1】:

在调用实际分配变量的_build() 之后,您将emb 重置为None;其他属性也是如此。而是首先将其默认为None

self.emb = None
self.lstm = None
self.mpls = None
self.model = None
self._build()

您也没有调用函数来实际构建 _build() 中的属性。

def _build(self):
    self._build_emb()
    self._build_lstm()

【讨论】:

  • 谢谢!但是,当我将 self._build() 放在 None 定义之后时,我仍然会收到错误消息。以前有过这样的,但是忘记换回来了。上面给出了更新后的代码。
  • @Omatikaya 查看我的编辑
  • 哇哦,当然它们也是函数!谢谢!
猜你喜欢
  • 2019-01-01
  • 2021-12-26
  • 2019-07-23
  • 2018-05-13
  • 2020-09-07
  • 2017-05-03
  • 2023-03-16
  • 2018-07-14
  • 2013-06-16
相关资源
最近更新 更多