model-optimization icon indicating copy to clipboard operation
model-optimization copied to clipboard

Can't de serialize PolynomialDecay pruning schedule (and potentially others

Open alessandroaimar opened this issue 6 years ago • 3 comments

import tensorflow as tf
from tensorflow_model_optimization.python.core.sparsity.keras.pruning_schedule import PolynomialDecay
poly = PolynomialDecay(0, 0.5, 0, 10)
serialized = tf.keras.utils.serialize_keras_object(poly)
deserialized = tf.keras.utils.deserialize_keras_object(serialized, custom_objects = {"PolynomialDecay":  PolynomialDecay})

Traceback (most recent call last):
  File "C:\Users\Alessandro\.conda\envs\keras\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    self.sys_excepthook = old_excepthook
  File "<ipython-input-12-572c6a0027da>", line 2, in <module>
    PolynomialDecay})
  File "C:\Users\Alessandro\.conda\envs\keras\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 306, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "C:\Users\Alessandro\AppData\Roaming\Python\Python37\site-packages\tensorflow_model_optimization\python\core\sparsity\keras\pruning_schedule.py", line 134, in from_config
    return cls(**config)
TypeError: __init__() got an unexpected keyword argument 'class_name'

In file tensorflow_model_optimization\python\core\sparsity\keras\pruning_schedule.py line 251 the code is

def get_config(self):
  return {
      'class_name': self.__class__.__name__,
      'config': {
          'initial_sparsity': self.initial_sparsity,
          'final_sparsity': self.final_sparsity,
          'power': self.power,
          'begin_step': self.begin_step,
          'end_step': self.end_step,
          'frequency': self.frequency
      }
  }

for the deserialization to work properly it should be

def get_config(self):
   return  {
           'initial_sparsity': self.initial_sparsity,
           'final_sparsity': self.final_sparsity,
           'power': self.power,
           'begin_step': self.begin_step,
           'end_step': self.end_step,
           'frequency': self.frequency
       }

alessandroaimar avatar Jul 12 '20 11:07 alessandroaimar

@nutsiepully Can you take a look at this issue?

gargn avatar Jul 17 '20 03:07 gargn

Hi @liyunlu0618 , can you check?

teijeong avatar Apr 18 '21 07:04 teijeong

This is intended. Our goal is for the model-level serialization/deserialization to work properly. With this fix, you're going to hit this error: https://github.com/keras-team/keras/blob/master/keras/utils/generic_utils.py#L551

liyunlu0618 avatar Apr 20 '21 22:04 liyunlu0618