python - 模块 'tensorflow' 没有带有股票预测的属性 'get_default_graph'

标签 python tensorflow machine-learning keras

我试图在这个 page 中使用相同的代码但我在代码中间遇到错误。

    AttributeError                            Traceback (most recent call last)
<ipython-input-34-9ff70788070d> in <module>()
----> 1 model = Sequential()
      2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
      3 model.add(LSTM(units=50, return_sequences=False))
      4 model.add(Dense(units=25))
      5 model.add(Dense(units=1))

1 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
     66     """
     67     global _GRAPH_UID_DICTS
---> 68     graph = tf.get_default_graph()
     69     if graph not in _GRAPH_UID_DICTS:
     70         _GRAPH_UID_DICTS[graph] = defaultdict(int)

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

这是我的导入列表:

    #Import the libraries
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import LSTM, Dense, RepeatVector, Masking, TimeDistributed
from tensorflow. keras.utils import plot_model
import quandl
import numpy as np 
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
import pandas_datareader as web

from sklearn.model_selection import train_test_split
import math
from keras.models import Sequential


from keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
#import tensorflow as tf
#newinv=inventory+str(add)
from tensorflow.keras.layers import Embedding

from matplotlib import pyplot as plt
python tensorflow machine-learning keras

更新:根据 Giorgos 的回答编辑代码后,不,我得到这是错误:

NameError                                 Traceback (most recent call last)
<ipython-input-20-9ff70788070d> in <module>()
----> 1 model = Sequential()
      2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
      3 model.add(LSTM(units=50, return_sequences=False))
      4 model.add(Dense(units=25))
      5 model.add(Dense(units=1))

NameError: name 'Sequential' is not defined

这是我的导入列表:

       import math
import pandas_datareader as web
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import quandl
import tensorflow as tf
model = tf.keras.Sequential()
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

这是我收到错误的地方:

#Build the LSTM network model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=25))
model.add(Dense(units=1))

最佳答案

如果您使用tf.keras,而不是

from keras.models import Sequential
from keras.layers import Dense, LSTM

使用以下内容:

import tensorflow as tf


model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(tf.keras.layers.LSTM(units=50, return_sequences=False))
model.add(tf.keras.layers.Dense(units=25))
model.add(tf.keras.layers.Dense(units=1))

还要确保删除旧的导入 from keras.models import Sequential ,以便 Sequential() 在命名空间中不会被覆盖。这同样适用于 from keras.layers import Dense, LSTM

关于python - 模块 'tensorflow' 没有带有股票预测的属性 'get_default_graph',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61123134/

相关文章:

python - 如何创建一个传递给作为文件打开的python对象

python - 如何将 Jinja2 添加到我的cherrypy 项目中?

python - 如何使用 TensorFlow 在 ROI 周围创建边界框

machine-learning - Pytorch,即使在最简单的网络上也无法运行backward()而不出现错误

python - 在神经网络中对输入序列进行二进制编码或填充?

python - doc2vec 获取最相似的文档

python - Theano - Keras - 没有名为 `pool` 的模块

python - 说这个算法是 O(n+m) 正确吗?

tensorflow - 如何在Tensorflow中使用LSTM模型生成例句?

python - ValueError : features should be a dictionary of `Tensor` s. 给定类型:<class 'NoneType' >