AttributeError: 'KerasTPUModel' object has no attribute '_run_eagerly' - tensorflow

I am using tf.kaeras in google colab with python3.0 notebook and getting the error with the following code:
model = tf.keras.Model(inputs=[Inp], outputs=[output])
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(
tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
)
)
tpu_model.fit(
train_input_fn,
steps_per_epoch = 60,
epochs=epochs)
And the error message is
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-d6a0cf977e09> in <module>()
2 train_input_fn,
3 steps_per_epoch = 60,
----> 4 epochs=epochs)
5
6 score = tpu_model.evaluate(x_test, y_test, verbose=0)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in run_eagerly(self)
399 Boolean, whether the model should run eagerly.
400 """
--> 401 if self._run_eagerly is True and not context.executing_eagerly():
402 raise ValueError('You can only set `run_eagerly=True` if eager execution '
403 'is enabled.')
AttributeError: 'KerasTPUModel' object has no attribute '_run_eagerly'
INFO:
I get this error in google colab. Here is the Python and tensorflow version.
import sys
import tensorflow as tf
print("Python Version:", sys.version_info)
print("TensorFlow Version:", tf.__version__)
Python Version: sys.version_info(major=3, minor=6, micro=7, releaselevel='final', serial=0)
TensorFlow Version: 1.14.0-rc1

Related

I can't execute cross_val_score with scikeras.wrappers.KerasRegressor

from tensorflow import keras
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_regression
from scikeras.wrappers import KerasRegressor
X, y = make_regression(n_samples=10_000)
input_shape = X.shape[1]
model = keras.Sequential([
keras.layers.Dense(100, activation='relu', input_dim=input_shape),
keras.layers.Dense(200, activation='relu'),
keras.layers.Dense(200, activation='relu'),
keras.layers.Dense(1, activation='linear')])
model.compile(keras.optimizers.Adam(), loss='mse')
model = KerasRegressor(model, batch_size=256, verbose=1, epochs=10)
val_score = cross_val_score(model, X, y, cv=5)
plt.plot(val_score)
when I run the attached code normally it should work but for some reason it displays this error :
----------------------------------------------------------------------------------------------
Empty Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/joblib/parallel.py in dispatch_one_batch(self, iterator)
861 try:
--> 862 tasks = self._ready_batches.get(block=False)
863 except queue.Empty:
13 frames
Empty:
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/optimizer_v2.py in _getattribute_(self, name)
864 """Overridden to support hyperparameter access."""
865 try:
--> 866 return super(OptimizerV2, self)._getattribute_(name)
867 except AttributeError as e:
868 # Needed to avoid infinite recursion with _setattr_.
AttributeError: 'Adam' object has no attribute 'build'
(TensorFlow 2.11) Make sure you're doing:
from tensorflow import keras
There is a difference between import keras and from tensorflow import keras:
>>> import keras
>>> keras.optimizers.Adam.build
AttributeError: type object 'Adam' has no attribute 'build'
>>> from tensorflow import keras
>>> keras.optimizers.Adam.build
<function Adam.build at 0x7f1ff29e7b50>
(TensorFlow 2.9)
Boilerplate wrapping in a get_model function appears to resolve this:
from tensorflow import keras
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_regression
from scikeras.wrappers import KerasRegressor
X, y = make_regression(n_samples=10_000)
def get_model(meta):
X_shape_ = meta["X_shape_"]
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation='relu', input_shape=X_shape_[1:]))
model.add(keras.layers.Dense(200, activation='relu'))
model.add(keras.layers.Dense(200, activation='relu'))
model.add(keras.layers.Dense(1, activation='linear'))
return model
model = KerasRegressor(model=get_model, loss="mse", batch_size=256, verbose=1, epochs=10)
cross_val_score(model, X, y, cv=5)

Tensorflow cannot quantize reshape function

I am going to train my model quantization aware. However, when i use it , the tensorflow_model_optimization cannot quantize tf.reshape function , and throws an error.
tensorflow version : '2.4.0-dev20200903'
python version : 3.6.9
the code:
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from tensorflow.keras.applications import VGG16
import tensorflow_model_optimization as tfmot
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
quantize_model = tfmot.quantization.keras.quantize_model
inputs = keras.Input(shape=(784,))
# img_inputs = keras.Input(shape=(32, 32, 3))
dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
outputs = tf.reshape(outputs, [-1, 2, 5])
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
# keras.utils.plot_model(model, "my_first_model.png")
q_aware_model = quantize_model(model)
and the output:
Traceback (most recent call last):
File "<ipython-input-39-af601b78c010>", line 14, in <module>
q_aware_model = quantize_model(model)
File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 137, in quantize_model
annotated_model = quantize_annotate_model(to_quantize)
File "/home/essys/.local/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py", line 210, in quantize_annotate_model
to_annotate, input_tensors=None, clone_function=_add_quant_wrapper)
...
File "/home/essys/anaconda3/envs/tf_gpu/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py", line 667, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
TypeError: tf__call() got an unexpected keyword argument 'shape'
If somebody know, please help ?
The reason behind is because your layer is not yet support for QAT at the moment. If you want to quantize it, you have to self writing your quantization by quantize_annotate_layer and pass it through quantize_scope and apply to your model by quantize_apply as describe in here: https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide?hl=en#quantize_custom_keras_layer
I have create a batch_norm_layer in here as an example
Tensorflow 2.x is not complete for QAT layer, pls consider using tf1.x by adding FakeQuant after operators.

AssertionError in keras while model Fitting

I am getting the following error:
Epoch 1/15
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-8-08aa5a9ec3b3> in <module>()
13 validation_data=[X_test, X_test],
14 callbacks=[keras_utils.TqdmProgressCallback()],
---> 15 verbose=0)
5 frames
/content/tqdm_utils.py in __init__(self, iterable, total, **kwargs)
10 self.iterable = list(iterable) if iterable is not None else None
11 self.total = len(self.iterable) if self.iterable is not None else total
---> 12 assert self.iterable is not None or self.total is not None
13 self.current_step = 0
14 self.print_frequency = max(self.total // 50, 1)
AssertionError:
While running the following piece of code:
s = reset_tf_session()
encoder, decoder = build_pca_autoencoder(IMG_SHAPE, code_size=32)
inp = L.Input(IMG_SHAPE)
code = encoder(inp)
reconstruction = decoder(code)
autoencoder = keras.models.Model(inputs=inp, outputs=reconstruction)
autoencoder.compile(loss='mse',optimizer='adamax' )
autoencoder.fit(x=X_train, y=X_train, epochs=15,
validation_data=[X_test, X_test],
callbacks=[keras_utils.TqdmProgressCallback()],
verbose=0)
I'm on Tensorflow version 1.15.2 and keras version 2.3.1
The code is from a coursera assignment which I am running on Google Colab.

keras.layers.TimeDistributed with Huggingface Transformer gives NotImplementedError

I wanted to apply Bert on a sequence of sentences in the following manner, but I am getting a NotImplementedError
How to reproduce :
import tensorflow as tf
from transformers import BertTokenizer, TFBertModel
inputs = tf.keras.Input(shape=(50, 64), dtype='int32')
model = TFBertModel.from_pretrained('bert-base-uncased')
outputs = tf.keras.layers.TimeDistributed(model)(inputs)
NotImplementedError Traceback (most recent call last)
<ipython-input-5-631f3cd2e8b2> in <module>
----> 1 outputs = tf.keras.layers.TimeDistributed(model)(inputs)
Whereas the code would work fine for
inputs = tf.keras.Input(shape=(10, 128, 128, 3))
conv_2d_layer = tf.keras.layers.Conv2D(64, (3, 3))
outputs = tf.keras.layers.TimeDistributed(conv_2d_layer)(inputs)
Is there anything I am missing here?

AttributeError: 'Graph' object has no attribute 'SerializeToString'

I just wrote a simple program to test tensorflow library in my windows 10.
It is running well but only problem is that I am not being able to use tensorboard to visualize graphs.
Following is my code
import tensorflow as tf
a = tf.constant(5, name="input_a")
b = tf.constant(3, name="input_b")
c = tf.mul(a,b, name="mul_c")
d = tf.add(a,b, name="add_d")
e = tf.add(c,d, name="add_e")
sess = tf.Session()
output = sess.run(e)
writer = tf.train.SummaryWriter('./my_graph', sess.graph)
writer.close()
sess.close()
Error log
AttributeError Traceback (most recent call last)
<ipython-input-13-5c6b380c18c8> in <module>()
7 sess = tf.Session()
8 output = sess.run(e)
----> 9 writer = tf.train.SummaryWriter('./my_graph', sess.graph)
10 writer.close()
11 sess.close()
/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/summary_io.pyc in __init__(self, logdir, graph_def, max_queue, flush_secs)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/summary_io.pyc in add_graph(self, graph_def, global_step)
AttributeError: 'Graph' object has no attribute 'SerializeToString'
Tensorflow version
In[14:]:tf.__version__
Out[14]:'0.7.1'
Tensorflow library upgrade
When I tried to upgrade tensorflow library to latest 0.12 using pip in jupyter notebook it downloaded and installed lot of stuffs and after that I again tested the version but it was same.So I again tried to upgrade but it displayed following message.
Requirement already up-to date
How to get out of this mess?