'ReduceLROnPlateau' object has no attribute '_implements_train_batch_hooks' - python

I'm trying to implement learning rate scheduling in my convolutional neural network for which I implemented the ReduceLROnPlateau method but I encounter this error.
My list of imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
np.random.seed(0)
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.utils.np_utils import to_categorical # convert to one-hot-encoding
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.activations import selu
The code I'm using to implement it:
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,patience=5, min_lr=0.001)
The full error that I encounter
My code is working without the learning rate scheduler and this is the error I get whenever I try to callback this particular scheduler.
Thank you

Changing
from keras.callbacks import ReduceLROnPlateau to
from tensorflow.keras.callbacks import ReduceLROnPlateau
might resolve your error.
Most probably it is happening because you are mixing tensorflow and keras imports.

Related

cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'

i'm trying to import these :
from numpy import array
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers.core import Activation, Dropout, Dense
from keras.layers import Flatten, LSTM
from keras.layers import GlobalMaxPooling1D
from keras.models import Model
But i'm getting error as cannot import name 'pad_sequences' from 'keras.preprocessing.sequence'
Can anyone help me here please?
Replace:
from keras.preprocessing.sequence import pad_sequences
With:
from keras_preprocessing.sequence import pad_sequences
you can use this. It is worked for me.
from tensorflow.keras.preprocessing.sequence import pad_sequences
According to the TensorFlow v2.10.0 doc, the correct path to pad_sequences is tf.keras.utils.pad_sequences. So in your script one should write:
from keras.utils import pad_sequences
It has resolved the problem for me.
most likely you are using tf version 2.9 - go back to 2.8 and the same path works
alternatively import it from keras.utils.data_utils import pad_sequences
TF is not so stable with paths - the best way is check their git source corresponding to the version you succeeded to install !! in the case of TF2.9 you can see how it is importedhere
You can use the following;
from tensorflow.keras.preprocessing.sequence import pad_sequences
The correct path to import is keras.io.preprocessing.sequence.pad_sequences. Your path lacks the io.
from keras.io.preprocessing.sequence import pad_sequences
I came across the same problem just now but still don't know what is going on(still waiting for an answer).
I gave up importing pad_sequences and write it in full and it works
keras.preprocessing.sequence.pad_sequences()
In their last update Kiras 2.11.0 they made few changes and improvements to their packages.
Considering your issue you should:
replace this:
from keras.preprocessing.sequence import pad_sequences
with this:
from keras_preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing import sequence
worked for me
from keras.utils.data_utils import pad_sequences
use this instead.

Unable to import SGD and Adam from 'tensorflow.python.keras.optimizers'

