ANN可视化错误:不支持可视化的图层

huangapple go评论78阅读模式
英文:

ANN Visualizer ERROR: Layer not supported for visualizing

问题

我是相对新手,使用Python和Tensorflow/Keras进行机器学习。我现在已经运行了一个模型,并想要可视化我的网络。为此,有一个用于可视化人工神经网络(ANN)的Python库:ANN Visualizer

不幸的是,我收到以下错误消息:

ValueError: ANN Visualizer: Layer not supported for visualizing

我建模型的方法如下:

def build_ann(self, nLayers=4, nNeurons=64, LR_init=1e-2, LR_adapt=1e-4, LR_steps=1e5, regularizer=1e-2, dropout=0.3):
   
        model = Sequential()
        for runs in range(nLayers):
            return_seq = True if runs < nLayers-1 else False
            model.add(LSTM(int(Neurons[runs]), return_sequences=return_seq))
            model.add(Dropout(dropout))
        model.add(Dense(int(Neurons[-1])))
        model.add(Dense(len(self.targets), activation='sigmoid'))
        model.compile(loss='mean_squared_error', optimizer='Adam', metrics=['accuracy'])  
        model.build(input_shape=(1, self.maxlen, len(self.features)))
        model.summary()

        return model

然后创建对象:

model = ann.build_ann(nLayers=2, nNeurons=2)
history = model.fit(trainx, trainy, batch_size=1, epochs=30, validation_data=(testsetx, testsety))
ann_viz(model)

为什么无法创建可视化?在其他示例中,是因为Flatten()层。我没有这样的层。是for循环吗?谢谢!

英文:

I am relatively new to machine learning with Python and Tensorflow/Keras.
I have now got a model running and would like to visualise my network.
For this there is a python library for visualizing Artificial Neural Networks (ANN): ANN Visualizer.

Unfortunately I receive this error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Temp\ipykernel_1787626106579.py in 
     24 history = model.fit(trainx, trainy, batch_size=1, epochs=30, validation_data=(testsetx, testsety))
     25 
---&gt; 26 ann_viz(model)

c:\Users\aalles\Anaconda3\lib\site-packages\ann_visualizer\visualize.py in ann_viz(model, view, filename, title)
    121                 c.node(str(n), label=&quot;Image\n&quot;+pxls[1]+&quot; x&quot;+pxls[2]+&quot; pixels\n&quot;+clrmap, fontcolor=&quot;white&quot;);
    122             else:
--&gt; 123                 raise ValueError(&quot;ANN Visualizer: Layer not supported for visualizing&quot;);
    124         for i in range(0, hidden_layers_nr):
    125             with g.subgraph(name=&quot;cluster_&quot;+str(i+1)) as c:

ValueError: ANN Visualizer: Layer not supported for visualizing

My method of building the model looks like this:

def build_ann(self, nLayers=4, nNeurons=64, LR_init=1e-2, LR_adapt=1e-4, LR_steps=1e5, regularizer=1e-2, dropout=0.3):
   
        model = Sequential()
        for runs in range(nLayers):
            return_seq = True if runs &lt; nLayers-1 else False
            model.add(LSTM(int(Neurons[runs]), return_sequences=return_seq))  # , dropout=0.3
            model.add(Dropout(dropout))
        model.add(Dense(int(Neurons[-1])))
        model.add(Dense(len(self.targets), activation=&#39;sigmoid&#39;))
        model.compile(loss=&#39;mean_squared_error&#39;, optimizer=&#39;Adam&#39;, metrics=[&#39;accuracy&#39;])  
        model.build(input_shape=(1, self.maxlen, len(self.features)))
        model.summary()

        return model

Consequently, the object will be created:

model = ann.build_ann(nLayers=2, nNeurons=2)
history = model.fit(trainx, trainy, batch_size=1, epochs=30, validation_data=(testsetx, testsety))
ann_viz(model)

And the error appears.

Why can't a visualisation be created? In the other examples it was because of the Flatter() layer. I do not have such a layer. Is it the for loop?

Thank You very much!

答案1

得分: 0

以下是已翻译的内容:

错误的原因可能是 ann_visualizer API 的更新。因此,您需要明确使用 graphviz 来访问 ann_visualizer 生成的文件(.gv)以可视化模型。

from ann_visualizer.visualize import ann_viz
ann_viz(model, filename="iris.gv", title="Iris NN")

import graphviz
graph_file = graphviz.Source.from_file("iris.gv")
graph_file

请查看此复制的 gist 以供参考。

注意: 您还可以使用 TensorFlow 的 plot_model API 通过以下代码来可视化模型:

from tensorflow.keras.utils import plot_model
plot_model(model, to_file="iris_model.png", show_shapes=True)
英文:

The reason behind this error could be the update in ann_visualizer API. So you need to use graphviz explicitly to access the ann_visualizer generated file(.gv) for visualizing the model.

from ann_visualizer.visualize import ann_viz
ann_viz(model, filename=&quot;iris.gv&quot;, title=&quot;Iris NN&quot;)

import graphviz
graph_file = graphviz.Source.from_file(&quot;iris.gv&quot;)
graph_file

Please check this replicated gist for your reference.

Note: You can also use tensorflow's plot_model api to visualize the model by using below code:

from tensorflow.keras.utils import plot_model
plot_model(model, to_file=&quot;iris_model.png&quot;, show_shapes=True)

huangapple
  • 本文由 发表于 2023年4月17日 22:18:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定