Singleton Pattern

It is also common practice to use design patterns in order to solve some software design problems. One of the simplest and most useful design patterns in python is the singleton one. It is used when you want to force the instantiation of a class to only one object, so even if you instantiate this class multiple times in several different places in your project, you will be referencing the same object. In our case if we ask TensorFlow to create multiple nodes or graphs with the same name, it will throw an error. Therefore, we use the singleton pattern while creating the graph to avoid building it twice. 

In the following example we summarize a simple classification model while also ensuring that the graph will not be built multiple times (aka Singleton pattern).

Pay attention to the definition of the __new__ class method. In Python, __new__ will be called when we create a new instance of a class.

class CAE_CNN_Encoder(object): 
   __instance = None 
 
   # Singleton pattern 
   def __new__(cls): 
       if CAE_CNN_Encoder.__instance is None: 
           # First time new is called 
           CAE_CNN_Encoder.__instance = object.__new__(cls) 
           CAE_CNN_Encoder.__instance.build_graph() 
       return CAE_CNN_Encoder.__instance 
 
   def build_graph(self, img_size=28): 
       self.__x = tf.placeholder(tf.float32, shape=[None, img_size * img_size], name='IMAGE_IN') 
       self.__x_image = tf.reshape(self.__x, [-1, img_size, img_size, 1]) 
       self.__y_ = tf.placeholder("float", shape=[None, 10], name='Y') 
 
       with tf.name_scope('ENCODER'): 
           ##### ENCODER 
           # CONV1: Input 28x28x1 after CONV 5x5 P:2 S:2 H_out: 1 + (28+4-5)/2 = 14, W_out= 1 + (28+4-5)/2 = 14 
           self.__conv1_act = tf.layers.conv2d(inputs=self.__x_image, strides=(2, 2), name='conv1', 
                                               filters=16, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) 
 
           # CONV2: Input 14x14x16 after CONV 5x5 P:0 S:2 H_out: 1 + (14+4-5)/2 = 7, W_out= 1 + (14+4-5)/2 = 7 
           self.__conv2_act = tf.layers.conv2d(inputs=self.__conv1_act, strides=(2, 2), name='conv2', 
                                               filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.relu) 
 
       with tf.name_scope('LATENT'): 
           # Reshape: Input 7x7x32 after [7x7x32] 
           self.__enc_out = tf.layers.flatten(self.__conv2_act, name='flatten_conv2') 
           self.__dense = tf.layers.dense(inputs=self.__enc_out, units=200, activation=tf.nn.relu, name='fc1') 
           self.__logits = tf.layers.dense(inputs=self.__dense, units=10, name='logits') 
 
   def __init__(self, img_size=28): 
       if CAE_CNN_Encoder.__instance is None: 
           self.build_graph(img_size) 
 
   @property 
   def output(self): 
       return self.__logits 
 
   @property 
   def labels(self): 
       return self.__y_ 
 
   @property 
   def input(self): 
       return self.__x 
 
   @property 
   def image_in(self): 
       return self.__x_image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.79.241