tensorflow.keras.utils.plot_model无法正常工作
我刚刚使用了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:
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.
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 :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的(输入和输出值的水平拆分)可以在
Tensorflow 2.7
的早期版本中实现。请使用以下代码安装
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:因此,我已经看了几个小时了,并且已经重新创建了这个问题。我可以重新创建两个绘制的模型(垂直和水平),但我无法直接控制它。我看到的唯一真正的区别是,您要寻找的
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.sequential
或functional.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 usingFunctional API
. UsingSequential API
produces the graph you don't want.So pretty much try recreating your model using
Functional API
instead ofSequential API
. You shouldn't see any difference in performance. I would've redone your model usingFunctional API
but there's no code to redo. If you want, you can play around withkeras.utils.vis_utils.py.model_to_dot()
to see if you can get theFunctional 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 issequential.Sequential
orfunctional.Functional
.