import tensorflow as tf

class Graph():
    """ Loading and running isolated tf graph """
    def __init__(self, loc):
        self.graph = tf.Graph()
        self.sess = tf.Session(graph=self.graph)
        with self.graph.as_default():
            saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
            saver.restore(self.sess, loc)
            self.activation = tf.get_collection('activation')[0]
    # To launch the graph
    def run(self, data):
        return self.sess.run(self.activation, feed_dict={"x:0": data})
import tensorflow as tf

class Graph():
    """ Loading and running isolated tf graph """
    def __init__(self, loc, operation='activation', input_name='x'):
        """
        loc: location of file containing saved model
        operation: name of operation for running the model
        input_name: name of input placeholder
        """
        self.input = input_name + ":0"
        self.graph = tf.Graph()
        self.sess = tf.Session(graph=self.graph)
        with self.graph.as_default():
            saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
            saver.restore(self.sess, loc)
            self.op = self.graph.get_operation_by_name(operation).outputs[0]

    def run(self, data):
        """ Run the specified operation on given data """
        return self.sess.run(self.op, feed_dict={self.input: data})
Update Graph class for loading saved models Requires renaming operations in models -> re-train them
Update Graph class for loading saved models
Requires renaming operations in models -> re-train them

  import tensorflow as tf
  
  class Graph():
      """ Loading and running isolated tf graph """
-     def __init__(self, loc):
+     def __init__(self, loc, operation='activation', input_name='x'):
+         """
+         loc: location of file containing saved model
+         operation: name of operation for running the model
+         input_name: name of input placeholder
+         """
+         self.input = input_name + ":0"
          self.graph = tf.Graph()
          self.sess = tf.Session(graph=self.graph)
          with self.graph.as_default():
              saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
              saver.restore(self.sess, loc)
-             self.activation = tf.get_collection('activation')[0]
-     # To launch the graph
+             self.op = self.graph.get_operation_by_name(operation).outputs[0]
+ 
      def run(self, data):
+         """ Run the specified operation on given data """
-         return self.sess.run(self.activation, feed_dict={"x:0": data})
+         return self.sess.run(self.op, feed_dict={self.input: data})
Update Graph class for loading saved models Requires renaming operations in models -> re-train them
## Code Before:
import tensorflow as tf

class Graph():
    """ Loading and running isolated tf graph """
    def __init__(self, loc):
        self.graph = tf.Graph()
        self.sess = tf.Session(graph=self.graph)
        with self.graph.as_default():
            saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
            saver.restore(self.sess, loc)
            self.activation = tf.get_collection('activation')[0]
    # To launch the graph
    def run(self, data):
        return self.sess.run(self.activation, feed_dict={"x:0": data})
## Instruction:
Update Graph class for loading saved models Requires renaming operations in models -> re-train them
## Code After:
import tensorflow as tf

class Graph():
    """ Loading and running isolated tf graph """
    def __init__(self, loc, operation='activation', input_name='x'):
        """
        loc: location of file containing saved model
        operation: name of operation for running the model
        input_name: name of input placeholder
        """
        self.input = input_name + ":0"
        self.graph = tf.Graph()
        self.sess = tf.Session(graph=self.graph)
        with self.graph.as_default():
            saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
            saver.restore(self.sess, loc)
            self.op = self.graph.get_operation_by_name(operation).outputs[0]

    def run(self, data):
        """ Run the specified operation on given data """
        return self.sess.run(self.op, feed_dict={self.input: data})