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_17876\3726106579.py in
24 history = model.fit(trainx, trainy, batch_size=1, epochs=30, validation_data=(testsetx, testsety))
25
---> 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="Image\n"+pxls[1]+" x"+pxls[2]+" pixels\n"+clrmap, fontcolor="white");
122 else:
--> 123 raise ValueError("ANN Visualizer: Layer not supported for visualizing");
124 for i in range(0, hidden_layers_nr):
125 with g.subgraph(name="cluster_"+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 < 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='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
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!
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="iris.gv", title="Iris NN")
import graphviz
graph_file = graphviz.Source.from_file("iris.gv")
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="iris_model.png", show_shapes=True)