【问题标题】:Keras: Assertion Error when plotting modelKeras:绘制模型时出现断言错误
【发布时间】:2020-08-14 13:02:56
【问题描述】:

我想使用下面的函数绘制我的 Keras 模型。每次我尝试这样做时,它都会给我以下 AssertionError。有人可以帮忙吗?

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-37-4e7b8789844d> in <module>
      9 
     10 model,_ = create_model()
---> 11 plot_keras_model(model, show_shapes=True, show_layer_names=False)

<ipython-input-37-4e7b8789844d> in plot_keras_model(model, show_shapes, show_layer_names)
      6 from keras.utils.vis_utils import model_to_dot
      7 def plot_keras_model(model, show_shapes=True, show_layer_names=True):
----> 8     return SVG(model_to_dot(model, show_shapes=show_shapes, show_layer_names=show_layer_names, expand_nested=True).create(prog='dot',format='svg'))
      9 
     10 model,_ = create_model()

/opt/conda/lib/python3.6/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir, expand_nested, dpi, subgraph)
    180                             if not is_model(layer) and (
    181                                     not is_wrapped_model(layer)):
--> 182                                 assert dot.get_node(inbound_layer_id)
    183                                 assert dot.get_node(layer_id)
    184                                 dot.add_edge(pydot.Edge(inbound_layer_id,

AssertionError: 

import pydotplus as pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
def plot_keras_model(model, show_shapes=True, show_layer_names=True):
    return SVG(model_to_dot(model, show_shapes=show_shapes, show_layer_names=show_layer_names).create(prog='dot',format='svg'))

model,_ = create_model()
plot_keras_model(model, show_shapes=True, show_layer_names=False)

【问题讨论】:

  • 我们能知道断言错误是什么,还是不完整?
  • @neel g:断言错误不完整。上面的错误信息就是我得到的全部..

标签: python tensorflow plot keras pydot


【解决方案1】:

如果不知道您是如何构建模型的,我们无法准确判断。但是,如果我不得不猜测,那么您可能在 Keras 模型中将 tf.keras.layers 与 keras.layers 混合在一起。这是引发此类错误的原因之一。

【讨论】:

    【解决方案2】:

    我只想指出我在尝试keras.utils.plot_model(model, etc) 时遇到了相同的断言错误----&gt; dot.get_node(inbound_layer_id)

    当我更改导入时问题得到解决:

    from keras.models import Sequential
    from keras.layers import Dense
    

    收件人:

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    

    我还必须更改 tensorflow.keras.utils import to_categorical ,因为 Keras 已完全集成到 Tensorflow 中。如此处所示: Error in "from keras.utils import to_categorical"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 2016-05-29
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多