'Can't I simply copy and paste a program from a Jupyter notebook to a file.py?

It's weird, I wrote a functioning program on a Jupyter notebook and I wanted to have it in a normal python file with VSCode aswell. However, while copying and pasting the exact same code that doesn't give me any troubles in Jupyter, I get the message from VSCode:

module 'keras.engine.base_layer' has no attribute 'BaseRandomLayer'

In the line

import tensorflow as tf

I am, of course, in the same environment (anaconda with python 3.9.7) Tensorflow version: 2.8.0

Any ideas?



Solution 1:[1]

This could be due to the multiple versions of Tensorflow or mismatching version of Tensorflow with Keras or other dependencies.

You can create a new environment and install latest tensorflow using.

pip install tensorflow==2.9.0

You can import all the packages using Tensorflow using below lines.

import tensorflow
from tensorflow import keras

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tfer3