将 Jupyter Notebooks 与 Anaconda 结合使用时导入库时出现问题

问题描述 投票:0回答:2
# lstm model
import tensorflow as tf
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.utils import to_categorical
from matplotlib import pyplot

当我运行此单元格时,出现以下错误。我在 Anaconda 上使用 Jupyter Notebooks,安装了与 Python 相关的每个包,但在导入库时仍然显示 Tensorflow 错误。我正在使用最新版本的Python(3.9)。谁能帮我解决这个问题吗?

错误附在下面。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10616/748904884.py in <module>
       1 # lstm model
 ----> 2 import tensorflow as tf
       3 from numpy import mean
       4 from numpy import std
       5 from numpy import dstack

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\__init__.py in <module>
      39 import sys as _sys
      40 
 ---> 41 from tensorflow.python.tools import module_util as _module_util
      42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
      43 

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\__init__.py in <module>
     44 
     45 # Bring in subpackages.
---> 46 from tensorflow.python import data
     47 from tensorflow.python import distribute
     48 # from tensorflow.python import keras

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\data\__init__.py in <module>
     23 
     24 # pylint: disable=unused-import
---> 25 from tensorflow.python.data import experimental
     26 from tensorflow.python.data.ops.dataset_ops import AUTOTUNE
     27 from tensorflow.python.data.ops.dataset_ops import Dataset

~\AppData\Roaming\Python\Python39\sitepackages\tensorflow\python\data\experimental\__init__.py 
   in <module>
     96 
     97 # pylint: disable=unused-import
---> 98 from tensorflow.python.data.experimental import service
     99 from tensorflow.python.data.experimental.ops.batching import dense_to_ragged_batch
    100 from tensorflow.python.data.experimental.ops.batching import dense_to_sparse_batch

  ~\AppData\Roaming\Python\Python39\site- 
  packages\tensorflow\python\data\experimental\service\__init__.py in <module>
    372 from __future__ import print_function
    373 
--> 374 from tensorflow.python.data.experimental.ops.data_service_ops import distribute
    375 from tensorflow.python.data.experimental.ops.data_service_ops import from_dataset_id
    376 from tensorflow.python.data.experimental.ops.data_service_ops import register_dataset

~\anaconda3\envs\mygpu\lib\site- 
 packages\tensorflow\python\data\experimental\ops\data_service_ops.py in <module>
     23 
     24 from tensorflow.python import tf2
---> 25 from tensorflow.python.data.experimental.ops import compression_ops
     26 from tensorflow.python.data.experimental.ops.distribute_options import AutoShardPolicy
     27 from tensorflow.python.data.experimental.ops.distribute_options import 
        ExternalStatePolicy

~\anaconda3\envs\mygpu\lib\site- 
    packages\tensorflow\python\data\experimental\ops\compression_ops.py in <module>
     18 from __future__ import print_function
     19 
---> 20 from tensorflow.python.data.util import structure
     21 from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
     22 

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\data\util\structure.py in <module>
     24 import wrapt
     25 
---> 26 from tensorflow.python.data.util import nest
     27 from tensorflow.python.framework import composite_tensor
     28 from tensorflow.python.framework import ops

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\data\util\nest.py in <module>
     38 import six as _six
     39 
---> 40 from tensorflow.python.framework import sparse_tensor as _sparse_tensor
     41 from tensorflow.python.util import _pywrap_utils
     42 from tensorflow.python.util import nest

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\sparse_tensor.py in 
 <module>
     26 from tensorflow.python import tf2
     27 from tensorflow.python.framework import composite_tensor
---> 28 from tensorflow.python.framework import constant_op
     29 from tensorflow.python.framework import dtypes
     30 from tensorflow.python.framework import ops

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\constant_op.py in 
  <module>
     27 from tensorflow.core.framework import types_pb2
     28 from tensorflow.python.eager import context
---> 29 from tensorflow.python.eager import execute
     30 from tensorflow.python.framework import dtypes
     31 from tensorflow.python.framework import op_callbacks

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\eager\execute.py in <module>
     25 from tensorflow.python import pywrap_tfe
     26 from tensorflow.python.eager import core
---> 27 from tensorflow.python.framework import dtypes
     28 from tensorflow.python.framework import ops
     29 from tensorflow.python.framework import tensor_shape

~\anaconda3\envs\mygpu\lib\site-packages\tensorflow\python\framework\dtypes.py in <module>
     30 from tensorflow.python.util.tf_export import tf_export
     31 
---> 32 _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()
     33 
     34 

TypeError: Unable to convert function return value to a Python type! The signature was
    () -> handle
python tensorflow importerror
2个回答
0
投票

先尝试将各个软件包逐个安装一下,然后就可以使用了。


0
投票

我在 m1 macbook 上遇到了这个问题,结果发现我的tensorflow依赖项与我的tensorflow-macos包不同步。这将是一个检查的好地方,在此页面中,他们给出的版本是2.6.0,但是运行此命令时tf-macos的最新版本

python -m pip install tensorflow-macos
给出了2.8.0,因此出现错误。如果您刚刚更新了 Tensorflow 版本,很可能会出现这种情况,请检查您的 Tensorflow 依赖项并查看是否存在混淆。

如果这不能解决问题,那么首先安装 tokenizer,然后安装转换器,看看是否可以解决问题

© www.soinside.com 2019 - 2024. All rights reserved.