13

I am having an error regarding (Keras that does not support TensorFlow 2.0. We recommend using tf.keras, or alternatively, downgrading to TensorFlow 1.14.) any recommendations.

thanks

import keras
#For building the Neural Network layer by layer
from keras.models import Sequential
#To randomly initialize the weights to small numbers close to 0(But not 0)
from keras.layers import Dense

classifier=tf.keras.Sequential()

classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))




RuntimeError: It looks like you are trying to use a version of multi-backend Keras that does not support TensorFlow 2.0. We recommend using `tf.keras`, or alternatively, downgrading to TensorFlow 1.14.
Dean
  • 187
  • 1
  • 3
  • 11

7 Answers7

14

You should only have to change the imports at the top:

from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras import Sequential

classifier = Sequential()
classifier.add(Dense(6, init = 'uniform', activation = 'relu', input_dim = 11))
nickthefreak
  • 1,450
  • 12
  • 22
  • I have for pointing this out. I have done exactly what you listed up. but I have got the following error TypeError: __init__() missing 1 required positional argument: 'units' Thanks – Dean Nov 28 '19 at 14:17
  • This is an error in the Dense layer construction, different from the import error you had so far (so the code you have supplied above). In short, all layers have a required units parameter that defines the number of neurons. You can see more details in the [documentation](https://keras.io/layers/core/) – nickthefreak Nov 28 '19 at 14:23
  • do you mean units=6 as the input layer classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu', input_dim = 11)) – Dean Nov 28 '19 at 15:23
  • More like `classifier.add(Dense(6, init = 'uniform', activation = 'relu', input_shape = (11,)))`. Input shape needs to be a tuple as per the documentation. This is kind of separate problem, so you might have to open a new question or check for existing examples of MLP implementations using keras. – nickthefreak Nov 28 '19 at 15:34
5

TensorFlow 2.0+ is only compatible with Keras 2.3.0+, so if you wish to use Keras 2.2.5-, you'll need TensorFlow 1.15.0-. Alternatively, yes, you can do from tensorflow.keras import ..., but that will not use your keras package at all and you might as well uninstall it.

OverLordGoldDragon
  • 14,529
  • 6
  • 35
  • 69
  • 1
    There is a big difference between "can" and is actually supported, only Keras 2.3.x supports TensorFlow 2.0, so do not recommend to use 2.2.5 with it. – Dr. Snoopy Nov 28 '19 at 15:13
  • @MatiasValdenegro Good thing there's a second half to that sentence – OverLordGoldDragon Nov 28 '19 at 15:18
  • Yes, that's why I recommend not to mention partially supported TF versions. – Dr. Snoopy Nov 28 '19 at 15:19
  • @MatiasValdenegro If anything, it _explicitly discourages_ using K2.2.5+TF2 - else the user may run it w/o error and think it's fine. But alright, guess I can make it more explicit - answer updated – OverLordGoldDragon Nov 28 '19 at 15:27
  • 1
    No, now I found evidence that Keras 2.2.5 does not actually support TF 2.0, just look at [this commit](https://github.com/keras-team/keras/commit/aa28910799bcd169d28ae1e5b50508c868df7380#diff-a18b8c6a1191d6f49303e0d599ca8c37R69), so just saying "can" is actually wrong. – Dr. Snoopy Nov 28 '19 at 15:36
  • @MatiasValdenegro Hmm... I'm possibly recalling something incorrectly - my mistake, thanks for pointing it out; fixed – OverLordGoldDragon Nov 28 '19 at 15:38
2

if you want to use tensorflow 2.0+ you must have keras 2.3+
try to upgrade your keras it works for me :

pip install -U keras

or you may specify the keras version to 2.3

hossein hayati
  • 822
  • 2
  • 11
  • 24
1

I ran into the same issue. Downgraded my TensorFlow to version 1.14 using the following:

!pip install tensorflow==1.14.0

Fixed the error.

Dharman
  • 21,838
  • 18
  • 57
  • 107
1

first, import tensorflow:

import tensorflow as tf

Next, in place of this,

classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))

use:

classifier.add(tf.keras.layers.Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))

Let me know if it works.

0

this line of code on the first cell worked for me

%tensorflow_version 1.x

lucas
  • 449
  • 1
  • 8
0

I fixed the problem by running

pip install --ignore-installed --upgrade keras
shihs
  • 107
  • 1
  • 6