Importing Tensorflow's Dataset class - python

I am running Ubuntu 16.04, and have installed tensorflow through pip, using pip3 install tensorflow. I now want to import and use the Dataset class, which is part of the contrib.data package. In various tutorials, this has simply been done using:
import tensorflow as tf
from tf.contrib.data import Dataset
But this prints out:
ImportError: No module named 'tf'
I have also tried using this without the renaming of tensorflow:
import tensorflow
from tensorflow.contrib.data import Dataset
But this prints out:
ImportError: cannot import name 'Dataset'
If I navigate to /usr/local/lib/python3.5/dist-packages/tensorflow/contrib/data, then there are two directories (__pycache__ and python), and two files (_dataset_ops.so, __init__.py). But I'm not sure where the Dataset class should be defined here, if at all...
So what should I do differently to enable me to import Dataset?

data was moved out of contrib several Tensorflow versions ago. As far as I know a recent update completely removed most "standard" ops from tf.contrib.data. It now only contains "experimental"/volatile code. Simply use tf.data.Dataset instead.

Related

How to change the way a module imports one of its module?

I would like to know if it is possible to parameterized the way a module import one of its own module.
My problem is the following. I have a number of generic tensorflow functions, such as losses, that work with both version (1 and 2) of the API.
If the module is used with TF2, or with an old version of TF1, tensorflow needs to be imported like
import tensorflow as tf
However if I use TF 1.15, or if I want to use the version 1 of the API with TF2, tensorflow needs to be imported as
import tensorflow.compat.v1 as tf
tf.disable_v1_behavior()
So the way the import is done cannot be automatically deduced from the TF version, as TF2 can be used in TF1 "compatibility" mode.
Is there a way I can change the way the import is done in the module?
A hack that seems to work for modules that are directly imported:
import my_module
my_module.tf = tf
That forces the tf module to be the same as the current one. However,
This could have invisible and hard-to-track side effects since tensorflow is imported with potentially different APIs requirement, this could mess up any global variable settings.
This works for modules imported directly, not for modules that are imported by other modules, unless the hack is propagated to all modules.
I use, as in https://stackoverflow.com/a/32965521/2069610:
>>> import pkg_resources
>>> pkg_resources.get_distribution("tensorflow").version
'XX.XX'
Another idea would be to grep:
$ pip freeze | grep tensorflow
tensorflow==XX.XX
are you using pip or conda? This might also offer couple of different options, based on the output of conda list or so..
Based on that, you could use one or the other import...

ModuleNotFoundError: No module named 'tensorflow.contrib' with tensorflow=2.0.0

I am using TensorFlow version=2.0.0
python version=3.7.3
I am trying to import the below statement
from tensorflow.contrib import rnn
And it gives error as
Module 'tensorflow' has no attribute 'contrib'
How can I resolve this?
from tensor flow
https://www.tensorflow.org/guide/upgrade#compatibility_modules
Because of TensorFlow 2.x module deprecations (for example, tf.flags and tf.contrib), some changes can not be worked around by switching to compat.v1. Upgrading this code may require using an additional library (for example, absl.flags) or switching to a package in tensorflow/addons.
and as describe in this thread
tensorflow.contrib doesn't exist in 2.0.
https://github.com/tensorflow/tensorflow/issues/31350#issuecomment-518749548
I haven't used older versions of tensorflow. Is this what you're looking for?
from tensorflow.keras.layers import RNN
Info on contrib:
https://www.tensorflow.org/guide/migrate#a_note_on_slim_contriblayers

No module named 'tensorflow.python._impl

I want to run code for try neural network with python but
No module named 'tensorflow.python._impl
part of my code:
import tensorflow as tf
from tensorflow import keras
from tensorflow.python._impl import activations
I did all solutions that some sites said such as
- Uninstall and install package like tensorflow,..
also:
- Upgrade tensorflow
- Upgrade keras
but it doesn't solve yet. I have this error on co lab too.
please help me if you have any solution
thanks
from keras.layers.recurrent import ...
Official TensorFlow Support discourages use of tf.python.* as it is private and intended for development purposes only. While it may work in some cases, it will "break unannounced" in many others.
Instead, try importing tensorflow with the .python portion removed, e.g.:
from tensorflow._impl import activations

Module import works differently for two ways of importing tensorflow

I'm currently trying to teach myself tensorflow. The new version has keras built in.
I can access the Dense function in the following way
import tensorflow as tf
tf.keras.layers.Dense
but this does not work:
from tensorflow.keras.layers import Dense
Why is that? I notice that:
from tensorflow.python.keras.layers import Dense
Does work. When I import tensorflow, does it know to intelligently add the .python to the module name?
In the GitHub Repo for Tensorflow, if you look at the two __init__.py files inside tensorflow-master/tensorflow/python/keras/ and tensorflow-master/tensorflow/python/keras/layers/, you can see which modules are imported as part of the package structure. This determines what and how you as a user import things when using the package and its modules.
David Beazley has a really good talk on the innerworkings of this:
https://www.youtube.com/watch?v=0oTh1CXRaQ0

How can jupyter access a new tensorflow module installed in the right path?

Where should I stick the model folder? I'm confused because python imports modules from somewhere in anaconda (e.g. import numpy), but I can also import data (e.g. file.csv) from the folder in which my jupyter notebook is saved in.
The TF-Slim image models library is not part of the core TF library. So I checked out the tensorflow/models repository as:
cd $HOME/workspace
git clone https://github.com/tensorflow/models/
I'm not sure what $HOME/workspace is. I'm running a ipython/jupyter notebook from users/me/workspace/ so I saved it to:
users/me/workspace/models
In jupyter, I'll write:
import tensorflow as tf
from datasets import dataset_utils
# Main slim library
slim = tf.contrib.slim
But I get an error:
ImportError: No module named datasets
Any tips? I understand that my tensorflow code is stored in '/Users/me/anaconda/lib/python2.7/site-packages/tensorflow/init.pyc' so maybe I should save the new models folder (which contains models/datasets) there?
From the error "ImportError: No module named datasets"
It seems that no package named datasets is present.
You need to install datasets package and then run your script.
Once you install it, then you can find the package present in location "/Users/me/anaconda/lib/python2.7/site-packages/" or at the location "/Users/me/anaconda/lib/python2.7/"
Download the package from https://pypi.python.org/pypi/dataset and install it.This should work
You can find the folder address on your device and append it to system path.
import sys
sys.path.append(r"D:\Python35\models\slim\datasets")
import dataset_utils
You'll need to do the same with 'nets' and 'preprocessing'
sys.path.append(r"D:\Python35\models\slim\nets")
import vgg
sys.path.append(r"D:\Python35\models\slim\preprocessing")
import vgg_preprocessing

Categories