tensorflow.keras can't import Activation - tensorflow

just installed tensorflow-gpu via:
conda install --yes tensorflow-gpu==1.12.0
Now when I run from tensorflow.keras import layers into the error:
ImportError: cannot import name 'Activation'
I tried removing tf and keras then reinstalling tf, but hasn't helped.

This is due to a change in 1.12.0
As seen below; in 1.11, tensorflow uses tensorflow.python.keras.activations
https://github.com/tensorflow/tensorflow/blob/r1.11/tensorflow/python/keras/layers/advanced_activations.py
However in 1.12, it doesn't exist anymore;
https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/python/keras/layers/advanced_activations.py
So, I think you can directly call the activation function as;
keras.layers.{activation_function}
e.g. keras.layers.LeakyReLU
Alternatively, you can downgrade.

As #Amir replied, use tensorflow.python.keras. That worked for me!

Related

How to overcome "No module named 'keras'" while importing tensoflow_hub

I am using Anaconda and Jupyter Notebook. I need a tensorflow_hub to import the ResNet model, however, I get the error No module named Keras. I've installed keras and tensorflow_hub in Anaconda. I have no issues when I am using keras.
This "AlreadyExistsError: Another metric with the same name already exists." error shows up when you have a mismatch version of TensorFlow and Keras installed in your system.
To verify this, please run below code in jupyter notebook:
import tensorflow as tf
print(tf.__version__)
import keras
print(keras.__version__)
You can resolve this error by upgrading the TensorFlow to the latest version as this error has been resolved in the latest TF releases - 2.7, 2.8:
!pip install --upgrade tensorflow
and import Keras from TensorFlow like this:
from tensorflow import keras
Now you can install and import tensorflow_hub using the below code without any error:
!pip install tensorflow_hub
import tensorflow_hub

ModuleNotFoundError: No module named 'keras' when using tensorflow 2.6

I created a new conda env with
conda create --name tf tensorflow=2.6
and tried to compile
import tensorflow as tf
model = tf.keras.models.Sequential()
resulting in ModuleNotFoundError: No module named 'keras'
conda install keras
doesn't change anything.
I could go with
from tensorflow.keras.models import Sequential
model = Sequential()
but when I
pip install tensorflow-addons
and
from tensorflow_addons.seq2seq.sampler import TrainingSampler
I end up with the same error
uninstalling tensorflow, installing just keras and trying
from keras.models import Sequential
model = Sequential()
results in the same error
my versions are
tensorflow = 2.6
keras = 2.6
tensorflow-adons = 0.14
What turned out is that I had both keras and keras nightly installed, the problem got resolved after uninstalling keras-nightly. If anyone encounters this check your conda list and pip list for duplicate keras installations

Why can't I access the keras.initializers class?

I'm trying to use the HeUniform() method from the keras.initializers class, bu when I go to run my code, I get the following output:
AttributeError: module 'tensorflow_core.keras.initializers' has no attribute 'HeUniform'
The code that I ran is the following:
init = tf.keras.initializers.HeUniform()
My imports are:
import gym
import tensorflow as tf
import numpy as np
from tensorflow import keras
from collections import deque
import time
import random
I'm using tensorflow version 2.0.0 installed from conda. I tried to upgrade the version, but I was getting further errors saying that version 2.2.0 and 2.4.1 does not exist for conda. Please advise.
Take a look at the tensorflow api documentation for version 2.0. There is no class tf.keras.initializers.HeUniform. Instead, there is a function tf.keras.initializers.he_uniform. You can find the source code for the function on GitHub.
TensorFlow recommends installing with pip. In the past, I have created a new conda environment specifically for tensorflow. It is bad practice to mix conda and pip packages, so that's why I create a new environment just for the specific version of tensorflow.
conda create -n tf2 python=3.8
conda activate tf2
python -m pip install --no-cache-dir tensorflow==2.4.1
Just keep in mind that the GPU versions depend on specific versions of CUDA and cuDNN. See https://www.tensorflow.org/install/source#gpu for more information about the requirements.

Colab's TensorFlow 2 Changes back to Tensorflow 1 After Runtime Restart

I restarted TF from 1.15 to TF2 using the command:
!pip install tensorflow==2.0
import tensorflow as tf
print(tf.__version__)
However, when I restart my colab notebook ( running above cell commenting out the first line),
It say my TF version is 1.15?
What should I do so that tensorflow remain at version 2?
Thank you,
CS
Magic command is an option as well:
try:
# Use the %tensorflow_version magic if in colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
Found the code from Udacity example: https://colab.research.google.com/github/tensorflow/examples/blob/master/courses/udacity_intro_to_tensorflow_for_deep_learning/l02c01_celsius_to_fahrenheit.ipynb#scrollTo=-ZMgCvSRFqxE
Put your installation command ! pip install tensorflow==2.0 in its own code cell and the imports in a separate cell after the installation cell.
If you wish your installation to be saved, save it to google drive, and do as follows:
After running ! pip install tensorflow==2.0, in a cell below it run;
from google.colab import drive
drive.mount('/content/drive')
! pip freeze --local > /content/drive/My\ Drive/installed.txt
#`then in another cell run` (this is what you'll need to run always to restore your installations)
from google.colab import drive
drive.mount('/content/drive')
! pip install --upgrade --force-reinstall `cat/content/drive/My\ Drive/installed
Then follow the link to get the access token to mount your drive.
from future import absolute_import, division, print_function, unicode_literals
try:
# Use the %tensorflow_version magic if in colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
import numpy as np
import logging
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

ImportError: No module named 'tensorflow.contrib.data'

My tensorflow version is 1.1.0
I try to import some file:
strong textfrom tensorflow.contrib.data import Dataset, Iterator
and got error :
ImportError: No module named 'tensorflow.contrib.data'
So, what is solution of this?
tf.contrib.data has been deprecated and been removed (Check here). So, in order to import "Dataset" and "iterator", follow the following steps:
You need to upgrade the tensorflow version using:
sudo pip3 install --upgrade tensorflow.
Check the installation guide here
Open the python terminal and type
import tensorflow as tf
dataset = tf.data.Dataset
Hope it will help.