module 'tensorflow' has no attribute 'log' - python

fashion_model.compile(
loss = keras.losses.categorical_crossentropy,
optimizer = tf.keras.optimizers.Adam(),
metrics = ['accuracy']
)
When I execute this line of code I am facing the error
module 'tensorflow' has no attribute 'log'
and my tensorflow version is 2.0

substitute tf.math.log for tf.log in tf 2.0.

If you know the exact line where tf.log is, replace it with tf.math.log.
if not, you can use this guide to Automatically upgrade code to TensorFlow 2

loss = tf.keras.losses.categorical_crossentropy
I also faced a similar issue then i called tensorflow with each Keras object and it resolved it

It is due to TensorFlow update.
Just do this:
from tensorflow import keras
Then run your code

Related

how to use deberta model from hugging face and use .compile() and . summary() with it

I used this code to load weights
from transformers import DebertaTokenizer, DebertaModel
import torch
tokenizer = DebertaTokenizer.from_pretrained('microsoft/deberta-base')
model = DebertaModel.from_pretrained('microsoft/deberta-base')
after that i want to optimize and use loss function using compile function
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=5e-5),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=tf.metrics.SparseCategoricalAccuracy(),
)
I got this error
AttributeError: 'DebertaModel' object has no attribute 'compile'
the only way to work on it using pytorch library
The transformers library offers a tensorflow-based model TFDebertaModel.
Replace DebertaModel with TFDebertaModel and the execution of compile() works.
I changed your snippet to the following and compile works. Try this:
from transformers import DebertaTokenizer, TFDebertaModel
Tested with transformers version: 4.19.2
https://huggingface.co/docs/transformers/v4.19.2/en/model_doc/deberta#transformers.TFDebertaModel

AttributeError: module 'tensorflow.python.framework.op_def_registry' has no attribute 'get_registered_ops'

I am trying to retrain the inception v3 model.
However, I got stuck by an error
AttributeError: module 'tensorflow.python.framework.op_def_registry' has no attribute 'get_registered_ops'
in the tensorflow_hub native_module.py file.
Why do I get this error?
missing_ops = graph_ops - set(op_def_registry.get_registered_ops().keys())
get_registered_ops() has been removed in recent versions of. Be sure to use tensorflow-hub>=0.7.0 to pick up on the black magic at https://github.com/tensorflow/hub/blob/v0.7.0/tensorflow_hub/native_module.py#L50
Use Tensorflow 1.11 or greater. However, don't go on to use Tensorflow 2.*.
Also, for the current latest version of tensorflow-hub 0.10.0, you need tf >= 1.15.0.
Thus, best to use 1.15.0 <= tf < 2.* along with tf-hub latest(0.10.0).

'tensorflow_core.estimator' has no attribute 'inputs', why does this happen?

I am running a neural network algorithm in a jupyter notebook.
input_func = tf.estimator.inputs.pandas_input_fn(
x=X_train,
y=y_train,
batch_size=10,
num_epochs=5,
shuffle=True)
Produces this error:
AttributeError: module 'tensorflow_core.estimator' has no attribute 'inputs'
I don't understand why this is happening. I have tried uninstalling and reinstalling tensorflow.
Does anyone know how to fix this?
in case you are running TF 2.00. You can check the version with
print(tf.__version__)
use tf.compat.v1.estimator.inputs.pandas_input_fn instead
This works:
from tensorflow_core.estimator import inputs

module 'tensorflow._api.v2.train' has no attribute 'GradientDescentOptimizer'

I used Python 3.7.3 and installed tensorflow 2.0.0-alpha0,But there are some problems。such as
module 'tensorflow._api.v2.train' has no attribute 'GradientDescentOptimizer'
Here's all my code
import tensorflow as tf
import numpy as np
x_data=np.random.rand(1,10).astype(np.float32)
y_data=x_data*0.1+0.3
Weights = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
y=Weights*x_data+biases
loss=tf.reduce_mean(tf.square(y-y_data))
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(201):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(Weights), sess.run(biases))
In TensorFlow 2.0, Keras became the default high-level API, and optimizer functions migrated from tf.keras.optimizers into separate API called tf.optimizers. They inherit from Keras class Optimizer. Relevant functions from tf.train aren't included into TF 2.0. So to access GradientDescentOptimizer, call tf.optimizers.SGD
You are using Tensorflow 2.0.
The following code will be helpful:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
This is because you are using TensorFlow version 2.
`tf.train.GradientDescentOptimizer(0.5)`
The above call is for TensorFlow version 1(ex : 1.15.0).
You can try pip install tensorflow==1.15.0 to downgrade the TensorFlow and use the code as it is.
Else use the TensorFlow version 2(what you already has) with following call.
tf.optimizers.SGD (learning_rate=0.001, lr_decay=0.0, decay_step=100, staircase=False, use_locking=False, name='SGD')
For the answer #HoyeolKim gave, it may be needed to add:
tf.disable_v2_behavior()
As it is suggested in this answer.

Keras : Create MobileNet_V2 model "AttributeError"

I have successfully built several model based on mobileNet using keras. I noticed that MobileNet_V2 as been added in Keras 2.2.0, but I could not manage to make it work :
from keras.applications.mobilenet_v2 import mobilenet_v2
base_model = mobilenet_v2.MobileNetV2(weights='imagenet', include_top=False)
I get the following error : AttributeError: 'NoneType' object has no attribute 'image_data_format' on this line from mobilenet_v2.py data_format=backend.image_data_format()
It seems to me that backendhas a definition problem... I am using Tensorflow backend, maybe it does not work with this one ?
The problem is from the import. The proper way to do this is to do the following :
from keras.applications import MobileNetV2
m = MobileNetV2(weights='imagenet', include_top=False)

Categories