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

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.

Related

cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'

I am trying to build a ANN model using Tensorflow library on Spyder. Afte ı set my training and test data, ı imported the keras library as seen below
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
But the prcocess did not finished normally.I tookthe same error-> cannnot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'
I am using the following versions Spyder 4.2.5 tensorflow 2.7
Please create a new virtual environment in ananconda to install TensorFlow:
conda create -n tf tensorflow #"tf" is the name for your TensorFlow environment
conda activate tf # to activate the virtual environment
pip install tensorflow # install tensorlfow in that environment
Select the same "tf" environment in anaconda navigator and install Spyder to launch and type below code to check if TensorFlow installed properly:
import tensorflow as tf
Now please try again executing your code in this Spyder IDE.

import tensorflow as tf error occur in Anaconda Navigator

while importing tenserflow module in anaconda notebook I'm facing this error numpy.core.multiarray failed to import
Welcome to StackOverflow.
Please consider reviewing "How do i ask a good question" guidelines to provide more context.
With your question, it is likely you have an older version of numpy installed in Anaconda through pip. Try using anaconda environments and install tensorflow directly via this guide.
The commands you should run are to install the current release of CPU-only TensorFlow, recommended for beginners are:
conda create -n tf tensorflow
conda activate tf

Dataset of MNIST not found from Tensorflow

I have a python script whose initial lines read
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
When I run the script it gives this error
'no module such as tensorflow.tutorials.examples.mnist'.
I have already pip installed Tensorflow, what do i do now?
From your comment I can see that you are using Anaconda. Have you tried to install it with conda install tensorflow? Also, you could try to create an environment first, and install tensorflow afterwards with conda create -n tensorflow pip python=2.7 (or 3.x depending on your system) within it.

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.

Why spyder can't import tensorflow

I have installed tensorflow with anaconda in Windows, but I can't import tensorflow in spyder IDE?
The reason Spyder is unable to import Tensorflow is that Anaconda does not store the tensorflow package in the same environment.
One solution is to create a new separate environment in Anaconda dedicated to TensorFlow and containing Python with its own Spyder and then install tensorflow into that environment