<p><code>imblearn.keras.balanced_batch_generator</code> was pointing to an old version of <code>keras.utils.Sequence</code> and causing this error.</p>
<p>I just found that imblearn also has a tensorflow version of balanced_batch_generator, so I switched to <code>tensorflow.keras.utils.Sequence</code>
and the <code>imblearn.tensorflow.balanced_batch_generator</code>, and it seems to import without any errors.</p>
<p>Now let's see if I can finally make progress.</p>

CC BY-SA 4.0
2022-04-23T18:52:16.107Z
2022-04-23T18:52:16.107Z
Jennifer Crosby
<p>I am using Google Colab. tensorflow version = 2.8.0, and keras is the same.  I am trying to get a <code>BalancedDataGenerator(Sequence)</code> class created, but I can't get <code>keras.utils.Sequence</code> to load.</p>
<pre><code>from tensorflow.python.keras.utils import Sequence
from tensorflow.python.keras.utils.np_utils import Sequence
from tensorflow.python.keras.utils.all_utils import Sequence
</code></pre>
<p>I've tried it taking out &quot;python&quot;, or taking out &quot;tensorflow.python&quot;, or searching as to where it is now currently located, but haven't found it.</p>
<p>The errors I get are:</p>
<blockquote>
<p>ImportError: cannot import name 'Sequence' from
'tensorflow.python.keras.utils'
(/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/<strong>init</strong>.py)</p>
<p>AttributeError: module 'keras.utils' has no attribute 'Sequence'</p>
<p>ImportError: cannot import name 'Sequence' from 'keras.utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/<strong>init</strong>.py)</p>
<p>ImportError: cannot import name 'Sequence' from
'keras.utils.all_utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/all_utils.py)</p>
<hr />
<p>NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.</p>
</blockquote>
<p>When I go /usr/local/lib/python3.7/dist-packages/keras/utils and read all_utils.py I see 'from keras.utils.data_utils import Sequence' as an option and it says that this all_utils.py module is 'used as a shortcut to access all the symbols.  Those symbols werd exposed under <strong>init</strong>, and was causing some hourglass import issue.'</p>
<p>If I read data_utils.py I do see the Sequence module inside there.<br />
if I type from 'keras.utils.data_utils import Sequence' in a cell and run it it looks like it was accepted.  However, if I put that in the balanceddatagenerator.py and import the class I created that uses Sequence, and then import the class I get this error.  I don't know how to overcome this.</p>
<p>How get this installed and imported successfully?</p>

CC BY-SA 4.0
2022-04-23T18:46:14.083Z
I just had an error message that indicated that "from imblearn.keras import balanced_batch_generator" was not finding Sequence in keras.utils.Sequence.  And I know that keras.utils.data_utils.Sequence is where it is located.  So, it looks like since I have an updated version of Keras, that they put Sequence in a different place. Why they seem to keep moving Sequence's location, I don't know, but it is causing problems between versions.
Jennifer Crosby
CC BY-SA 4.0
2022-04-23T18:46:51.940Z
I just found that imblearn also has a tensorflow version of balanced_batch_generator, so switch to tensorflow.keras.utils.Sequence and use the imblearn.tensorflow.balanced_batch_generator, and it seems to import without any errors.  Now let's see if I can finally make progress.
Jennifer Crosby
CC BY-SA 4.0
2022-04-23T21:08:46.687Z
Hi Jennifer. Welcome to our community! It seems that you were having a programming issue. This type of question is off-topic and better asked on Stack Overflow, even though it's in the context of AI. We focus on the **theoretical**, **philosophical** and **social** aspects of AI. See https://ai.stackexchange.com/help/on-topic
nbro
CC BY-SA 4.0
2022-04-25T19:24:20.017Z
That is funny, @nbro, I was told over there that I should post all my Machine Learning and Data Science questions here in Stackexchange.
Jennifer Crosby
CC BY-SA 4.0
2022-04-25T19:35:52.423Z
Some people think that our site also covers programming issues, but that's not the case. Programming questions should really be asked on Stack Overflow. There you will find [many programming questions related e.g. to Keras and TensorFlow](https://stackoverflow.com/questions/tagged/tensorflow).
nbro
<tensorflow><keras>
can't find a viable import class for keras.utils.Sequence
Jennifer Crosby
# can't find a viable import class for keras.utils.Sequence

