docstr: Create a TFOptimizer from a TensorFlow loss tensor.
  The loss tensor must come from a TensorFlow graph that only takes TFDataset.tensors and
  the tensors in `tensor_with_value` as inputs.
  :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 session: 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.
  :param val_outputs: the validation output TensorFlow tensor to be used by val_methods
  :param val_labels: the validation label TensorFlow tensor to be used by val_methods
  :param val_method: the BigDL val_method(s) to be used.
  :param clip_norm: float >= 0. Gradients will be clipped when their L2 norm exceeds
  this value.
  :param clip_value: float >= 0. Gradients will be clipped when their absolute value
  exceeds this value.
  :param metrics: a dictionary. The key should be a string representing the metric's name
  and the value should be the corresponding TensorFlow tensor, which should be a scalar.
  :param tensor_with_value: a dictionary. The key is TensorFlow tensor, usually a
  placeholder, the value of the dictionary is a tuple of two elements. The first one of
  the tuple is the value to feed to the tensor in training phase and the second one
  is the value to feed to the tensor in validation phase.
  :return: a TFOptimizer
docstr_tok: ["Create","a","TFOptimizer","from","a","TensorFlow","loss","tensor",".","The","loss","tensor","must","come","from","a","TensorFlow","graph","that","only","takes","TFDataset.tensors","and","the","tensors","in","`","tensor_with_value","`","as","inputs",".",":","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","session",":","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",".",":","param","val_outputs",":","the","validation","output","TensorFlow","tensor","to","be","used","by","val_methods",":","param","val_labels",":","the","validation","label","TensorFlow","tensor","to","be","used","by","val_methods",":","param","val_method",":","the","BigDL","val_method","(","s",")","to","be","used",".",":","param","clip_norm",":","float",">","=","0",".","Gradients","will","be","clipped","when","their","L2","norm","exceeds","this","value",".",":","param","clip_value",":","float",">","=","0",".","Gradients","will","be","clipped","when","their","absolute","value","exceeds","this","value",".",":","param","metrics",":","a","dictionary",".","The","key","should","be","a","string","representing","the","metric","'s","name","and","the","value","should","be","the","corresponding","TensorFlow","tensor",",","which","should","be","a","scalar",".",":","param","tensor_with_value",":","a","dictionary",".","The","key","is","TensorFlow","tensor",",","usually","a","placeholder",",","the","value","of","the","dictionary","is","a","tuple","of","two","elements",".","The","first","one","of","the","tuple","is","the","value","to","feed","to","the","tensor","in","training","phase","and","the","second","one","is","the","value","to","feed","to","the","tensor","in","validation","phase",".",":","return",":","a","TFOptimizer"]
code: def from_loss(
      cls,
      loss,
      optim_method,
      session=None,
      inputs=None,
      dataset=None,
      val_outputs=None,
      val_labels=None,
      val_method=None,
      clip_norm=None,
      clip_value=None,
      metrics=None,
      tensor_with_value=None,
      session_config=None,
      model_dir=None,
      updates=None,
  ):
      """
      Create a TFOptimizer from a TensorFlow loss tensor.
      The loss tensor must come from a TensorFlow graph that only takes TFDataset.tensors and
      the tensors in `tensor_with_value` as inputs.
      :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 session: 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.
      :param val_outputs: the validation output TensorFlow tensor to be used by val_methods
      :param val_labels: the validation label TensorFlow tensor to be used by val_methods
      :param val_method: the BigDL val_method(s) to be used.
      :param clip_norm: float >= 0. Gradients will be clipped when their L2 norm exceeds
      this value.
      :param clip_value: float >= 0. Gradients will be clipped when their absolute value
      exceeds this value.
      :param metrics: a dictionary. The key should be a string representing the metric's name
      and the value should be the corresponding TensorFlow tensor, which should be a scalar.
      :param tensor_with_value: a dictionary. The key is TensorFlow tensor, usually a
      placeholder, the value of the dictionary is a tuple of two elements. The first one of
      the tuple is the value to feed to the tensor in training phase and the second one
      is the value to feed to the tensor in validation phase.
      :return: a TFOptimizer
      """
      sess = TFOptimizer._get_or_create_session(session)
      grads, variables = TFOptimizer._get_vars_grads(loss)
  
      if dataset is None and inputs is None:
          dataset = TFOptimizer._get_dataset_from_loss(loss)
          inputs = dataset._original_tensors
      else:
          if inputs is None:
              raise ValueError("please specify inputs")
          _ = dataset.tensors  # trigger creating placeholders
  
      if isinstance(inputs, tuple) and len(inputs) == 2:
          inputs, labels = inputs
      else:
          labels = []
  
      inputs = nest.flatten(inputs)
      labels = nest.flatten(labels)
  
      if clip_value is not None:
          if isinstance(clip_value, float) or isinstance(
              clip_value, int
          ):
              if clip_value <= 0:
                  ValueError(
                      "The clip_value argument should be positive number"
                  )
              clip_value = (-float(clip_value), float(clip_value))
  
          if not isinstance(clip_value, tuple):
              raise ValueError(
                  "The clip_value argument should be"
                  + " a positive float/int which clips to"
                  + " (-clip_value, clip_value); "
                  + "or a tuple which clips to (min_value, max_value)"
              )
  
      if val_method is not None:
          val_methods = to_list(val_method)
          if metrics is None:
              metrics = {}
  
          for i, method in enumerate(val_methods):
              metrics["bigdl_metric_" + str(i)] = BigDLMetric(
                  method, val_outputs, val_labels
              )
  
      return TFOptimizer._from_grads(
          loss,
          sess,
          inputs,
          labels,
          grads,
          variables,
          dataset,
          optim_method,
          clip_norm,
          clip_value,
          metrics,
          tensor_with_value,
          session_config,
          model_dir,
          updates,
      )
  
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 9 for calling the TFOptimizer obj's underlying member method code with example usage: obj.from_loss(cls, loss, optim_method, session, inputs, dataset, val_outputs, val_labels, val_method, clip_norm, clip_value, metrics, tensor_with_value, session_config, model_dir, updates) and returns: TFOptimizer