manages dependencies on other objects with keras tracking similar to tf autotrackable but disabling tracking is based on tracking within keras this serves as an interface between keras tracking and tf tracking support self foo trackable syntax try if getattrself name is value short circuit for self x self x return except attributeerror pass if getattrself selfsetattrtracking true value stickyattributeassignment trackableself valuevalue namename super setattrname value def stickyattributeassignmenttrackable name value if isinstance value tracking trackedlist tracking trackeddict tracking trackedset and hasattrtrackable tracked trackable tracked appendname if not tracking istrackingenabled return value if isinstancevalue tf internal tracking trackable trackable tracktrackable pylint disableprotectedaccess value namename allow the user to switch the trackable which is tracked by this name since assigning a new variable to an attribute has historically been fine e g adam did this overwritetrue return value manages dependencies on other objects with keras tracking similar to tf autotrackable but disabling tracking is based on tracking within keras this serves as an interface between keras tracking and tf tracking support self foo trackable syntax short circuit for self x self x adds dependencies called from __setattr__ args trackable the object to add dependencies to generally the one having an attribute assigned name the attribute name being assigned value the value being assigned not necessarily a trackable object returns the value which should be stored in the attribute pylint disable protected access allow the user to switch the trackable which is tracked by this name since assigning a new variable to an attribute has historically been fine e g adam did this
import tensorflow as tf

from keras.utils import tracking


class KerasAutoTrackable(tf.__internal__.tracking.AutoTrackable):
    

    def __setattr__(self, name, value):
        
        try:
            if getattr(self, name) is value:
                
                return
        except AttributeError:
            pass

        if getattr(self, "_self_setattr_tracking", True):
            value = sticky_attribute_assignment(
                trackable=self, value=value, name=name
            )
        super().__setattr__(name, value)


def sticky_attribute_assignment(trackable, name, value):
    
    if isinstance(
        value, (tracking.TrackedList, tracking.TrackedDict, tracking.TrackedSet)
    ) and hasattr(trackable, "_tracked"):
        trackable._tracked.append(name)
    if not tracking.is_tracking_enabled():
        return value
    if isinstance(value, tf.__internal__.tracking.Trackable):
        trackable._track_trackable(  
            value,
            name=name,
            
            
            
            overwrite=True,
        )
    return value