module 'tensorflow' has no attribute 'random' - python

i'm trying to generate seed using TensorFlow 2.1 but this error appears module 'TensorFlow' has no attribute 'random'

I suspect that you think you're using TensorFlow 2.0 but actually, it is a previous version of tensorflow.
It's easy to check:
import tensorflow as tf
print(tf.__version__)
This should produce output like this:
2.1.0
If not, you know you have the wrong TensorFlow version installed.
It's also possible that you have multiple versions of python installed. For example:
Python 3.6 might be installed with TensorFlow 1.x, and
Python 3.7 might be installed with TensorFlow 2.x.
In this case, just be careful that the version of Python being used by Jupyter Notebook is the version of Python with TensorFlow 2.x installed.

just want to make sure
run this command then restart the runtime pip install --upgrade tensorflow
and then try that code again

Related

Issues with getting cuda to work on torch, importing torch and cuda modules into Python

I'm having some basic issues running the torch and cuda modules in my Python script.
I think that this has something to do with the different versions of Python that I have installed. I have two versions of Python installed:
I think I have torch and cuda installed for the wrong one or something. I don't know how to fix this.
Per the Pytorch website, I installed torch as follows:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
I've installed cuda as follows: pip3 install cuda-python==11.7 (It doesn't matter if I use the newest version -- I still get the same results.)
When I look up the versions, I get the following:
So it seems like it is all installed correctly.
However, if I run the following:
import torch
print(torch.cuda.is_available())
I get False.
If I try to run my code that uses torch, I get the following error:
I don't get what I'm doing wrong. As far as I can tell, I've installed torch with CUDA enabled. So I'm not sure why it's telling me otherwise.
Any ideas? Thanks for any help.
Edit: Here is the info for my GPU:
The issue had to do with the different versions of Python. I ended up using a Conda virtual environment, which solved this issue by ensuring I was using the correct version of Pytorch.

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?
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

failed to install tensorflow in anaconda

Hi
in jupyter notebook, when i tried to import tensorflow like below, i got an error of ModuleNotFoundError: No module named 'tensorflow'. Then, I opened anaconda prompt and trying to install tensorflow in anaconda and it failed many times and here is the error message. Could you please help me what is the problem? Thanks
import tensorflow as tf
ok, so i tried to downgrade python from 3.8 to 3.6 and i still got an error at the end, could anyone help pls? Thanks
EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
environment location: C:\ProgramData\Anaconda3
According to TensorFlow docs, tf is compatible with python 3.5, 3.6, 3.7 or 3.8
and also python 3 64bit release is a must, so it's maybe worth to check if you don't try to install tf accidentally on 32bit version of python.
https://www.tensorflow.org/install/pip#windows
import sys
print(sys.version)
I installed tf according to Anaconda docs:
create fresh env,
activate it,
and install tf:
conda create -n myproject tensorflow
conda activate myproject
conda install tensorflow
I can see that conda automatically selected python version 3.7.9
verify:
import tensorflow as tf
tf.version.VERSION
your python version is not compatible with this version of tensorflow. you are using python 3.8 downgrade python to either 3.5, 3.6 or 3.7. downgrading python should solve your issue.

no module named keras after installing keras

I'm using anaconda ver 3, and I have installed python as well separately from anaconda. I installed python ver 2.7 and ver 3.6 from python website.
Now, I have installed keras from anaconda command prompt by using conda install keras. However, when I open jupyter notebook and write :
import keras
it says :
no module named keras
I also tried importing tensorflow but it gave me the same error
As far as i know, keras is a version of tensorflow. You should try installing tensorflow instead and then run
import tensorflow as tf
tf.__version__
if you get '2.1.0' or any 2., you should be all set!
EDIT1: Keras is part of tensorflow not a version (as pointed out in the comments).
EDIT2: The link below gives good details on environments activation/creation.
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Okay so what I did is first updated anaconda by using :
conda update conda
and then I installed python :
conda install python=3.7
and finally I installed keras again :
conda install keras
And now I can import both packages of keras and tenserflow

tf.estimator.DNNClassifier not available

I am trying to work with tensorflow, but the DNNClassifier (and the other estimators) are not available from tf.estimator.
I am running Python 3.6 within Anaconda on Windows 10. I used conda install tensorflow to load and import tensorflow as tf to import the package.
When I try to use DNNClassifier, I get the error message:
AttributeError: module 'tensorflow.python.estimator.estimator_lib' has no attribute 'DNNClassifier'
Have any others had this problem?
Install using pip. Install the CPU version unless you have an nvidia GPU.

Categories

Resources