im trying to import categorical_dqn
when i try the following
from tf_agents.agents.categorical_dqn import categorical_dqn_agent
i get
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
the advice i find around the internet Error importing binary_weighted_focal_crossentropy from keras backend: Cannot import name is to try importing this stuff first
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy
i end up with the exact same error caused by the second line of this suggestion however.
ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (C:\Users\tgmjack\anaconda3\lib\site-packages\keras\backend.py)
####### bonus info ########
im running all this on anaconda
tensorflow version = 2.9.2
tf agents version = 0.5.0
keras version = 2.9.0
im trying to follow this tutorial = https://github.com/tensorflow/agents/blob/master/docs/tutorials/9_c51_tutorial.ipynb
I had a similar problem with tf_agents a few months ago. Doing this fixed it for me:
pip install tf-agents[reverb]
I have the following packages with their respective versions:
keras 2.9.0
tensorflow 2.9.2
tf-agents 0.13.0
I am looking at this workbook which comes from huggingface course. I dont have internet access from my python environment but I could download files and save them in python environment. I copied all file from this folder and saved it in the folder bert-base-uncased/. I renamed some of the files to match what is in the above folder
I have below packages
tensorflow.__version__
'2.2.0'
keras.__version__
'2.4.3'
Then I installed transformers
!pip install datasets transformers[sentencepiece]
checkpoint = "bert-base-uncased/"
from transformers import TFAutoModelForSequenceClassification
Successfully installed datasets-1.17.0 dill-0.3.4 fsspec-2022.1.0 huggingface-hub-0.4.0 multiprocess-0.70.12.2 pyarrow-6.0.1 sacremoses-0.0.47 sentencepiece-0.1.96 tokenizers-0.10.3 transformers-4.15.0 xxhash-2.0.2
all the files are available
#files from https://huggingface.co/bert-base-uncased/tree/main
import os
cwd = os.getcwd()
print (cwd)
os.listdir('bert-base-uncased/')
['gitattributes',
'vocab.txt',
'tokenizer_config.json',
'tokenizer.json',
'README.md',
'.dominokeep',
'config.json',
'tf_model.h5',
'flax_model.msgpack',
'rust_model.ot',
'pytorch_model.bin']
But I still get the below error. I don't get this error when I run the same code using google colab
model = TFAutoModelForSequenceClassification.from_pretrained('bert-base-uncased/', num_labels=2)
print ("----------------")
print (type(model))
print ("----------------")
RuntimeError: Failed to import transformers.models.bert.modeling_tf_bert because of the following error (look up to see its traceback):
No module named 'tensorflow.python.keras.engine.keras_tensor'
To replicate Your issue I install TensorFlow version 2.2.0.
Then I tried to import problematic module:
from tensorflow.python.keras.engine import keras_tensor
Yield importError.
Meaning, that TensorFlow version 2.2.0 doesn't have module keras_tensor required by transformer.
Then I updated TensorFlow to version 2.7.0 and try to import keras_tensor module again and everything worked.
Updating TensorFlow to newer version should solve Your issue.
EDIT
Digging a bit for lowest working version of TensorFlow I get to setup.py of Transformers.
Version requirements for TensorFlow is >= 2.3.0.
I am trying to download a TensorFlow dataset yelp polarity reviews
In order to do this I am doing the following command:
tfds.text.YelpPolarityReviews
But this return the following error:
AttributeError: module 'tensorflow_datasets.text' has no attribute
'YelpPolarityReviews'
After this I triyed to download the hole datasets of the following way:
import tensorflow_datasets.download.DownloadManager as dl_manager
train_dir = dl_manager.download_and_extract('https://s3.amazonaws.com/fast-ai-nlp/yelp_review_polarity_csv.tgz')
But this doesn't work and I get the following error:
ModuleNotFoundError: No module named 'tensorflow_datasets.download'
The question is how can I download that dataset in order to use it with TensorFlow?
Thanks
The dataset is yelp_polarity_reviews, you have to add the underscore and make sure everything is in small. Can you try this out and see if it works?
!pip install tensorflow-datasets
import tensorflow as tf
import tensorflow_datasets as tfds
data = tfds.load('yelp_polarity_reviews', split='train', shuffle_files=True)
I installed NumPy, SciPy, Pandas, and Patsy before installing statsmodels, yet when I tried to import statsmodels.api I got the following error message:
ImportError: cannot import name '_initialization' from
'statsmodels.tsa.statespace'
(/Library/Frameworks/Python.framework/Versions/3.8/lib/
python3.8/site-packages/statsmodels/tsa/statespace/__init__.py)
Importing statsmodels by itself works fine. I've tried to resolve this issue by completely uninstalling statsmodels and installing it again as well as installing statsmodels via pip install git+https://github.com/statsmodels/statsmodels, but again no luck.
The versions of everything are as follows:
NumPy: 1.18.1
Pandas: 0.25.3
SciPy: 1.4.1
Patsy: 0.5.1
Statsmodels: 0.11.1
Investigating the error message further, I opened the file __init.py__ within the statespace folder and found the following code:
from statsmodels.tools._testing import PytestTester
test = PytestTester()
Running this code, I get the following error message:
File "/Library/Frameworks/Python.framework/Versions/
3.8/lib/python3.8/site-packages/statsmodels/tools/_testing.py", line 29, in __init__
raise ValueError('Unable to determine path')
ValueError: Unable to determine path
I don't know if this means anything since I simply ran the code all by itself, but this problem has been bugging me for a while: please help!
From the file in the error, https://github.com/statsmodels/statsmodels/blob/f3f89ad777b22e6db565897397ece24d41af5700/statsmodels/tools/_testing.py#L27, it expects a filename. If you're running the file directly it won't work
#temp.py
def foo():
import sys
f = sys._getframe(1)
print("Name:", f.f_locals.get('__file__', None))
$ python3 ./temp.py
>>> Name: None
# temp2.py
import temp
temp.foo()
$ python3 ./temp2.py
>>> Name: "temp2.py"
TensorFlow MNIST example not running with fully_connected_feed.py
I checked this out and realized that input_data was not built-in. So I downloaded the whole folder from here. How can I start the tutorial:
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-6-a5af65173c89> in <module>()
----> 1 import input_data
2 mnist = tf.input_data.read_data_sets("MNIST_data/", one_hot=True)
ImportError: No module named input_data
I'm using iPython (Jupyter) so do I need to change my working directory to this folder I downloaded? or can I add this to my tensorflow directory? If so, where do I add the files? I installed tensorflow with pip (on my OSX) and the current location is ~/anaconda/lib/python2.7/site-packages/tensorflow/__init__.py
Are these files meant to be accessed directly through tensorflow like sklearn datasets? or am I just supposed to cd into the directory and work from there? The example is not clear.
EDIT:
This post is very out-dated
So let's assume that you are in the directory: /somePath/tensorflow/tutorial (and this is your working directory).
All you need to do is to download the input_data.py file and place it like this. Let's assume that the file name you invoke:
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
...
is main.py and it is also in the same directory.
Once this is done, you can just start running main.py which will start downloading the files and will put them in the MNIST_data folder (once they are there the script will not be downloading them next time).
The old tutorial said, to import the MNIST data, use:
import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
This will cause the error.
The new tutorial uses the following code to do so:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
And this works well.
I am using different version - following Install on Windows with Docker here - and had similar problem.
An easy workaround I've found was:
1.Into the Linux command line, figure out where is the input_data.py on my Docker image (in your case you mentionned that you had to download it manually. In my case, it was already here). I used the follwing linux command:
$ sudo find . -print | grep -i '.*[.]py'
I've got the files & path
./tensorflow/g3doc/tutorials/mnist/mnist.py
./tensorflow/g3doc/tutorials/mnist/input_data.py
2.launch Python and type the following command using SYS:
>> import sys
>> print(sys.path)
you will get the existing paths.
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat']
4.add the path of inputa_data.py:
>> sys.path.insert(1,'/tensorflow/tensorflow/g3doc/tutorials/mnist')
Hope that it can help. If you found better option, let me know. :)
How can I start the tutorial
I didn't download the folder you did but I installed tensorflow by pip and then I had similar problem.
My workaround was to replace
import tensorflow.examples.tutorials.mnist.input_data
with
import tensorflow.examples.tutorials.mnist.input_data as input_data
If you're using Tensorflow 2.0 or higher, you need to install tensorflow_datasets first:
pip install tensorflow_datasets
or if you're using an Anaconda distribution:
conda install tensorflow_datasets
from the command line.
If you're using a Jupyter Notebook you will need to install and enable ipywidgets. According to the docs (https://ipywidgets.readthedocs.io/en/stable/user_install.html) using pip:
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
If you're using an Anaconda distribution, install ipywidgets from the command line like such:
conda install -c conda-forge ipywidgets
With the Anaconda distribution there is no need to enable the extension, conda handles this for you.
Then import into your code:
import tensorflow_datasets as tfds
mnist = tfds.load(name='mnist')
You should be able to use it without error if you follow these instructions.
I might be kinda late, but for tensorflow version 0.12.1, you might wanna use input_data.read_data_sets instead.
Basically using this function to load the data from your local drive that you had downloaded from http://yann.lecun.com/exdb/mnist/.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('data_set/')
For TensorFlow API 2.0 the mnist data changed place to: tf.keras.datasets.mnist.load_data
There's now a much easier way to load MNIST data into tensorflow without having to download the data by using Tensorflow 2 and Tensorflow Datasets
To get started, make sure you import Tensorflow and specify the 2nd version:
%tensorflow_version 2.x
import tensorflow as tf
Then load the data into a dictionary using the following code:
MNIST_data = tfds.load(name = "mnist")
and Then split the data into train and test:
train, test = MNIST_data['train'] , MNIST_data['test']
Now you can use these data generators however you like.
Remove the lines:
from tensorflow.examples.tutorials.mnist import input_data
fashion_mnist = input_data.read_data_sets('input/data',one_hot=True)
and the line below will suffice:
fashion_mnist = keras.datasets.fashion_mnist
Note that if the dataset is not available in the examples built-in to the keras, this will download the dataset and solve the problem. :)
cd your_mnist_dir &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/mnist_data.pkl &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/t10k-images-idx3-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/t10k-labels-idx1-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/train-images-idx3-ubyte.gz &&\
wget https://github.com/HIPS/hypergrad/raw/master/data/mnist/train-labels-idx1-ubyte.gz
MNIST input_data was built-in, it's just not a individual module, it's inside Tensorflow module, try
from tensorflow.examples.tutorials.mnist import input_data
MNIST data set included as a part of tensorflow examples tutorial, If we want to use this :
Import MNIST data to identify handwritten digites
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST data", one_hot=True)
As TensorFlow official website shown, All MNIST data is hosted on http://yann.lecun.com/exdb/mnist/
For Tensorflow API above 2.0, to use MNIST dataset following command can be used,
import tensorflow_datasets as tfds
data = tfds.load(name = "mnist")
The following steps work perfectly in my Notebook:
step 1 : get Python files from github :
!git clone https://github.com/tensorflow/tensorflow.git
step 2 : append these files in my Python path :
import sys
sys.path.append('/content/tensorflow/tensorflow/examples/tutorials/mnist')
step 3 : load the MNIST data with 'input_data' fonction
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
That's all !