I am reading the online TensorFlow Probability (TFP) version of "Bayesian Methods for Hackers".
But when I excecute the first cell of Ch2_MorePyMC_TFP.ipynb
the following error occurs:
AttributeError: module 'tensorflow' has no attribute 'contrib'
I suppose this version of "Bayesian Methods for Hackers" jupyter notebook was written for TF1.
Do you have an easy fix or a updated version of this jupyter notebook working with TF2 ?
Some of the contrib functions are removed and some of them are merged into TensorFlow core. You need to find the equivalent version of them.
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors
print(tf.__version__) # 2.5.0
print(tfp.__version__) # 0.12.1
For example first contrib functions are available in TensorFlow and can be re-written as:
parameter = tfd.Exponential(rate=1., name="poisson_param").sample()
rv_data_generator = tfd.Poisson(parameter, name="data_generator")
data_generator = rv_data_generator.sample()
data_generator_ = tf.nest.pack_sequence_as(
data_generator,
[t.numpy() if tf.is_tensor(t) else t
for t in tf.nest.flatten(data_generator)])
print("Value of sample from data generator random variable:", data_generator_)
For other TF Operations you can replace them like this:
with tf.compat.v1.variable_scope(tf.compat.v1.get_variable_scope(), reuse=tf.compat.v1.AUTO_REUSE):
step_size = tf.compat.v1.get_variable(
name='step_size',
initializer=tf.constant(0.5, dtype=tf.float32),
trainable=False,
use_resource=True
)
More info can be found in the documentation
Frightera, I have problems getting rid of the following error :
module 'tensorflow' has no attribute 'variable_scope'
at cell :
# Initialize the step_size. (It will be automatically adapted.)
with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE):
step_size = tf.get_variable(
name='step_size',
initializer=tf.constant(0.5, dtype=tf.float),
trainable=False,
use_resource=True
)
Do you have any clue how to replace this one?
Related
I am trying to build a NER model in Keras using ELMO Embeddings. SO I stumped across this tutorial and started implementing. I got lots of errors and some of them are as:
import tensorflow as tf
import tensorflow_hub as hub
from keras import backend as K
sess = tf.Session()
K.set_session(sess)
elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())
def ElmoEmbedding(x):
return elmo_model(inputs={"tokens": tf.squeeze(tf.cast(x, tf.string)),
"sequence_len": tf.constant(batch_size*[max_len])},signature="tokens",as_dict=True)["elmo"]
input_text = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(None, 1024))(input_text)
It gives me AttributeError: module 'tensorflow' has no attribute 'Session' . So if I comment out sess= code and run, it gives me AttributeError: module 'keras.backend' has no attribute 'set_session'.
Then again, Elmo code line is giving me RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled..
I have the following configurations:
tf.__version__
'2.3.1'
keras.__version__
'2.4.3'
import sys
sys.version
'3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]'
How can I use ELMO Embeddings in Keras Model?
You are using the old Tensorflow 1.x syntax but you have tensorflow 2 installed.
This is the new way to do elmo in TF2
Extracting ELMo features using tensorflow and convert them to numpy
I am getting the following error when using the DeepExplainer for the Keras sequential model for Multiclass text classification. Please find the details below:
##KERAS VERSION: 2.4.3.
##MODEL
model = Sequential()
model.add(InputLayer (input_shape= (max_len ,)))
model.add(Embedding (vocab_size +1 , embed_dim , input_length= max_len, weights[embed_matrix]))
model.add(LSTM (LSTM_unit , dropout= dropouts, recurrent_dropout=dropouts,return_sequences=False))
model.add(Dense (6 , activation="softmax"))
##DEEPEXPLAINER
explainer = shap.DeepExplainer(model, X_train[:100], learning_phase_flags = None)
shap_values = explainer.shap_values(X_test[:10])
ERROR
LookupError: gradient registry has no entry for: shap_TensorListStack
Please help me with the solution to this issue. I have tried all the possible solutions provided in the issues mentioned in the repository but couldn't able to solve this error.
Best Regards,
Meghna Goyal
This error is due to the "shap" package that you are using.
It requires a specific set of packages to work:
TensorFlow 1.14
Python 3.7
Protobuf 3.20
h5py 2.10
import tensorflow as tf
import sys
import google.protobuf
import h5py
assert tf.__version__ == '1.14.0'
assert sys.version_info[0] == 3 and sys.version_info[1] == 7
assert google.protobuf.__version__ == '3.20.0'
assert h5py.__version__ == '2.10.0'
Ref: https://github.com/slundberg/shap/issues/1490#issuecomment-1368185975
I am trying to load the Tf hub model and predict the output using #tf.function decorator. It is throwing tensorflow.python.framework.errors_impl.FailedPreconditionError: Table not initialized. error.
TF version - 2.1.0
TF hub Version - 0.8.0
Note: It is working without using #tf.function decorator
import tensorflow as tf
import tensorflow_hub as hub
image_tensor = tf.constant(2.0, shape=[1, 298, 298, 3])
#tf.function
def run_function(method, args):
return method(args)
detector = hub.KerasLayer("https://tfhub.dev/google/openimages_v4/ssd/mobilenet_v2/1",
signature_outputs_as_dict=True)
detector_output = run_function(detector, image_tensor)
class_names = detector_output["detection_class_entities"]
print(class_names)
Can anyone know the reason why it is not working with #tf.function?
You are using a TensorFlow V1 hub model in hub.KerasLayer which is to be used for tf2.0 models
In TensorFlow hub, you can find a toggle button to view tf hub models for specific TensorFlow versions.
To make it work using hub.KeralLayer, change the URL to either of the following tf2.0 mobilenet versions
https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4
https://tfhub.dev/google/imagenet/mobilenet_v2_050_96/classification/4
or if you have to use the exact URL as in your example. Use hub.Module instead of hub.KeralLayer
I'm trying to use a module off Tensorflow Hub (a word embedding module) with tf.contrib.estimator.RNNClassifier.
My desired model
embedded_text_feature_column = hub.text_embedding_column(
key="description",
module_spec="https://tfhub.dev/google/nnlm-en-dim128/1")
estimator = tf.contrib.estimator.RNNClassifier(
sequence_feature_columns=[embedded_text_feature_column],
num_units=[32, 16])
Running that returns the following error:
ValueError: All feature_columns must be of type _SequenceDenseColumn.
You can wrap a sequence_categorical_column with an embedding_column or indicator_column.
Given (
type <class 'tensorflow_hub.feature_column._TextEmbeddingColumn'>):
_TextEmbeddingColumn(key='title_description', module_spec=<tensorflow_hub.native_module._ModuleSpec object at 0x7fb0102a5a90>, trainable=False
)
A working model
Using the TF Hub module works fine with:
estimator = tf.estimator.DNNClassifier(
hidden_units=[32, 16],
feature_columns=[embedded_text_feature_column])
Is it possible to use the nnlm module with RNNClassifier?
The Code corresponding to your Desired Model seems to be working without error in Google Colab with Tensorflow Version 1.15.
Please find the working code below:
!pip install tensorflow==1.15
import tensorflow as tf
import tensorflow_hub as hub
embedded_text_feature_column = hub.text_embedding_column(
key="description",
module_spec="https://tfhub.dev/google/nnlm-en-dim128/1")
estimator = tf.contrib.estimator.RNNClassifier(
sequence_feature_columns=[embedded_text_feature_column],
num_units=[32, 16])
Here is the Link for Github Colab Gist.
I am new to tensorflow and trying to set it up.
When I try to train a model using CuDNNGRU it seems to load correctly and then gives an error :
tensorflow.python.framework.errors_impl.NotFoundError: Op type not
registered 'CudnnRNN'
I do see a Cudnn_rnn directory in tensorflow/contrib for what that is worth.
I have python 3.6 and VS2013.
I have tried the following, but still getting an error:
Both Cuda 8/9
uninstalling/reinstalling tensorflow/Theano/Keras/TensorFlow
Honestly the setup seems so convoluted and touchy, I may have screwed something up.
Am I missing a to-do? Some way to manually fix? Thanks!
Sample code I am trying to replicate:
def get_model(embedding, sequence_length, dropout_rate, recurrent, dense_size):
input_layer = Input(shape=(sequence_length,))
embedding_layer = Embedding(embedding.shape[0], embedding.shape[1],
weights=[embedding], trainable=False)(input_layer)
x = Bidirectional(CuDNNGRU(recurrent, return_sequences=True))(embedding_layer)
x = Dropout(dropout_rate)(x)
x = Bidirectional(CuDNNGRU(recurrent, return_sequences=False))(x)
x = Dense(dense_size, activation="relu")(x)
I fixed this by doing :
pip install tensorflow --ignore-installed --upgrade
and then
from tensorflow.python.client import device_lib print(device_lib.list_local_devices())