C4 W2 A2:Module 'tensorflow.keras.layers' has no attribute 'RandomFlip'

This is how I have written the code.

def data_augmenter():
‘’’
Create a Sequential model composed of 2 layers
Returns:
tf.keras.Sequential
‘’’
### START CODE HERE
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip(“horizontal”),
tf.keras.layers.RandomRotation(0.2)
])
### END CODE HERE

return data_augmentation

1 Like

Same Error for Me!
It seems to be an issue with the python version we are using.
In the latest TensorFlow version it is working fine…
But, no idea how to make this work in the Jupyter Notebooks.

1 Like

I do not see any error messages or asserts.
What problem are you asking about?

1 Like

This is the error I am getting.

AttributeError Traceback (most recent call last)
in
----> 1 augmenter = data_augmenter()
2
3 assert(augmenter.layers[0].name.startswith(‘random_flip’)), “First layer must be RandomFlip”
4 assert augmenter.layers[0].mode == ‘horizontal’, “RandomFlip parameter must be horizontal”
5 assert(augmenter.layers[1].name.startswith(‘random_rotation’)), “Second layer must be RandomRotation”

in data_augmenter()
9 ### START CODE HERE
10 data_augmentation = tf.keras.Sequential([
—> 11 tf.keras.layers.RandomFlip(mode=horizontal),
12 tf.keras.layers.RandomRotation(0.2)
13 ])

AttributeError: module ‘tensorflow.keras.layers’ has no attribute ‘RandomFlip’

{moderator edit: code removed}

1 Like

Hey, @sandeep911 , @SairajNeelam found a solution, don’t use tf.keras.layers, look at the top RandomFlip and RandomRotation have already been imported, just use it as described above the graded code cell, and one more thing use tf.keras.models.Sequential() otherwise it will give ’ “‘module’ object is not callable. keras” error.

So, ur code should be
{mentor edit: code removed}

18 Likes

Thank you @Aditya_Singh .

It worked like a charm.

But, it’s strange that RandomFlip from tf.keras.layers.RandomFlip is not working!

1 Like

Yeah, it is not working, maybe I guess it’s because of the tensorflow version.

1 Like

Hey @Aditya_Singh thanks for sharing the solution, it works now.
Thanks again.

1 Like

@Aditya_Singh
Do not post the code solutions on the Forum. That breaks the course Honor Code.

The RandomFlip and RandomRotation layers are in a different Keras library, not the standard layers.

‘horizontal’ also needs to be within a set of quotes.

Ok sorry, it won’t happen again, I will take care from here on.

1 Like

one source of confusion…

tf.keras.layers.RandomFlip v2.6

tf.keras.layers.experimental.preprocessing.RandomFlip v2.3.0

3 Likes

The “experimental preprocessing” is what I was referring to.

@Aditya_Singh has it right, the problem with the path is related to TF version. It was refactored between 2.3 and the latest, 2.6

I have multiple virtual environments on my desktop, so I got in the practice of adding these two lines of code in the notebook cell following all the imports. This way I’m always reminded what version is active in the environment:

print(tf.__version__)
print(tf.keras.__version__)

You might want to print out the Python version, too.

Hope this helps

3 Likes

Can someone explain to me, what we shall learn here with such an example?

I’m using in parallel PyCharm with TF, and Keras and there my source code worked smoothly (I got the same error as @SairajNeelam) .

The Asserts there threw no error and I was happy, and then I have to trick around here in the Jupyter NB to get my source code working?

Folks, I don’t get this.

1 Like

It’s not the Jupyter Notebook that causes the issue, it’s the version of TensorFlow on the cloud behind it. Actually a pretty common problem when dealing with the Python/TF/Keras/third party packages world where asynchronous refactoring and non- backward compatible changes are a fact of life. You can either configure a local env to match the versions run by Coursera or branch the source code to support different env configurations. Probably a good exercise to build a local virtual env and keep it synchronized with Coursera AWS (or Google Colab depending on the course and assignment). HTH

I am getting the same AttributeError and I’m not using tf.keras.layers. I am working with the code already in the cell plus the examples given in the tf.keras.Sequential documentation:

This doesn’t seem to be a common problem, so have I made a very basic error?

Please advise

1 Like

@Peter_Darrell , That’s not the right syntax for using add() in the Sequential API. Compare what you have there with the examples in this thread

Thanks ai_curious.

(again)

2 Likes

thanks, I didn’t really noticed it in packages cell, but you significantly saved my time :innocent:)

1 Like