title: Tensorflow prediction loop
body: <p>I have trained multiple tensorflow models for the same set of data, each with slightly different configuration.</p>
  
  <p>Now I want to run prediction for the given input file utilizing each tensorflow model and store the prediction in a csv.</p>
  
  <p>It seems I am unable to get tensorflow to completely unload/reset before loading new model.</p>
  
  <p>Here is my code. It works fine for the first model, then it generates error. I have tried changing sequence of models, it always run the first model without any issue, no matter which model is first. </p>
  
  <pre><code>import tensorflow as tf
  import os
  import numpy as np
  
  
  predictionoutputfile =  'data\\prediction.csv'
  predictioninputfile = 'data\\today.csv'
  modelslist = 'data\\models.csv'
  
  def predict(dirname,testfield,testper,threshold,prediction_OutFile):
      with tf.Session() as sess:
          print(dirname)
          exported_path = 'imp\\exported\\' + dirname
  
          tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], exported_path)
  
          # get the predictor , refer tf.contrib.predictor
          predictor = tf.contrib.predictor.from_saved_model(exported_path)
  
          with open(predictioninputfile) as inf:
              # Skip header
              #next(inf)
              for line in inf:
  
                  # Read data, using python, into our features
                  var1,var2,var3,var4,var5 = line.strip().split(",")
  
                  # Create a feature_dict for train.example - Get Feature Columns using
                  feature_dict = {
                      'var1': _bytes_feature(value=var1.encode()),
                      'var2': _bytes_feature(value=var2.encode()),
                      'var3': _bytes_feature(value=var3.encode()),
                      'var4':_float_feature(value=int(var4)),
                      'var5':_float_feature(value=int(var5)),
                  }
  
  
                  # Prepare model input
  
                  model_input = tf.train.Example(features=tf.train.Features(feature=feature_dict))
  
                  model_input = model_input.SerializeToString()
                  output_dict = predictor({"inputs": [model_input]})
  
  
                  # Positive label = 1
  
                  if float(output_dict['scores'][0][1])&gt;=float(threshold) :
                      prediction_OutFile.write(str(var1)+ "," + str(var2)+ "," + str(var3)+ "," + str(var4)+ "," + str(var5)+ ",")
                      label_index = tf.argmax(output_dict['scores'])
                      prediction_OutFile.write(str(output_dict['classes'][0][1]))
                      prediction_OutFile.write(',')
                      prediction_OutFile.write(str(output_dict['scores'][0][1]))
                      prediction_OutFile.write('\n')
  
  
  def main():
          prediction_OutFile = open(predictionoutputfile, 'w')
          prediction_OutFile.write("model,SYMBOL,RECORDDATE,TESTFIELD,TESTPER,prediction,probability")
          prediction_OutFile.write('\n')
          with open(modelslist) as modlist:
              #Skip header
              next(modlist)   
              for mline in modlist:
  
                      try:
                          dirname = ''
                          modelname,datafield,dataper,testfield,testper,threshold,truepositive,falsepositive,truenegative,falsenegative,correct,incorrect,accuracy,precision = mline.strip().split(",")
                          # load the current model
  
  
  
                          predict(modelname,testfield,testper,threshold,prediction_OutFile)
  
                          # Read file and create feature_dict for each record
  
                      except:
                          print('error' + modelname)
  
          prediction_OutFile.close()
  
  
  def _float_feature(value):
      return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
  
  
  def _bytes_feature(value):
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  
  
  def _int64_feature(value):
      return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  
  
  if __name__ == "__main__":
      main()
  </code></pre>