Trying to run---
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Flatten, Dense
from tensorflow.python.keras.optimizers import SGD, Adam
import numpy as np
print(tf.__version__)
I get this error---
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-f05f8f753c47> in <module>()
4 from tensorflow.python.keras.models import Sequential
5 from tensorflow.python.keras.layers import Flatten, Dense
----> 6 from tensorflow.python.keras.optimizers import SGD, Adam
7
8 import numpy as np
ImportError: cannot import name 'SGD' from 'tensorflow.python.keras.optimizers' (/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/optimizers.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
---------------------------------------------------------------------------
I'm studying machine learning in Google Colab.
I pasted the example code and run it, and get error message.
I could find similar errors in Google, but I couldn't find anything to solve this problem.
I tried 'from tensorflow.keras.optimizers import SGD, Adam', 'from tf.keras.optimizers import SGD, Adam', and 'from keras.optimizers import SGD, Adam'.
But everything didn't work.
try this:
from tensorflow.python.keras.optimizer_v1 import SGD
You need to mention the exact updated alias name while importing the model(Sequential),layers (Flatten, Dense) and optimizers (SGD, Adam).
Please try again using the below code in new Google Colab notebook.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential #removed python from each layer
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras.optimizers import SGD, Adam
import numpy as np
print(tf.__version__)
Output:
2.8.2

ValueError: The name "dense_1" is used 2 times in the model. All layer names should be unique

I'm trying to do Transfer Learning but I found with this error and I can't fix it. Can anybody help me please?
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense
from keras.models import load_model, Model
from keras import backend as K
from keras.datasets import mnist
from keras.utils import np_utils
Modelo = load_model('Mono64_9.h5')
x=Modelo.output
x=Dense(512, activation='relu')(x)
x=Dense(256, activation='relu')(x)
x=Dense(128, activation='relu')(x)
preds=Dense(3,activation='softmax')(x)
model=Model(inputs=Modelo.input,outputs=preds)
ValueError: The name "dense_1" is used 2 times in the model. All layer names should be unique.
What is most likely happening is that your loaded model already has Dense layers which are named using the generic defaults for the Dense layers when you construct them (i.e. dense_1, dense_2, etc.). I'm unsure of what you're trying to do in your transfer learning task - whether you are adding more Dense layers to your network, or if you want to remove the Dense layers and add new ones.
Either way, to make this particular version of your code run, you'll need to add an additional name attribute to uniquely name the new layers.
Something like this could work:
import numpy as np
import os
import keras
import matplotlib.pyplot as plt
from keras.layers import Dense
from keras.models import load_model, Model
from keras import backend as K
from keras.datasets import mnist
from keras.utils import np_utils
Modelo = load_model('Mono64_9.h5')
x=Modelo.output
x=Dense(512, activation='relu', name="dense_a")(x) # New
x=Dense(256, activation='relu', name="dense_b")(x) # New
x=Dense(128, activation='relu', name="dense_c")(x) # New
preds=Dense(3,activation='softmax', name="dense_output")(x) # New
model=Model(inputs=Modelo.input,outputs=preds)
Its very likely that the model Mono64_9.h5 has a layer called dense_1, just because that is the first auto-generated name for dense layers. You can explicitly give names to your new layers so they do not clash with the original model, like:
x=Dense(512, activation='relu', name='dense_10')(x)
x=Dense(256, activation='relu', name='dense_11')(x)
x=Dense(128, activation='relu', name='dense_12')(x)
preds=Dense(3,activation='softmax', name='dense_out')(x)
You can give layers any name, they are arbitrary and just for the user to refer to layers by name.

How to import keras.engine.topology in Tensorflow?

I want to import keras.engine.topology in Tensorflow.
I used to add the word tensorflow at the beginning of every Keras import if I want to use the Tensorflow version of Keras.
For example: instead of writing:
from keras.layers import Dense, Dropout, Input
I just write the following code and it works fine :
from tensorflow.keras.layers import Dense, Dropout, Input
But that's not the case for this specific import:
from tensorflow.keras.engine.topology import Layer, InputSpec
And I m getting the following error message:
No module named 'tensorflow.keras.engine'
You can import Layer and InputSpec from TensorFlow as follows:
from tensorflow.python.keras.layers import Layer, InputSpec
UPDATE: 30/10/2019
from tensorflow.keras.layers import Layer, InputSpec
In the keras_vggface/models.py file, change the import from:
from keras.engine.topology import get_source_inputs
to:
from keras.utils.layer_utils import get_source_inputs
In order to import keras.engine you may try using:
import tensorflow.python.keras.engine
Note: But from tensorflow.python.keras.engine you cannot import topology
I solved this issue by changing the import from from keras.engine.topology import get_source_inputs to from keras.utils.layer_utils import get_source_inputs

How to import keras from tf.keras in Tensorflow?

import tensorflow as tf
import tensorflow
from tensorflow import keras
from keras.layers import Dense
I am getting the below error
from keras.layers import Input, Dense
Traceback (most recent call last):
File "<ipython-input-6-b5da44e251a5>", line 1, in <module>
from keras.layers import Input, Dense
ModuleNotFoundError: No module named 'keras'
How do I solve this?
Note: I am using Tensorflow version 1.4
Use the keras module from tensorflow like this:
import tensorflow as tf
Import classes
from tensorflow.python.keras.layers import Input, Dense
or use directly
dense = tf.keras.layers.Dense(...)
EDIT Tensorflow 2
from tensorflow.keras.layers import Input, Dense
and the rest stays the same.
Try from tensorflow.python import keras
with this, you can easily change keras dependent code to tensorflow in one line change.
You can also try from tensorflow.contrib import keras. This works on tensorflow 1.3
Edited: for tensorflow 1.10 and above you can use import tensorflow.keras as keras to get keras in tensorflow.
To make it simple I will take the two versions of the code in keras and tf.keras. The example here is a simple Neural Network Model with different layers in it.
In Keras (v2.1.5)
from keras.models import Sequential
from keras.layers import Dense
def get_model(n_x, n_h1, n_h2):
model = Sequential()
model.add(Dense(n_h1, input_dim=n_x, activation='relu'))
model.add(Dense(n_h2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model
In tf.keras (v1.9)
import tensorflow as tf
def get_model(n_x, n_h1, n_h2):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(n_h1, input_dim=n_x, activation='relu'))
model.add(tf.keras.layers.Dense(n_h2, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
return model
or it can be imported the following way instead of the above-mentioned way
from tensorflow.keras.layers import Dense
The official documentation of tf.keras
Note: TensorFlow Version is 1.9
Its not quite fine to downgrade everytime, you may need to make following changes as shown below:
Tensorflow
import tensorflow as tf
#Keras
from tensorflow.keras.models import Sequential, Model, load_model, save_model
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D, Embedding
from tensorflow.keras.layers import GRU, Bidirectional, BatchNormalization, Reshape
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import ModelCheckpoint
The point is that instead of using
from keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
you need to add
from tensorflow.keras.layers import Reshape, Dropout, Dense,Multiply, Dot, Concatenate,Embedding
Starting from TensorFlow 2.0, only PyCharm versions > 2019.3 are able to recognise tensorflow and keras inside tensorflow (tensorflow.keras). Francois Chollet himself (author of Keras) recommends that everybody switches to tensorflow.keras in place of plain keras.
There is also one important mentioning here:
IMPORTANT NOTE FOR TF >= 2.0
There (this) is an ongoing issue with JetBrains (in fact from TensorFlow side), it seems that this error comes up from time to time (https://youtrack.jetbrains.com/issue/PY-53599).
It sometimes happens that PyCharm is not able to correctly import/recognize keras inside tensorflow or other imports.
Depending on Python + TF + PyCharm versions, you may have to alternate between the following import types:
from tensorflow.keras.models import Model
OR
from tensorflow.python.keras.models import Model
this worked for me in tensorflow==1.4.0
from tensorflow.python import keras
I have a similar problem importing those libs. I am using Anaconda Navigator 1.8.2 with Spyder 3.2.8.
My code is the following:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import math
#from tf.keras.models import Sequential # This does not work!
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import InputLayer, Input
from tensorflow.python.keras.layers import Reshape, MaxPooling2D
from tensorflow.python.keras.layers import Conv2D, Dense, Flatten
I get the following error:
from tensorflow.python.keras.models import Sequential
ModuleNotFoundError: No module named 'tensorflow.python.keras'
I solve this erasing tensorflow.python
With this code I solve the error:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import math
#from tf.keras.models import Sequential # This does not work!
from keras.models import Sequential
from keras.layers import InputLayer, Input
from keras.layers import Reshape, MaxPooling2D
from keras.layers import Conv2D, Dense, Flatten
I had the same problem with Tensorflow 2.0.0 in PyCharm. PyCharm did not recognize tensorflow.keras; I updated my PyCharm and the problem was resolved!

Categories