'tensorflow' has no attribute 'to_int32' - python

I am trying to implement CTC loss to audio files but I get the following error:
TensorFlow has no attribute 'to_int32'
I'm running tf.version 2.0.0.
I think it's with the version, I'm currently using, as we see the error is thrown in the package itself ' tensorflow_backend.py' code.
I have imported packages as "tensorflow.keras.class_name" with backend as K. Below is the screenshot.

You can cast the tensor in TensorFlow 2 as follows:
tf.cast(my_tensor, tf.int32)
You can read the documentation of the method in https://www.tensorflow.org/api_docs/python/tf/cast
You can also see that the to_int32 is deprecated and was used in TensorFlow 1
https://www.tensorflow.org/api_docs/python/tf/compat/v1/to_int32

After you make the import just write
tf.to_int=lambda x: tf.cast(x, tf.int32)
This is similar to writing the behavior of tf.to_int in everywhere in the code, so you don't have to manually edit a TF1.0 code

Related

module 'keras.layers' has no attribute 'experimental'

hello so I was trying to resize and rescale my dataset as shown below l but I encountered this error:
AttributeError: module 'keras.layers' has no attribute 'experimental'
resize_and_rescale= tf.keras.Sequential([
layers.experimental.preprocessing.Resizing(IMAGE_SIZE,IMAGE_SIZE),
layers.experimental.preprocessing.Rescaling(1.0/255)
])
actually I tried adding "tf.keras" in front of my layers line and it worked :
resize_and_rescale= tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.Resizing(IMAGE_SIZE,IMAGE_SIZE),
tf.keras.layers.experimental.preprocessing.Rescaling(1.0/255)])
thank you !
Yes, if it worked by adding tf.keras.layers, even though you have already imported Keras from TensorFlow earlier, then it is most likely an issue with the importation chain of the libraries rather than the actual block of code. Cross-check how you imported the libraries earlier to reduce redundant importing as you might have done now. Also, this will make your code cleaner.

module 'jax' has no attribute 'tree_multimap' in AlphaFold2 CoLab

