module 'tensorflow' has no attribute 'get_default_graph' with Stocks Prediction

Viewed 3068

I am trying to use the same code in this page but I am getting an error at the middle of the code.

    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'

Here is my import list:

    #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

Update: After editing the code based on Giorgos' answer, no I get this is the error:

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

Here is my import list:

       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')

Here is where I am getting the error:

#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))
2 Answers

If you are using tf.keras, instead of

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

use the following:

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))

And also make sure to remove your older import from keras.models import Sequential so that Sequential() is not overwritten in namespace. Same applies for from keras.layers import Dense, LSTM.

There might be some incompatibilities between your Keras and TensorFlow. The thing that did the trick for me was uninstalling the Keras using sudo pip uninstall keras and re-installing it.

Related