docstr: TFOptimizer is used for distributed training of TensorFlow
  on Spark/BigDL.
  
  Note that if grads and variables are not None, then they need to be sorted by name
  if you want to use multiple optimization methods for a TensorFlow model according to
  variable names.
  
  :param loss: The loss tensor of the TensorFlow model, should be a scalar
  :param optim_method: the optimization method to be used, such as bigdl.optim.optimizer.Adam
  :param sess: the current TensorFlow Session, if you want to used a pre-trained model, you
  should use the Session to load the pre-trained variables and pass it to TFOptimizer.
docstr_tok: ["TFOptimizer","is","used","for","distributed","training","of","TensorFlow","on","Spark\/BigDL",".","Note","that","if","grads","and","variables","are","not","None",",","then","they","need","to","be","sorted","by","name","if","you","want","to","use","multiple","optimization","methods","for","a","TensorFlow","model","according","to","variable","names",".",":","param","loss",":","The","loss","tensor","of","the","TensorFlow","model",",","should","be","a","scalar",":","param","optim_method",":","the","optimization","method","to","be","used",",","such","as","bigdl.optim.optimizer.Adam",":","param","sess",":","the","current","TensorFlow","Session",",","if","you","want","to","used","a","pre-trained","model",",","you","should","use","the","Session","to","load","the","pre-trained","variables","and","pass","it","to","TFOptimizer","."]
code: def __init__(
      self,
      tf_model,
      optim_method,
      sess=None,
      dataset=None,
      clip_norm=None,
      clip_value=None,
      model_dir=None,
  ):
      """
      TFOptimizer is used for distributed training of TensorFlow
      on Spark/BigDL.
  
      Note that if grads and variables are not None, then they need to be sorted by name
      if you want to use multiple optimization methods for a TensorFlow model according to
      variable names.
  
      :param loss: The loss tensor of the TensorFlow model, should be a scalar
      :param optim_method: the optimization method to be used, such as bigdl.optim.optimizer.Adam
      :param sess: the current TensorFlow Session, if you want to used a pre-trained model, you
      should use the Session to load the pre-trained variables and pass it to TFOptimizer.
      """
  
      self.optim_method = optim_method
      self.sess = sess
      self.dataset = dataset
  
      self.clip_norm = clip_norm
      if clip_value is not None and not isinstance(clip_value, tuple):
          raise ValueError(
              "The clip_value argument should be a tuple (min_value, max_value)"
          )
      self.clip_constant = clip_value
  
      if self.dataset.batch_size <= 0:
          raise ValueError(
              "You should set batch_size instead of batch_per_thread for training"
          )
  
      self.model_dir = model_dir
  
      self.tf_model = tf_model
  
      batch_size = self.dataset.batch_size
  
      self.train_data = self.dataset.get_training_data()
      self.val_data = self.dataset.get_validation_data()
  
      self.batch_size = batch_size
  
      self.estimator = Estimator(
          self.tf_model.training_helper_layer,
          self.optim_method,
          self.model_dir,
      )
  
      if self.clip_norm:
          self.estimator.set_l2_norm_gradient_clipping(self.clip_norm)
      if self.clip_constant:
          min_value, max_value = self.clip_constant
          self.estimator.set_constant_gradient_clipping(
              min_value, max_value
          )
  
imports: import json
  import logging
  import os
  import sys
  import tempfile
  from bigdl.nn.criterion import Criterion
  from bigdl.nn.layer import Layer
  from bigdl.optim.optimizer import MaxEpoch, EveryEpoch
  from bigdl.util.common import to_list, JavaValue
  from zoo.common.utils import callZooFunc
  from zoo.feature.common import FeatureSet
  from zoo.pipeline.api.keras.engine.topology import to_bigdl_metric, Loss, OptimMethod
  from zoo.pipeline.api.net.utils import find_placeholders, to_bigdl_optim_method, find_tensors
  from zoo.pipeline.estimator import Estimator
  from zoo.util import nest
  from zoo.util.triggers import EveryEpoch
  from zoo.util.triggers import ZooTrigger
  from zoo.tfpark.tf_dataset import TFNdarrayDataset, check_data_compatible
  from zoo.tfpark.tf_dataset import _standarize_feature_label_dataset
label_desc: Use image node_id 1 to create a new TFOptimizer object  with example: obj = TFOptimizer(tf_model, optim_method, sess, dataset, clip_norm, clip_value, model_dir)