I am attempting to model a protein using an AlphaFold2 (AlphaFold v2.1.0.) CoLab (https://colab.research.google.com/github/deepmind/alphafold/blob/main/notebooks/AlphaFold.ipynb#scrollTo=pc5-mbsX9PZC).
I have done this successfully on 9/2/2022. However I have repeatedly had issues since 9/7/2022 doing the modelling with a different peptide sequence.
I get the following warning when I run the search against the genetic databases:
/opt/conda/lib/python3.7/site-packages/haiku/_src/data_structures.py:37: FutureWarning: jax.tree_structure is deprecated, and will be removed in a future release. Use jax.tree_util.tree_structure instead.
PyTreeDef = type(jax.tree_structure(None))
I then get several other future warnings when I run AlphaFold2 about other jax.tree_ deprecations.
The problem with AlphaFold running seems to be related to this:
AttributeError: module 'jax' has no attribute 'tree_multimap'
I have tried substituting jax.tree_util.tree_structure with no success.
I see another question on stackoverflow that is similar (AttributeError: module 'jaxlib.xla_extension' has no attribute 'PmapFunction'), however I do not know how best to implement the solution in the CoLab environment.
How should I fix this issue so that AlphaFold2 will run properly?
Traceback shown below:
44 processed_feature_dict = model_runner.process_features(np_example, random_seed=0)
---> 45 prediction = model_runner.predict(processed_feature_dict, random_seed=random.randrange(sys.maxsize))
/opt/conda/lib/python3.7/site-packages/haiku/_src/stateful.py in difference(before, after)
310 params_before, params_after = box_and_fill_missing(before.params,
311 after.params)
--> 312 params_after = jax.tree_multimap(functools.partial(if_changed, is_new_param),
313 params_before, params_after)
jax.tree_multimap was deprecated in JAX version 0.3.5, and removed in JAX version 0.3.16.
You can either change the source to use jax.tree_map as a drop-in replacement for jax.tree_multimap, or install an older version of JAX, e.g.:
!pip install "jax<=0.3.16" "jaxlib<=0.3.16"
And then be sure to restart your runtime to pick up the new versiom.

Tensorflow v2 alternative of sequence_loss_by_example

I am exploring the following tensorflow example: https://github.com/googledatalab/notebooks/blob/master/samples/TensorFlow/LSTM%20Punctuation%20Model%20With%20TensorFlow.ipynb which apparently is written in tf v1, so I upgraded with the v2 upgrade script and there were three main inconsistencies:
ERROR: Using member tf.contrib.rnn.DropoutWrapper in deprecated module tf.contrib. tf.contrib.rnn.DropoutWrapper cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
ERROR: Using member tf.contrib.legacy_seq2seq.sequence_loss_by_example in deprecated module tf.contrib. tf.contrib.legacy_seq2seq.sequence_loss_by_example cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
ERROR: Using member tf.contrib.framework.get_or_create_global_step in deprecated module tf.contrib. tf.contrib.framework.get_or_create_global_step cannot be converted automatically. tf.contrib will not be distributed with TensorFlow 2.0, please consider an alternative in non-contrib TensorFlow, a community-maintained repository such as tensorflow/addons, or fork the required code.
So for compatibility I manually replaced framework.get_or_create_global_step with tf.compat.v1.train.get_or_create_global_step, and also rnn.DropoutWrapper with tf.compat.v1.nn.rnn_cell.DropoutWrapper.
But I was unable to find a solution on how to handle the tf.contrib.legacy_seq2seq.sequence_loss_by_example method, since I cannot find a backwards compatible alternative. I tried installing Tensroflow Addons and use its seq2seq loss function, but wasn't able to figure out how to adapt it to work with the rest of the code.
Stumbled across some errors like Consider casting elements to a supported type. or Logits must be a [batch_size x sequence_length x logits] tensor, because probably i am not implementing something correctly.
So my question: Are you aware if legacy_seq2seq.sequence_loss_by_example is supported through some third party addon/library which I can use, and more importantly, could someone show me how to implement supported tensorflow v2 alternative of this loss function, so it acts similarly to the code below.
output = tf.reshape(tf.concat(axis=1, values=outputs), [-1, size])
softmax_w = tf.compat.v1.get_variable("softmax_w", [size, len(TARGETS)], dtype=tf.float32)
softmax_b = tf.compat.v1.get_variable("softmax_b", [len(TARGETS)], dtype=tf.float32)
logits = tf.matmul(output, softmax_w) + softmax_b
self._predictions = tf.argmax(input=logits, axis=1)
self._targets = tf.reshape(input_.targets, [-1])
loss = tfa.seq2seq.sequence_loss(
[logits],
[tf.reshape(input_.targets, [-1])],
[tf.ones([batch_size * num_steps], dtype=tf.float32)])
self._cost = cost = tf.reduce_sum(input_tensor=loss) / batch_size
self._final_state = state
Full code here.
First install tensorflow addons using:
pip install tensorflow-addons
Then import it in your program:
import tensorflow_addons as tfa
And use:
tfa.seq2seq.sequence_loss

Getting an Error for BERT module when trying to access bert.variables

I'm trying to get BERT to do sentiment analysis from the code obtained from here: https://github.com/strongio/keras-bert
But when I try to build the model, I get an error saying,
'Module' object has no attribute 'variables'
This occurs specifically in the build function of the BertLayer class when I try to access self.bert.variables.
I tried a dir(self.bert) to get all the attributes of the object and it indeed did not have an attribute called variables. These are the attributes I obtained:
['\__call__', '\__class__', '\__delattr__', '\__dict__', '\__dir__', '\__doc__', '\__eq__', '\__format__', '\__ge__', '\__getattribute__', '\__gt__', '\__hash__', '\__init__', '\__init_subclass__', '\__le__', '\__lt__', '\__module__', '\__ne__', '\__new__', '\__reduce__', '\__reduce_ex__', '\__repr__', '\__setattr__', '\__sizeof__', '\__str__', '\__subclasshook__', '\__weakref__', '_graph', '_impl', '_name', '_spec', '_tags', '_trainable', 'export', 'get_input_info_dict', 'get_output_info_dict', 'get_signature_names', 'variable_map']
I'm using tf version: 1.13.0 with Python: 3.5
Installing the very latest versions of both tensorflow and tensorflow hub fixed this issue.

Code using tensorflow.mul() fails after upgrading to tensorflow v1.0

This code runs on Tensorflow v0.12.1, but fails on my new installation on TF v1.0. Is it that this function is deprecated? What's the function I should use? (Tensorflow up and running so I believe it's not a misconfiguration)
File "***.py", line 115, in trainNetwork
readout_action = tf.reduce_sum(tf.mul(readout, a), reduction_indices = 1)
AttributeError: module 'tensorflow' has no attribute 'mul'
The tf.mul() function has been renamed to tf.multiply() in TensorFlow 1.0.
For your kind information , tf.mul, tf.sub and tf.neg were used in the prior version of tensor-flow. They have been deprecated.
You can instead use tf.multiply, tf.subtract and tf.negative.
Thanks.

Categories

Resources