找不到名为'tensorflow.python.ops.numpy_ops'的模块。

3

我想开始学习一些机器学习,并尝试导入Tensorflow,但是出现了ModuleNotFoundError错误:

ModuleNotFoundError: No module named 'tensorflow.python.ops.numpy_ops'

当前代码只是尝试导入包:

import os
import sys
import math
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras import LSTM

完整的回溯信息如下:
Traceback (most recent call last): 

 File "C:\Users\stacey\Documents\Atela\MachineLearning\NeuralNetworks\NeuralNetwork1.0.py", line 17, in <module>
    from tensorflow.keras import LSTM

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\__init__.py", line 47, in <module>
    from tensorflow.python import distribute

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\distribute\__init__.py", line 28, in <module>
    from tensorflow.python.distribute.experimental import collective_all_reduce_strategy

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\distribute\experimental\__init__.py", line 25, in <module>
    from tensorflow.python.distribute import tpu_strategy

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\distribute\tpu_strategy.py", line 28, in <module>
    from tensorflow.compiler.xla.experimental.xla_sharding import xla_sharding

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\compiler\xla\experimental\xla_sharding\xla_sharding.py", line 23, in <module>
    from tensorflow.compiler.tf2xla.python import xla as tf2xla

  File "C:\Users\stacey\AppData\Roaming\Python\Python36\site-packages\tensorflow\compiler\tf2xla\python\xla.py", line 43, in <module>
    from tensorflow.python.ops.numpy_ops import np_utils

ModuleNotFoundError: No module named 'tensorflow.python.ops.numpy_ops'

Python版本:3.6
TensorFlow版本:2.6.2

1个回答

3
我认为没有 tensorflow.keras.LSTM。也许你正在寻找 tensorflow.keras.layers.LSTM更新: 通过将tensorflow (2.9.1)和python (3.9)升级到最新版本,问题得到了解决。可以在 这里 找到版本兼容性表。升级

我尝试使用keras.layers中的LSTM,但是得到了相同的错误。 - Stacey
这有帮助吗?也许你只需要升级tensorflow版本。https://stackoverflow.com/a/66203280/14774959 - ClaudiaR
我在发布问题之前卸载并重新安装了 - 我如何确保我拥有最新版本? - Stacey
我认为你可以执行 pip install tensorflow --upgrade 或者 pip install tensorflow==2.9.1(2.9.1 是目前最新版本)。如果你使用的是 GPU,那么你需要安装 tensorflow-gpu。然而,我看到你正在使用 Python 3.6,所以也许较新的 TensorFlow 版本与之不兼容。 - ClaudiaR
1
是的,看起来在Python 3.6中,您可以获得的TensorFlow的最大版本是2.6。如果这对您没有问题,您应该考虑升级Python https://www.tensorflow.org/install/source?hl=en#tested_build_configurations - ClaudiaR
1
谢谢@ClaudiaR - 最终我卸载了Python和所有的包,并重新安装了最新版本,这解决了问题。 - Stacey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接