tensorflow.keras.utils.plot_model无法正常工作

发布于 2025-02-13 10:46:26 字数 1266 浏览 1 评论 0原文

我刚刚使用了tf.keras.utils.plot_model,显示了以下图:

“出现”>

https://i.sstatic.net/bierh.png“ alt =”图表 图的右侧部分显示了输入和输出形状的垂直拆分,但应该是水平拆分,以适合“ intput:”和“ output:”左侧的单元格。

代码行:

# Import the required libraries.
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model

input_shape = Input(shape=(128,128,3), name='input')
conv1 = Conv2D(filters=256, kernel_size=6, strides=2, padding='valid', activation='relu', name='conv1')(input_shape)
flt = Flatten()(conv1)
shared = Dense(64)(flt)
sub1 = Dense(16)(shared)
out1 = Dense(3, activation='softmax', name='gait')(sub1)
model = Model(inputs=input_shape, outputs=out1)


plot_model(model=model, show_shapes=True)

您想要的结果:

I've just used the tf.keras.utils.plot_model and the following diagram appeared:

diagram appeared

The right part of the diagram shows the input and output shapes with a vertical split, but it should be an horizontal split to fit with the "intput:" and "output:" cells at its left.

enter image description here

code line :

# Import the required libraries.
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import Model

input_shape = Input(shape=(128,128,3), name='input')
conv1 = Conv2D(filters=256, kernel_size=6, strides=2, padding='valid', activation='relu', name='conv1')(input_shape)
flt = Flatten()(conv1)
shared = Dense(64)(flt)
sub1 = Dense(16)(shared)
out1 = Dense(3, activation='softmax', name='gait')(sub1)
model = Model(inputs=input_shape, outputs=out1)


plot_model(model=model, show_shapes=True)

The result you want :

Result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2025-02-20 10:46:26

您正在寻找的(输入和输出值的水平拆分)可以在Tensorflow 2.7的早期版本中实现。

请使用以下代码安装tensorflow 2.7,重新启动内核,然后尝试在上面的代码上运行相同的代码:

!pip install tensorflow==2.7

“

What you are looking for(Horizontal Split of Input and Output values) can be achieved in the earlier version of TensorFlow 2.7.

Please install the TensorFlow 2.7 using the below code, restart the kernel and then try running the same above code:

!pip install tensorflow==2.7

enter image description here

暖风昔人 2025-02-20 10:46:26

因此,我已经看了几个小时了,并且已经重新创建了这个问题。我可以重新创建两个绘制的模型(垂直和水平),但我无法直接控制它。我看到的唯一真正的区别是,您要寻找的plot_model()结果是使用functional api构建模型时。使用顺序API生成图形您 not 想要。

因此,几乎尝试使用功能API而不是顺序API来重新创建模型。您不应该看到性能的任何差异。我会使用功能API重做您的模型,但是没有代码重做。如果需要,可以使用keras.utils.vis_utils.py.model_to_dot()来查看是否可以获取所有模型类型的function> function> function> function> function> function> function>输出。如果我找到了更改,我可能会在以后更新此答案。查看该文件,以及与生成的图相比,我很肯定,这是函数检查模型是否为sequential.sequential.sequentialfunctional.functional.functional.functional的时候。

So I've looked at this for a few hours now and I've recreated the issue. I can recreate both plotted models how you show (vertical and horizontal) but I can't control it directly. The only real difference I can see is that the plot_model() results you're looking for is when the model is built using Functional API. Using Sequential API produces the graph you don't want.

So pretty much try recreating your model using Functional API instead of Sequential API. You shouldn't see any difference in performance. I would've redone your model using Functional API but there's no code to redo. If you want, you can play around with keras.utils.vis_utils.py.model_to_dot() to see if you can get the Functional API output for all model types. I might update this answer later with the changes if I find them. Looking at that file, and also comparing to generated plots, I'm positive it's when the function checks if the model is sequential.Sequential or functional.Functional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文