Alternative for TensorFlow tf.contrib - python

I keep getting an error when i run this code:
from sklearn import preprocessing
lencoder = preprocessing.LabelEncoder()
voc_processor = tf.contrib.learn.preprocessing.VocabularyProcessor(kw_list)
voc_processor.fit(vocabulary)
X_transform = voc_processor.transform(reviews_df.reviewText)
X_transform = np.array(list(X_transform))
This is the error I get:
AttributeError: module 'tensorflow' has no attribute 'contrib'
Is there another approach to run this without having use an old version of tensorflow. I understand I am getting this error because tf.contrib. has been deprecated.

You can take a look at how new examples are dealing with text preprocessing, as an example
import tensorflow as tf
import tensorflow_transform as tft
[...]
review_tokens = tf.compat.v1.string_split(review, DELIMITERS)
review_indices = tft.compute_and_apply_vocabulary(
review_tokens, top_k=VOCAB_SIZE)
from
https://github.com/tensorflow/transform/blob/master/examples/sentiment_example.py

Related

'NearMiss' object has no attribute '_validate_data'

Detailed Image
This is the code below which shows the error.
from imblearn.under_sampling import NearMiss
nm = NearMiss()
X_res,y_res=nm.fit_sample(X,Y)
You are probably trying to under sample your imbalanced dataset. For this purpose, you can use RandomUnderSampler instead of NearMiss.
Try the following code:
from imblearn.under_sampling import RandomUnderSampler
under_sampler = RandomUnderSampler()
X_res, y_res = under_sampler.fit_resample(X, y)
Now, your dataset is balanced. You can verify it using y_res.value_counts().
Cheers!
Instead of "imblearn" package my conda installed a package named "imbalanced-learn" that's why it does not take the data. But it is strange that the jupyter notebook doesn't tell me that "imblearn" isn't installed.

Can't convert a tf.data.Dataset object to a numpy iterator

I am using Tensorflow 1.14.0 and tensorflow_datasets 1.2.0
When trying to run the following code
import tensorflow as tf
import tensorflow_datasets as tfds
smallnorb = tfds.load("smallnorb")
smallnorb_train, smallnorb_test = smallnorb["train"], smallnorb["test"]
assert isinstance(smallnorb_train, tf.data.Dataset)
smallnorb_train = smallnorb_train.as_numpy_iterator()
I get the following error
AttributeError: 'DatasetV1Adapter' object has no attribute 'as_numpy_iterator'
According to the tensorflow_datasets docs this should work.
Why won't it? And why am I getting a DatasetV1Adapter object in the first place?
You are using wrong tensorflow and tensorflow_datasets versions.
Please use 2.x unless you need 1.x for some very specific reasons.
This code works if you use tensorflow 2.1.0 and tensorflow_datasets 2.0.0. Proper documentation for 1.x of tf.data.Dataset can be found here and it has no such method indeed.
As #szymon mentioned, tensorflow-1.14 does not support the as_numpy_iterator. You should move your code to tf>=2.0
A handy tip which I frequently use is firing up a REPL python shell in one of the bash shells and use dir(tf.data.Dataset) to list all the attributes & methods that can be called from that object. You can further use the help(tf.data.Dataset.xxx) for parameters and return values of that method.
>>> import tensorflow as tf
>>> dir(tf.data.Dataset)
... <output>
>>> help(tf.data.Dataset.from_tensor_slices)
... and so on
If you do the same, you'll find that as_numpy_iterator won't be present in the dir(tf.data.Dataset) list output, hence the error.

TensorFlow: AttributeError when calling TensorFlow's IO functions; why are they missing?

I use TensorFlow 1.12.0 with Python 3.5. When I call tf.io.decode_jpg() (or any function from tf.io), I get the following error: AttributeError: module 'tensorflow._api.v1.io' has no attribute 'decode_***'.
I import TensorFlow as following:
import tensorflow as tf
I also tried importing the io module directly, but to no avail. I call the function as follows:
rgb = tf.io.decode_image(tf.read_file(DIR_DATASET + sample_name + ".jpg"))

Tensorflow Module Import error: AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell'

When attempting to pass my RNN call, I call tf.nn.rnn_cell and I receive the following error:
AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell'
Which is odd, because I'm sure I imported everything correctly:
from __future__ import print_function, division
from tensorflow.contrib import rnn
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
But looking at the docs, things have moved around between tensorflow versions.
what would you all recommend to fix this??
Line, I'm getting the error against:
state_per_layer_list = tf.unstack(init_state, axis=0)
rnn_tuple_state = tuple(
[tf.nn.rnn_cell.LSTMStateTuple(state_per_layer_list[idx][0], state_per_layer_list[idx][1])
for idx in range(num_layers)]
)
Specifically:
tf.nn.rnn_cell
I'm using anaconda 3 to manage all of this so, the dependancies should all be taken care of. I have already tried working around a damn rank/shape error with Tensor shapes which took ages to resolve.
Cheers in advance.
Replace tf.nn.rnn_cell with tf.contrib.rnn
Since version 1.0, rnn implemented as part of the contrib module.
More information can be found here
https://www.tensorflow.org/api_guides/python/contrib.rnn

How to solve error in python program named: AttributeError: 'module' object has no attribute 'TensorFlowLinearClassifier'

This is the code which uses tensorflow library.
import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
print iris
classifier = skflow.TensorFlowLinearClassifier(n_classes=3)
classifier.fit(iris.data, iris.target)
score=metrics.accuracy_score(iris.target,classifier.predict(iris.data))
print ("Accracy: %f" % score)
I have created a python virtual environment and installed tensorflow in it. I tried to use conda as well this results in similar error
They have changed the name to LinearClassifier, therefore this will work
classifier = skflow.LinearClassifier(n_classes=3)
try using from tensorflow.contrib.learn import TensorFlowLinearClassifier

Categories