Keras + TensorFlow: “module 'tensorflow' has no attribute 'merge_all_summaries''” - python

Very similar to Keras + tensorflow gives the error "no attribute 'control_flow_ops'", from the Convolutional autoencoder example from https://blog.keras.io/building-autoencoders-in-keras.html I get the error
[...]lib/python3.5/site-packages/keras/callbacks.py in _set_model(self, model)
478 tf.histogram_summary('{}_out'.format(layer),
479 layer.output)
--> 480 self.merged = tf.merge_all_summaries()
481 if self.write_graph:
482 if parse_version(tf.__version__) >= parse_version('0.8.0'):
AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'
I tried
import tensorflow as tf
tf.merge_all_summaries = tf
but that did not work. What should I do?
In AttributeError: 'module' object has no attribute 'merge_all_summaries' the error is mentioned. I also have the version 1.0.0. But what is the solution? I don't want to downgrade TensorFlow.

Make42 is absolutely correct that the changes they describe in their answer must be made in order to migrate a codebase to work with TensorFlow 1.0. However, the errors you are seeing are in the Keras library itself. Fortunately, these errors have been fixed in the Keras codebase since January 2017, so upgrading to Keras 1.2.2 or later will fix the error for you.

The answer is to migrate as appropriate. Check out https://www.tensorflow.org/install/migration. There you see that
- tf.merge_summary
- should be renamed to tf.summary.merge
- tf.train.SummaryWriter
- should be renamed to tf.summary.FileWriter
(Actually SummaryWriter has also been changed.) So instead of
import tensorflow as tf
tf.merge_all_summaries = tf
you should write
import tensorflow as tf
tf.merge_all_summaries = tf.summary.merge_all
tf.train.SummaryWriter = tf.summary.FileWriter

Related

When using the Deepbrain libary error message "module 'tensorflow' has no attribute 'Session"

I am trying to use the library Deepbrain to extract brains from the entire MRIs scan I am using the code
def Reduce_Brain(img):
img_data = img.get_fdata()
prob = ext.run(img)
print(prob)
img = nib.load('ADNI_002_S_0295.nii')
Reduce_Brain(img)
however, when I tried this I got the error module 'tensorflow' has no attribute 'Session' which I found was an error to do with the wrong version of tensorflow so I then changed the library code as said in the other question (see below). but this produced more errors such as module 'tensorflow' has no attribute 'gfile'
Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'
https://github.com/iitzco/deepbrain
In TF 2.x you should use tf.compat.v1.Session() instead of tf.Session().
Take a look at Migrate_tf2 guide for more information
To get TF 1.x like behaviour in TF 2.0 add below code
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

'tensorflow' has no attribute 'to_int32'

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

'tensorflow' has no attribute 'Session'

I am trying to convert a Tensor to a numpy array.
The tensor i have has a shape as below
LastDenseLayer.output.shape
TensorShape([None, 128])
When i am running the code as below,
with tf.Session() as sess:
LastLayer = LastDenseLayer.output.eval()
getting the below error
module 'tensorflow' has no attribute 'Session'
I am running Keras model and trying to get the values of a specific layer out of that.
Unable to understand that is wrong here.
Regards
Sachin
TensorFlow 2.x removed tf.Session because eager execution is now a default. Please refer to the TensorFlow migration guide for more information.
It's recommended to update your code to meet Tensorflow 2.0 requirements. As a quick solution you can use tf.compat.v1.Session() instead of tf.Session():
with tf.compat.v1.Session() as sess:
LastLayer = LastDenseLayer.output.eval()

AttributeError when using callback Tensorboard on Keras: 'Model' object has no attribute 'run_eagerly'

I have built a model using the functional API from Keras, and when I am adding the tensorboard instance to my callbacks in my model.fit() function, it throws an Error: "AttributeError: 'Model' object has no attribute 'run_eagerly'"
The Model class does indeed not have an attribute run_eagerly, but in the Keras doc, it says that it can be passed as parameter to the model.compile() function. This returns
"ValueError: ('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['run_eagerly']))"
Does this mean I don't have a suitable version of Tensorflow/Keras?
Tensorflow: 1.14.0
Keras: 2.2.4-tf
model = Model(inputs=[input_ant1, input_ant2], outputs=main_output)
tensorboard = TensorBoard(log_dir='.logs/'.format(time()))
[...]
model.fit([input1, input2],[labels], epochs=10, callbacks=[tensorboard])
I got the same error : AttributeError: 'Model' object has no attribute 'run_eagerly'
After two minor changes my tensorboard is running now.
make sure you import tensorboard as follows:
from keras.callbacks import TensorBoard
change the log dir like this:
tensorboard = TensorBoard(log_dir="logs")
I got same error message when I use:
from tensorflow.keras.callbacks import ModelCheckpoint
I fix it with by importing the same as:
from keras.callbacks.callbacks import ModelCheckpoint

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