简体   繁体   English

错误“无法从“tensorflow.python.keras.layers”导入名称“包装器”?

[英]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安装了graphvizpydot之后,仍然无法生成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.

相关问题 Python 错误:无法解析导入'tensorflow.python.keras.layers'Python(未解析导入) - Python Error: unresolved import 'tensorflow.python.keras.layers'Python(unresolved-import) 导入错误:无法从 &#39;tensorflow.python.keras.layers.normalization&#39; 导入名称 &#39;LayerNormalization&#39;,在 colab 上出现此错误? - ImportError: cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization' , getting this error on colab? 错误:无法从“tensorflow.keras.layers.experimental.preprocessing”导入名称“StringLookup” - Error: cannot import name 'StringLookup' from 'tensorflow.keras.layers.experimental.preprocessing' ImportError:无法从“tensorflow.python.keras.layers.normalization”导入名称“LayerNormalization” - ImportError: cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization' "ImportError:无法从“tensorflow.python.eager.context”导入名称“get_config”[起点:从 keras.layers.core 导入激活]" - ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' [start point : from keras.layers.core import Activation] 无法从“tensorflow.keras.layers.experimental.preprocessing”导入名称“CenterCrop” - cannot import name 'CenterCrop' from 'tensorflow.keras.layers.experimental.preprocessing' ImportError:无法从“tensorflow”导入名称“keras” - ImportError: cannot import name 'keras' from 'tensorflow' from keras.layers import Dense -- 无法导入名称“Dense” - from keras.layers import Dense -- cannot import name 'Dense' 无法从“keras.layers”导入名称“Merge” - Cannot import name 'Merge' from 'keras.layers' 导入错误:无法从“keras.layers.normalization”导入名称“BatchNormalization” - ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM