code: def compute_validation_error(
      mlp, data_train, data_vali, data_test, gpu_id, params
  ):
      """Trains tensorflow neural network and then computes validation error."""
  
      # Check if num_classes exists
      if "num_classes" in params:
          num_classes = params["num_classes"]
      else:
          num_classes = None
  
      model_fn = get_model_fn(mlp, params["learningRate"], num_classes)
      model = tf.estimator.Estimator(model_fn)
      deviceStr = "/gpu:" + str(gpu_id)
      # deviceStr = '/cpu:'+str(gpu_id)
  
      # Define input layer (hook in data)
      x_train = np.array(data_train["x"])
      y_train = np.array(data_train["y"])
      if mlp.class_or_reg == "reg":
          y_train = np.reshape(y_train, (y_train.shape[0], 1))
  
      # Validation set
      x_vali = np.array(data_vali["x"])
      y_vali = np.array(data_vali["y"])
      if mlp.class_or_reg == "reg":
          y_vali = np.reshape(y_vali, (y_vali.shape[0], 1))
  
      # Test set
      x_test = np.array(data_test["x"])
      y_test = np.array(data_test["y"])
      if mlp.class_or_reg == "reg":
          y_test = np.reshape(y_test, (y_test.shape[0], 1))
  
      with tf.device(deviceStr):
          train_input_fn = tf.estimator.inputs.numpy_input_fn(
              x={"x": x_train},
              y=y_train,
              num_epochs=None,
              shuffle=True,
              batch_size=params["trainBatchSize"],
          )
          vali_input_fn = tf.estimator.inputs.numpy_input_fn(
              x={"x": x_vali},
              y=y_vali,
              num_epochs=None,
              shuffle=False,
              batch_size=params["valiBatchSize"],
          )
          eval_input_fn = tf.estimator.inputs.numpy_input_fn(
              x={"x": x_test},
              y=y_test,
              num_epochs=None,
              shuffle=False,
              batch_size=params["evalBatchSize"],
          )  #### NEED TO DEFINE evalBatchSize
          neg_vali_errors = []
          neg_eval_errors = []
          for loop in range(params["numLoops"]):
              model.train(
                  train_input_fn, steps=params["trainNumStepsPerLoop"]
              )
              if mlp.class_or_reg == "reg":
                  neg_vali_errors.append(
                      -1
                      * model.evaluate(
                          vali_input_fn,
                          steps=params["valiNumStepsPerLoop"],
                      )["mse"]
                  )
                  neg_eval_errors.append(
                      -1
                      * model.evaluate(
                          eval_input_fn,
                          steps=params["evalNumStepsPerLoop"],
                      )["mse"]
                  )  #### NEED TO DEFINE evalNumStepsPerLoop
              elif mlp.class_or_reg == "class":
                  neg_vali_errors.append(
                      model.evaluate(
                          vali_input_fn,
                          steps=params["valiNumStepsPerLoop"],
                      )["accuracy"]
                  )
                  neg_eval_errors.append(
                      model.evaluate(
                          eval_input_fn,
                          steps=params["evalNumStepsPerLoop"],
                      )["accuracy"]
                  )  #### NEED TO DEFINE evalNumStepsPerLoop
              print(
                  "Finished iter: "
                  + str((loop + 1) * params["trainNumStepsPerLoop"])
              )
  
      # return eval error at index where neg_vali_errors is maximized
      return neg_eval_errors[np.argmax(neg_vali_errors)]
  
file_path: dragonfly/examples/nas/cg/run_tensorflow_testset.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  import tensorflow
  import numpy
  import os
label_desc: Use image node_id 4 for calling a global function with example usage: compute_validation_error(mlp, data_train, data_vali, data_test, gpu_id, params) and returns: neg_eval_errors