How to update weight in TensorFLow 2.4.1?

Hi there,
I have converted a Pytorch pre-trained model into a TensorFlow model via onnx, but I need to update the weights of some of the fields, is there a way to do this?
I am using TensorFlow 2.4.1, and tf.get_variable() does not work for me.

Thanks!

what do you mean by update the weights of some fields?, do you want to take those weights and train a new model with them?

you can get the weights of a model using model.trainable_variables, that returns a list with the tensors, pretty much like model.named_parameters() in Pytorch, you can loop through that list and get the weights you want

1 Like

Hi, thank you very much for your reply, but my model is already trained and model.trainable returns False for me.

What I am trying to do is to convert the dynamic model to tensorflow and finally convert it to Tensorflow Lite.

My Pytorch model is actually a dynamic one, which can dynamically adjust the width by update the parameter of the model with the key ‘width_mult’. For evaluation, basically what I do in the PyTorch is

model.eval()
for width_mult in sorted(FLAGS.width_mult_list, reverse=True):
      model.apply(
      lambda m: setattr(m, 'width_mult', width_mult))
      outputs = model(input)

say FLAGS.width_mult_list = [0.35, 0.5, 0.7, 1.0]. If the prediction is different then the model can achieve dynamic switching.

Then I used the onnx for the conversion and got the model in TensorFlow. But I don’t know if there is any way for me to verify the dynamics of the model, as in PyTorch.

Many Thanks:)