(Asked by: username_0 on 2022-04-23)

I am using Google Colab. tensorflow version = 2.8.0, and keras is the same.  I am trying to get a BalancedDataGenerator(Sequence) class created, but I can't get keras.utils.Sequence to load.
from tensorflow.python.keras.utils import Sequence
from tensorflow.python.keras.utils.np_utils import Sequence
from tensorflow.python.keras.utils.all_utils import Sequence

I've tried it taking out "python", or taking out "tensorflow.python", or searching as to where it is now currently located, but haven't found it.
The errors I get are:

ImportError: cannot import name 'Sequence' from
'tensorflow.python.keras.utils'
(/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/init.py)
AttributeError: module 'keras.utils' has no attribute 'Sequence'
ImportError: cannot import name 'Sequence' from 'keras.utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/init.py)
ImportError: cannot import name 'Sequence' from
'keras.utils.all_utils'
(/usr/local/lib/python3.7/dist-packages/keras/utils/all_utils.py)

NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

When I go /usr/local/lib/python3.7/dist-packages/keras/utils and read all_utils.py I see 'from keras.utils.data_utils import Sequence' as an option and it says that this all_utils.py module is 'used as a shortcut to access all the symbols.  Those symbols werd exposed under init, and was causing some hourglass import issue.'
If I read data_utils.py I do see the Sequence module inside there.
if I type from 'keras.utils.data_utils import Sequence' in a cell and run it it looks like it was accepted.  However, if I put that in the balanceddatagenerator.py and import the class I created that uses Sequence, and then import the class I get this error.  I don't know how to overcome this.
How get this installed and imported successfully?

**Tags:** tensorflow, keras

> **Comments:**
> **username_0**: I just had an error message that indicated that "from imblearn.keras import balanced_batch_generator" was not finding Sequence in keras.utils.Sequence.  And I know that keras.utils.data_utils.Sequence is where it is located.  So, it looks like since I have an updated version of Keras, that they put Sequence in a different place. Why they seem to keep moving Sequence's location, I don't know, but it is causing problems between versions.  
> **username_0**: I just found that imblearn also has a tensorflow version of balanced_batch_generator, so switch to tensorflow.keras.utils.Sequence and use the imblearn.tensorflow.balanced_batch_generator, and it seems to import without any errors.  Now let's see if I can finally make progress.  
> **username_1**: Hi Jennifer. Welcome to our community! It seems that you were having a programming issue. This type of question is off-topic and better asked on Stack Overflow, even though it's in the context of AI. We focus on the **theoretical**, **philosophical** and **social** aspects of AI. See https://ai.stackexchange.com/help/on-topic  
> **username_0**: That is funny, @username_1, I was told over there that I should post all my Machine Learning and Data Science questions here in Stackexchange.  
> **username_1**: Some people think that our site also covers programming issues, but that's not the case. Programming questions should really be asked on Stack Overflow. There you will find [many programming questions related e.g. to Keras and TensorFlow](https://stackoverflow.com/questions/tagged/tensorflow).  

---

## 1 answers

### Answer by username_0 on 2022-04-23. Score: 0 | Accepted answer

`imblearn.keras.balanced_batch_generator` was pointing to an old version of `keras.utils.Sequence` and causing this error.

I just found that imblearn also has a tensorflow version of balanced\_batch\_generator, so I switched to `tensorflow.keras.utils.Sequence`
and the `imblearn.tensorflow.balanced_batch_generator`, and it seems to import without any errors.

Now let's see if I can finally make progress.


---

