[英]Error `Cannot import name 'wrappers' from 'tensorflow.python.keras.layers'`?
The code is giving the following error message代码给出以下错误消息
Cannot import name 'wrappers' from 'tensorflow.python.keras.layers'
- and ImportError: graphviz or pydot are not available.
Cannot import name 'wrappers' from 'tensorflow.python.keras.layers'
- 并且ImportError: graphviz or pydot are not available.
Even after installing the graphviz
and pydot
using the !apt-get -qq install -y graphviz && pip install pydot
still not able to genrate model.png
.即使在使用!apt-get -qq install -y graphviz && pip install pydot
安装了graphviz
和pydot
之后,仍然无法生成model.png
。
im running the following code in colab我在 colab 中运行以下代码
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow.python.keras.utils.vis_utils import plot_model
import pydot
from tensorflow.keras.models import Model
def build_model_with_sequential():
# instantiate a Sequential class and linearly stack the layers of your model
seq_model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
return seq_model
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: new_layer()(previous_layer)
flatten_layer = tf.keras.layers.Flatten()(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(first_dense)
# declare inputs and outputs
func_model = Model(inputs=input_layer, outputs=output_layer)
return func_model
model = build_model_with_functional()
#model = build_model_with_sequential()
# Plot model graph
plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
ERROR错误
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-aaa8f6f7cc6e> in <module>
3
4 # Plot model graph
----> 5 plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_dtype, show_layer_names, rankdir, expand_nested, dpi)
327 rankdir=rankdir,
328 expand_nested=expand_nested,
--> 329 dpi=dpi)
330 to_file = path_to_string(to_file)
331 if dot is None:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_dtype, show_layer_names, rankdir, expand_nested, dpi, subgraph)
96 ImportError: if graphviz or pydot are not available.
97 """
---> 98 from tensorflow.python.keras.layers import wrappers
99 from tensorflow.python.keras.engine import sequential
100 from tensorflow.python.keras.engine import functional
ImportError: cannot import name 'wrappers' from 'tensorflow.python.keras.layers' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/__init__.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
-------------------------------------------------------------------------
Seems to be some import conflicts.似乎是一些进口冲突。 Rather use tf.keras
for everything:而是将tf.keras
用于所有内容:
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
import pydot
def build_model_with_sequential():
# instantiate a Sequential class and linearly stack the layers of your model
seq_model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
return seq_model
def build_model_with_functional():
# instantiate the input Tensor
input_layer = tf.keras.Input(shape=(28, 28))
# stack the layers using the syntax: new_layer()(previous_layer)
flatten_layer = tf.keras.layers.Flatten()(input_layer)
first_dense = tf.keras.layers.Dense(128, activation=tf.nn.relu)(flatten_layer)
output_layer = tf.keras.layers.Dense(10, activation=tf.nn.softmax)(first_dense)
# declare inputs and outputs
func_model = tf.keras.Model(inputs=input_layer, outputs=output_layer)
return func_model
model = build_model_with_functional()
tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True, to_file='model.png')
It's the version issue.是版本问题。
According to the latest tensorflow doc (at least from 2.8.1).根据最新的 tensorflow 文档(至少从 2.8.1 开始)。 there's no tensorflow.python
package.没有tensorflow.python
package。 plot_model
module has been moved to tensorflow.keras.utils
. plot_model
模块已移至tensorflow.keras.utils
。
so simply replacing所以简单地替换
from tensorflow.python.keras.utils.vis_utils import plot_model
with和
from tensorflow.keras.utils import plot_model
could solve the issue.可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.