【问题标题】:NotImplementedError attempting to build LSTM model using Tensorflow and KerasNotImplementedError 尝试使用 Tensorflow 和 Keras 构建 LSTM 模型
【发布时间】:2021-09-25 16:05:16
【问题描述】:

我目前正在关注本教程的多变量时间序列 LSTM https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/?unapproved=616760&moderation-hash=4e3c737e7c47cf8d889faaf84c3f73db#comment-616760

我们在构建模型时遇到了一个问题:

from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
from pandas import DataFrame
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

...
# design network
model = Sequential()
model.add(LSTM(50, input_shape=(train_X.shape[1], train_X.shape[2]))) #error here

我得到的错误是NotImplementedError: Cannot convert a symbolic Tensor (lstm_4/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported 其他一切似乎都在运行。我得到了一个收益/损失图和一切。这个特定的代码部分只是给了我这个错误。我查了一下,有些人在没有正确安装 Tensorflow 后得到了这个错误。据我所知,它安装得很好,并且在我导入它时似乎没有给我任何问题。

我正在使用 Python 3.7.9 上的 Spyder 5.0.5;但是,我在 Spyder 中的特定环境是 Miniconda 3,即 Python 3.9.5。卸载并重新安装我的软件包似乎不起作用。升级我的包会产生一条消息,基本上说明没有要安装的升级。

编辑:

TensorFlow 2.5.0 版

keras 2.5.0 版

numpy 版本 1.20.3

【问题讨论】:

  • 我找到了一个假设的解决方案pip install --force-reinstall numpy==1.19.3。显然问题出现在 numpy 版本 1.20 和 1.21 中。但是,现在我得到了 pandas 的错误。 ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject 这就是我首先升级 numpy 的原因。
  • 是的@AcidCatfish,这个问题之前已经解决了by downgrading by either downgrading python 3.8 to 3.6 or 1.20 to 1.18/1.19,由于兼容性而发生,下面是一些可能对你有帮助的文章。 stackoverflow.com/questions/66207609/…exerror.com/…
  • 谢谢!我实际上找到了我遇到降级问题的主要原因。您建议的修复方法有效。

标签: python tensorflow keras


【解决方案1】:

请确保,如果使用 Spyder IDE,您仅使用 PipConda 来安装软件包,并且您没有将两者混合使用。

如果是这种情况,请从头开始创建一个新环境并将其设置为您的解释器。

确保在降级或安装特定版本的软件包时冻结所有其他软件包。这样,您可以确保不会自动更新任何内容。

此外,如果您使用MinicondaAnaconda,安装包的任何问题通常可以使用conda install -c -conda-forge <some package> 解决。

如果使用Spyder,虽然您通常可以在 IDE 中从控制台安装软件包,但仍然建议从 IDE 之外的终端安装软件包。确保您安装到与 Spyder 中设置的解释器相同的环境中。您可以使用cd命令将工作目录更改为您的环境文件夹然后conda activate <environment name>,您可以直接安装到环境中。

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2016-08-24
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多