AttributeError: 'Adam' object has no attribute 'get_updates' - python

I'm training a VAE with TensorFlow Keras backend and I'm using Adam as the optimizer. the code I used is attached below.
def compile(self, learning_rate=0.0001):
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
self.model.compile(optimizer=optimizer,
loss=self._calculate_combined_loss,
metrics=[_calculate_reconstruction_loss,
calculate_kl_loss(self)])
The TensorFlow version I'm using is 2.11.0. The error I'm getting is
AttributeError: 'Adam' object has no attribute 'get_updates'
I'm suspecting the issues arise because of the version mismatch. Can someone please help me to sort out the issue? Thanks in advance.

Related

Object detection evaluation model main error

I'm currently working ona object detection model efficientnet and i tried to evaluate my mode with model main but got the error
ValueError: Tensor("Detections_Left_Groundtruth_Right/0:0", shape=(), dtype=string) must be from the same graph as Tensor("Loss/TargetAssignment/AvgNumGroundtruthBoxesPerImage:0", shape=(), dtype=string) (graphs are FuncGraph(name=cond_true_48273, id=139640549532432) and <tensorflow.python.framework.ops.Graph object at 0x7f0111ca0410>).
Can someone help me?
You can fix it by passing the checkpoint file path correctly, e.g.:
--checkpoint_dir=/checkpoint/ckpt-x, where x is the checkpoint number. Also check for this bug https://github.com/tensorflow/models/pull/5450. It worked for me.

How to update mirrored variable in tensorflow 2.0?

I am building a model in tensorflow version 2.0 (upgrading is not an option due to compatibility with my version of cuda, which I do not have permission to change). I am using tf.strategy.MirroredStrategy() to train my model on 2 GPUs. However, I am trying to instantiate a custom dense layer whose weights are the transpose of the weights of a different dense layer. My code involves this line to build the custom layer:
from tensorflow.keras import backend as K
class DenseTied(Layer):
# Really long class, full code can be found at link below
def build(self, input_shape):
self.kernel = K.transpose(self.tied_to.kernel)
I am then using this in a model as follows:
from tensorflow.keras.layers import Input, Dense
def build_model(input_shape):
model_input = Input(shape=input_shape)
dense1 = Dense(6144, activation='relu')
dense_tied1 = DenseTied(49152, tied_to=dense1)
x = dense1(model_input)
model_output = dense_tied1(x)
model = Model(model_input, model_output)
model.compile(optimizer='adam', loss='mse')
return model
When trying to build this model I get an error: AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute '_distribute_strategy'.
I have been tracking this down for a while now and I pinpointed that the issue is in the line
self.kernel = K.transpose(self.tied_to.kernel)
It seems that self.tied_to.kernel is of type <class 'tensorflow.python.distribute.values.MirroredVariable'> but after calling K.transpose() on it the resulting output is of type <class 'tensorflow.python.framework.ops.EagerTensor'>. I tried following the instructions here but it did not work. I get AttributeError: 'MirroredStrategy' object has no attribute 'run' when in the docs it does. So I think maybe my version of Tensorflow is too old for that method.
How can I update a mirrored variable in Tensorflow 2.0?
Also if you want to see the full custom layer code, I am trying to implement the dense tied layer described here.
As of now, the documentation is for tensorflow 2.3. If you are using 2.0 it should be
strategy.experimental_run_v2 instead of strategy.run.

AttributeError: 'Tensor' object has no attribute 'numpy' in custom loss function (Tensorflow 2.1.0)

I want to train a model with a custom loss function, in order to do that, I need to convert the tensor to numpy array inside the method below:
def median_loss_estimation(y_true, y_predicted):
a = y_predicted.numpy()
but I have this error:
AttributeError: 'Tensor' object has no attribute 'numpy'
Why?
How can I convert the tensor to a numpy array?
The answer is: put run_eagerly=True in model.compile!
You're doing the right thing, only Tensorflow 2.1 is currently broken in that aspect. This would normally happen if you run the code without eager mode enabled. However, Tensorflow 2 by default runs in eager mode... or at least it should. The issue is tracked here.
There are at least two solutions to this:
Install the latest nightly build.
Set model.run_eagerly = True.

Save a model with keras

I use the method save_model and load_mode but it don't work.
I have an error : AttributeError: 'GridSearchCV' object has no attribute 'get_config'
I don't know if I use correctly this method. I show my code for take an example:
gridSearch = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = "accuracy",
cv = 10)
gridSearch.fit(X_train, y_train)
save_model(gridSearch, filepath = 'monModele.h5')
The result is the error attribute Error. Can you help me to find a solution for this problem or to find an other method to save and load a keras model.
That is because GridSearchCV is not a Keras model, but a module from sklearn that also has a fit function with a similar API.
In order to use save_model and load_model you need the actual Keras model, my guess is it is your classifier. Specifically, an instance of the Model class from Keras.

NameError: name 'Subtract' is not defined

I'm working on neural network, which I will be using in Dueling DQN algorithm, but I encountered a problem with keras layer Subtract, when I use this layer I am getting this error:
AttributeError: module 'keras.layers' has no attribute 'Subtract'
Method, where I use Subtract:
def DDDQN(self):
inp=Input(shape=(self.state_size,))
x=Dense(units=32,activation='relu',kernel_initializer='he_uniform')(inp)
x=Dense(units=16,activation='relu',kernel_initializer='he_uniform')(x)
value_=Dense(units=1,activation='linear',kernel_initializer='he_uniform')(x)
ac_activation=Dense(units=self.action_size,activation='linear',kernel_initializer='he_uniform')(x)
avg_ac_activation=Lambda(lambda x: K_back.mean(x,axis=1,keepdims=True))(ac_activation)
concat_value=Concatenate(axis=-1)([value_,value_])
concat_avg_ac=Concatenate(axis=-1)([avg_ac_activation,avg_ac_activation])
for i in range(1,self.action_size-1):
concat_value=Concatenate(axis=-1)([concat_value,value_])
concat_avg_ac=Concatenate(axis=-1)([concat_avg_ac,avg_ac_activation])
ac_activation=Subtract()([ac_activation,concat_avg_ac])
merged_layers=Add()([concat_value,ac_activation])
final_model=Model(inputs=inp,outputs=merged_layers)
final_model.compile(loss='mean_squared_error',optimizer=Adam(lr=self.learning_rate))
return final_model
Other layers like Dense, Lambda or Multiplicate are working correctly, any suggestions how to solve this problem?
Basics first, do you have the appropriate Python Interpreter version installed ?
Try updating to Python3.6 for exemple (if this is relevant to you)

Categories

Resources