jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def __init__(self,
               model,
               n_tasks,
               n_feat,
               logdir=None,
               batch_size=50,
               final_loss='weighted_L2',
               learning_rate=.001,
               optimizer_type="adam",
               learning_rate_decay_time=1000,
               beta1=.9,
               beta2=.999,
               pad_batches=True,
               verbose=True):

    warnings.warn(
        "MultitaskGraphRegressor is deprecated. "
        "Will be removed in DeepChem 1.4.", DeprecationWarning)

    super(MultitaskGraphRegressor, self).__init__(
        model_dir=logdir, verbose=verbose)
    self.n_tasks = n_tasks
    self.final_loss = final_loss
    self.model = model
    self.sess = tf.Session(graph=self.model.graph)

    with self.model.graph.as_default():
      # Extract model info
      self.batch_size = batch_size
      self.pad_batches = pad_batches
      # Get graph topology for x
      self.graph_topology = self.model.get_graph_topology()
      self.feat_dim = n_feat

      # Building outputs
      self.outputs = self.build()
      self.loss_op = self.add_training_loss(self.final_loss, self.outputs)

      self.learning_rate = learning_rate
      self.T = learning_rate_decay_time
      self.optimizer_type = optimizer_type

      self.optimizer_beta1 = beta1
      self.optimizer_beta2 = beta2

      # Set epsilon
      self.epsilon = 1e-7
      self.add_optimizer()

      # Initialize
      self.init_fn = tf.initialize_all_variables()
      self.sess.run(self.init_fn)

      # Path to save checkpoint files, which matches the
      # replicated supervisor's default path.
      self._save_path = os.path.join(self.model_dir, 'model.ckpt')
//  [python]
def __init__(self,
               model,
               n_tasks,
               n_feat,
               logdir=None,
               batch_size=50,
               final_loss='weighted_L2',
               learning_rate=.001,
               optimizer_type="adam",
               learning_rate_decay_time=1000,
               beta1=.9,
               beta2=.999,
               pad_batches=True,
               verbose=True):

    warnings.warn(
        "MultitaskGraphRegressor is deprecated. "
        "Will be removed in DeepChem 1.4.", DeprecationWarning)

    super(MultitaskGraphRegressor, self).__init__(
        model_dir=logdir, verbose=verbose)
    self.n_tasks = n_tasks
    self.final_loss = final_loss
    self.model = model
    self.sess = tf.Session(graph=self.model.graph)

    with self.model.graph.as_default():
      # Extract model info
      self.batch_size = batch_size
      self.pad_batches = pad_batches
      # Get graph topology for x
      self.graph_topology = self.model.get_graph_topology()
      self.feat_dim = n_feat

      # Building outputs
      self.outputs = self.build()
      self.loss_op = self.add_training_loss(self.final_loss, self.outputs)

      self.learning_rate = learning_rate
      self.T = learning_rate_decay_time
      self.optimizer_type = optimizer_type

      self.optimizer_beta1 = beta1
      self.optimizer_beta2 = beta2

      # Set epsilon
      self.epsilon = 1e-7
      self.add_optimizer()

      # Initialize
      self.init_fn = tf.global_variables_initializer()
      self.sess.run(self.init_fn)

      # Path to save checkpoint files, which matches the
      # replicated supervisor's default path.
      self._save_path = os.path.join(self.model_dir, 'model.ckpt')
//  [python]
def _initialize_vars(self):
        self.sess.run(tf.global_variables_initializer())
        return
//  [python]
def build_graph(self):
        with tf.Graph().as_default() as graph:
            self.ema = tf.train.ExponentialMovingAverage(decay=0.99)
            self.global_step = tf.Variable(0, trainable=False)
            self.learning_rate_var = tf.Variable(0.0, trainable=False)
            self.beta1_decay_var = tf.Variable(0.0, trainable=False)

            self.loss = self.calculate_loss()
            self.update_parameters(self.loss)

            self.saver = tf.train.Saver(max_to_keep=1)
            if self.enable_parameter_averaging:
                self.saver_averaged = tf.train.Saver(self.ema.variables_to_restore(), max_to_keep=1)

            self.init = tf.initialize_all_variables()
            return graph
//  [python]
def build_graph(self):
        with tf.Graph().as_default() as graph:
            self.ema = tf.train.ExponentialMovingAverage(decay=0.99)
            self.global_step = tf.Variable(0, trainable=False)
            self.learning_rate_var = tf.Variable(0.0, trainable=False)
            self.beta1_decay_var = tf.Variable(0.0, trainable=False)

            self.loss = self.calculate_loss()
            self.update_parameters(self.loss)

            self.saver = tf.train.Saver(max_to_keep=1)
            if self.enable_parameter_averaging:
                self.saver_averaged = tf.train.Saver(self.ema.variables_to_restore(), max_to_keep=1)

            self.init = tf.global_variables_initializer()
            return graph
//  [python]
def make_unfused_gru_tests(options):
  """Make a set of tests for unfused gru op."""

  test_parameters = [{
      "units": [2, 5],
      "batch_size": [1, 2],
      "time": [3],
  }]

  def build_graph(parameters):
    inputs = [
        tf.placeholder(tf.float32,
                       [parameters["batch_size"], parameters["units"]])
        for _ in range(parameters["time"])
    ]
    cell_fw = tf.nn.rnn_cell.GRUCell(parameters["units"])
    cell_bw = tf.nn.rnn_cell.GRUCell(parameters["units"])
    outputs, _, _ = tf.nn.static_bidirectional_rnn(
        cell_fw, cell_bw, inputs, dtype=tf.float32)

    return inputs, outputs

  def build_inputs(parameters, sess, inputs, outputs):
    input_values = [
        create_tensor_data(tf.float32,
                           [parameters["batch_size"], parameters["units"]])
        for _ in range(parameters["time"])
    ]
    init = tf.initialize_all_variables()
    sess.run(init)
    return input_values, sess.run(
        outputs, feed_dict=dict(zip(inputs, input_values)))

  make_zip_of_tests(
      options,
      test_parameters,
      build_graph,
      build_inputs,
      use_frozen_graph=True)
//  [python]
def make_unfused_gru_tests(options):
  """Make a set of tests for unfused gru op."""

  test_parameters = [{
      "units": [2, 5],
      "batch_size": [1, 2],
      "time": [3],
  }]

  def build_graph(parameters):
    inputs = [
        tf.placeholder(tf.float32,
                       [parameters["batch_size"], parameters["units"]])
        for _ in range(parameters["time"])
    ]
    cell_fw = tf.nn.rnn_cell.GRUCell(parameters["units"])
    cell_bw = tf.nn.rnn_cell.GRUCell(parameters["units"])
    outputs, _, _ = tf.nn.static_bidirectional_rnn(
        cell_fw, cell_bw, inputs, dtype=tf.float32)

    return inputs, outputs

  def build_inputs(parameters, sess, inputs, outputs):
    input_values = [
        create_tensor_data(tf.float32,
                           [parameters["batch_size"], parameters["units"]])
        for _ in range(parameters["time"])
    ]
    init = tf.global_variables_initializer()
    sess.run(init)
    return input_values, sess.run(
        outputs, feed_dict=dict(zip(inputs, input_values)))

  make_zip_of_tests(
      options,
      test_parameters,
      build_graph,
      build_inputs,
      use_frozen_graph=True)
//  [python]
def initialize_session(self):
        """Defines self.sess and initialize the variables"""
        self.logger.info("Initializing tf session")
        self.sess = tf.Session()
        self.sess.run(tf.initialize_all_variables())
        self.saver = tf.train.Saver()
//  [python]
def initialize_session(self):
        """Defines self.sess and initialize the variables"""
        self.logger.info("Initializing tf session")
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver()
//  [python]
def create_or_load_model(model, model_dir, session, name, specific_ckpt=None):
    """Create model and initialize or load parameters in session."""

    latest_ckpt = tf.train.latest_checkpoint(model_dir)
    if specific_ckpt:
        specific_ckpt_path = os.path.join(model_dir, specific_ckpt)
        model = load_model(model, specific_ckpt_path, session, name)
        fresh = False
    elif latest_ckpt:
        model = load_model(model, latest_ckpt, session, name)
        fresh = False
    else:
        start_time = time.time()
        session.run(tf.initialize_all_variables())
        session.run(tf.tables_initializer())
        colorlog.info("  created %s model with new parameters, time %.2fs" %
                        (name, time.time() - start_time))
        fresh = True

    return model, fresh
//  [python]
def create_or_load_model(model, model_dir, session, name, specific_ckpt=None):
    """Create model and initialize or load parameters in session."""

    latest_ckpt = tf.train.latest_checkpoint(model_dir)
    if specific_ckpt:
        specific_ckpt_path = os.path.join(model_dir, specific_ckpt)
        model = load_model(model, specific_ckpt_path, session, name)
        fresh = False
    elif latest_ckpt:
        model = load_model(model, latest_ckpt, session, name)
        fresh = False
    else:
        start_time = time.time()
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        colorlog.info("  created %s model with new parameters, time %.2fs" %
                        (name, time.time() - start_time))
        fresh = True

    return model, fresh
//  [python]
def create_or_load_model(model, model_dir, session, out_dir, name):
    """Create translation model and initialize or load parameters in session."""
    start_time = time.time()
    latest_ckpt = tf.train.latest_checkpoint(model_dir)
    if latest_ckpt:
        model.saver.restore(session, latest_ckpt)
        utils.print_out(
                "  loaded %s model parameters from %s, time %.2fs" %
                (name, latest_ckpt, time.time() - start_time))
    else:
        utils.print_out("  created %s model with fresh parameters, time %.2fs." %
                                        (name, time.time() - start_time))
        session.run(tf.initialize_all_variables())

    global_step = model.global_step.eval(session=session)
    return model, global_step
//  [python]
def create_or_load_model(model, model_dir, session, out_dir, name):
    """Create translation model and initialize or load parameters in session."""
    start_time = time.time()
    latest_ckpt = tf.train.latest_checkpoint(model_dir)
    if latest_ckpt:
        model.saver.restore(session, latest_ckpt)
        utils.print_out(
                "  loaded %s model parameters from %s, time %.2fs" %
                (name, latest_ckpt, time.time() - start_time))
    else:
        utils.print_out("  created %s model with fresh parameters, time %.2fs." %
                                        (name, time.time() - start_time))
        session.run(tf.global_variables_initializer())

    global_step = model.global_step.eval(session=session)
    return model, global_step
//  [python]
def train(self):
    init_op = tf.group(tf.initialize_all_variables(), tf.local_variables_initializer())
    summary_op = tf.summary.merge_all()
    saver = tf.train.Saver()
    # Create a session for running operations in the Graph.
    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # Initialize the variables (like the epoch counter).
    sess.run(init_op)
    #saver.restore(sess, './models/model.ckpt-30000')
    summary_writer = tf.summary.FileWriter(self.train_dir, sess.graph)
    # Start input enqueue threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    iters = 0
    try:
      while not coord.should_stop():
        # Run training steps or whatever
        t1 = time.time()
        _, loss = sess.run([self.train_op, self.net.loss], feed_dict={self.net.train: True})
        t2 = time.time()
        print('step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)' % ((iters, loss, self.batch_size/(t2-t1), (t2-t1))))
        iters += 1
        if iters % 10 == 0:
          summary_str = sess.run(summary_op, feed_dict={self.net.train: True})
          summary_writer.add_summary(summary_str, iters)
        if iters % 1000 == 0:
          #self.sample(sess, mu=1.0, step=iters)
          self.sample(sess, mu=1.1, step=iters)
          #self.sample(sess, mu=100, step=iters)
        if iters % 10000 == 0:
          checkpoint_path = os.path.join(self.train_dir, 'model.ckpt')
          saver.save(sess, checkpoint_path, global_step=iters)
    except tf.errors.OutOfRangeError:
      checkpoint_path = os.path.join(self.train_dir, 'model.ckpt')
      saver.save(sess, checkpoint_path)
      print('Done training -- epoch limit reached')
    finally:
      # When done, ask the threads to stop.
      coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)
    sess.close()
//  [python]
def train(self):
    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    summary_op = tf.summary.merge_all()
    saver = tf.train.Saver()
    # Create a session for running operations in the Graph.
    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # Initialize the variables (like the epoch counter).
    sess.run(init_op)
    #saver.restore(sess, './models/model.ckpt-30000')
    summary_writer = tf.summary.FileWriter(self.train_dir, sess.graph)
    # Start input enqueue threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    iters = 0
    try:
      while not coord.should_stop():
        # Run training steps or whatever
        t1 = time.time()
        _, loss = sess.run([self.train_op, self.net.loss], feed_dict={self.net.train: True})
        t2 = time.time()
        print('step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)' % ((iters, loss, self.batch_size/(t2-t1), (t2-t1))))
        iters += 1
        if iters % 10 == 0:
          summary_str = sess.run(summary_op, feed_dict={self.net.train: True})
          summary_writer.add_summary(summary_str, iters)
        if iters % 1000 == 0:
          #self.sample(sess, mu=1.0, step=iters)
          self.sample(sess, mu=1.1, step=iters)
          #self.sample(sess, mu=100, step=iters)
        if iters % 10000 == 0:
          checkpoint_path = os.path.join(self.train_dir, 'model.ckpt')
          saver.save(sess, checkpoint_path, global_step=iters)
    except tf.errors.OutOfRangeError:
      checkpoint_path = os.path.join(self.train_dir, 'model.ckpt')
      saver.save(sess, checkpoint_path)
      print('Done training -- epoch limit reached')
    finally:
      # When done, ask the threads to stop.
      coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)
    sess.close()
//  [python]
def initialized_session(self):
    """Wrapper for test session context manager with required initialization.

    Yields:
      A session object that should be used as a context manager.
    """
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      sess.run(tf.local_variables_initializer())
      yield sess
//  [python]
def validation():
    print('validation')
    test_feeder = DataIterator(data_dir='../data/test/')

    final_predict_val = []
    final_predict_index = []
    groundtruth = []

    with tf.Session() as sess:
        test_images, test_labels = test_feeder.input_pipeline(batch_size=FLAGS.batch_size, num_epochs=1)
        graph = build_graph(3)
        try:
            sess.run(tf.initialize_all_variables())
            sess.run(tf.initialize_local_variables())  # initialize test_feeder's inside state
        except AttributeError:
            sess.run(tf.initialize_all_variables())
            sess.run(tf.initialize_local_variables())  # initialize test_feeder's inside state

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
        if ckpt:
            saver.restore(sess, ckpt)
            print("restore from the checkpoint {0}".format(ckpt))

        logger.info(':::Start validation:::')
        try:
            i = 0
            acc_top_1, acc_top_k = 0.0, 0.0
            while not coord.should_stop():
                i += 1
                start_time = time.time()
                test_images_batch, test_labels_batch = sess.run([test_images, test_labels])
                feed_dict = {graph['images']: test_images_batch,
                             graph['labels']: test_labels_batch,
                             graph['keep_prob']: 1.0}
                batch_labels, probs, indices, acc_1, acc_k = sess.run([graph['labels'],
                                                                       graph['predicted_val_top_k'],
                                                                       graph['predicted_index_top_k'],
                                                                       graph['accuracy'],
                                                                       graph['accuracy_top_k']], feed_dict=feed_dict)
                final_predict_val += probs.tolist()
                final_predict_index += indices.tolist()
                groundtruth += batch_labels.tolist()
                acc_top_1 += acc_1
                acc_top_k += acc_k
                end_time = time.time()
                logger.info("the batch {0} takes {1} seconds, accuracy = {2}(top_1) {3}(top_k)"
                            .format(i, end_time - start_time, acc_1, acc_k))

        except tf.errors.OutOfRangeError:
            logger.info('==================Validation Finished================')
            acc_top_1 = acc_top_1 * FLAGS.batch_size / test_feeder.size
            acc_top_k = acc_top_k * FLAGS.batch_size / test_feeder.size
            logger.info('top 1 accuracy {0} top k accuracy {1}'.format(acc_top_1, acc_top_k))
        finally:
            coord.request_stop()
        coord.join(threads)
    return {'prob': final_predict_val, 'indices': final_predict_index, 'groundtruth': groundtruth}
//  [python]
def validation():
    print('validation')
    test_feeder = DataIterator(data_dir='../data/test/')

    final_predict_val = []
    final_predict_index = []
    groundtruth = []

    with tf.Session() as sess:
        test_images, test_labels = test_feeder.input_pipeline(batch_size=FLAGS.batch_size, num_epochs=1)
        graph = build_graph(3)
        try:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())  # initialize test_feeder's inside state
        except AttributeError:
            sess.run(tf.initialize_all_variables())
            sess.run(tf.initialize_local_variables())  # initialize test_feeder's inside state

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
        if ckpt:
            saver.restore(sess, ckpt)
            print("restore from the checkpoint {0}".format(ckpt))

        logger.info(':::Start validation:::')
        try:
            i = 0
            acc_top_1, acc_top_k = 0.0, 0.0
            while not coord.should_stop():
                i += 1
                start_time = time.time()
                test_images_batch, test_labels_batch = sess.run([test_images, test_labels])
                feed_dict = {graph['images']: test_images_batch,
                             graph['labels']: test_labels_batch,
                             graph['keep_prob']: 1.0}
                batch_labels, probs, indices, acc_1, acc_k = sess.run([graph['labels'],
                                                                       graph['predicted_val_top_k'],
                                                                       graph['predicted_index_top_k'],
                                                                       graph['accuracy'],
                                                                       graph['accuracy_top_k']], feed_dict=feed_dict)
                final_predict_val += probs.tolist()
                final_predict_index += indices.tolist()
                groundtruth += batch_labels.tolist()
                acc_top_1 += acc_1
                acc_top_k += acc_k
                end_time = time.time()
                logger.info("the batch {0} takes {1} seconds, accuracy = {2}(top_1) {3}(top_k)"
                            .format(i, end_time - start_time, acc_1, acc_k))

        except tf.errors.OutOfRangeError:
            logger.info('==================Validation Finished================')
            acc_top_1 = acc_top_1 * FLAGS.batch_size / test_feeder.size
            acc_top_k = acc_top_k * FLAGS.batch_size / test_feeder.size
            logger.info('top 1 accuracy {0} top k accuracy {1}'.format(acc_top_1, acc_top_k))
        finally:
            coord.request_stop()
        coord.join(threads)
    return {'prob': final_predict_val, 'indices': final_predict_index, 'groundtruth': groundtruth}
//  [python]
def execute_cpu_tf1(self, compute_fn, inputs):
    """Executes compute_fn on CPU with Tensorflow 1.X.

    Args:
      compute_fn: a function containing Tensorflow computation that takes a list
        of input numpy tensors, performs computation and returns output numpy
        tensors.
      inputs: a list of numpy arrays to feed input to the `compute_fn`.

    Returns:
      A list of numpy arrays or a single numpy array.
    """
    if self.is_tf2():
      raise ValueError('Required version Tenforflow 1.X is not available.')
    with self.test_session(graph=tf.Graph()) as sess:
      placeholders = [tf.placeholder_with_default(v, v.shape) for v in inputs]
      results = compute_fn(*placeholders)
      if (not (isinstance(results, dict) or isinstance(results, tf.Tensor)) and
          hasattr(results, '__iter__')):
        results = list(results)
      sess.run([tf.global_variables_initializer(), tf.tables_initializer(),
                tf.local_variables_initializer()])
      materialized_results = sess.run(results, feed_dict=dict(zip(placeholders,
                                                                  inputs)))
    return self.maybe_extract_single_output(materialized_results)
//  [python]
def execute_tpu_tf1(self, compute_fn, inputs):
    """Executes compute_fn on TPU with Tensorflow 1.X.

    Args:
      compute_fn: a function containing Tensorflow computation that takes a list
        of input numpy tensors, performs computation and returns output numpy
        tensors.
      inputs: a list of numpy arrays to feed input to the `compute_fn`.

    Returns:
      A list of numpy arrays or a single numpy array.
    """
    with self.test_session(graph=tf.Graph()) as sess:
      placeholders = [tf.placeholder_with_default(v, v.shape) for v in inputs]
      def wrap_graph_fn(*args, **kwargs):
        results = compute_fn(*args, **kwargs)
        if (not (isinstance(results, dict) or isinstance(results, tf.Tensor))
            and hasattr(results, '__iter__')):
          results = list(results)
        return results
      tpu_computation = contrib_tpu.rewrite(wrap_graph_fn, placeholders)
      sess.run(contrib_tpu.initialize_system())
      sess.run([tf.global_variables_initializer(), tf.tables_initializer(),
                tf.local_variables_initializer()])
      materialized_results = sess.run(tpu_computation,
                                      feed_dict=dict(zip(placeholders, inputs)))
      sess.run(contrib_tpu.shutdown_system())
    return self.maybe_extract_single_output(materialized_results)
//  [python]
def _main(_):
    env = gym.make('CartPole-v0')
    env = env.unwrapped

    env_config = tx.agents.get_gym_env_config(env)
    agent = PGAgent(
        env_config,
        policy_kwargs={'action_space': env_config.action_space},
        hparams=config.pg_agent_hparams)

    sess = tf.Session()
    agent.sess = sess
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.tables_initializer())
    feed_dict = {tx.global_mode(): tf.estimator.ModeKeys.TRAIN}

    for e in range(300):
        reward_sum = 0.
        observ = env.reset()
        agent.reset()
        while True:
            action = agent.get_action(observ, feed_dict=feed_dict)

            next_observ, reward, terminal, _ = env.step(action=action)
            if terminal:
                reward = 0.
            agent.observe(reward, terminal, feed_dict=feed_dict)
            observ = next_observ

            reward_sum += reward
            if terminal:
                break

        if (e + 1) % 10 == 0:
            print('episode {}: {}'.format(e + 1, reward_sum))

    sess.close()
//  [python]
def train(self):
        batch = self.iterator.get_next()
        loss, acc, _ = self.forward(batch)
        op_step = tf.Variable(0, name='op_step')
        train_op = tx.core.get_train_op(loss, global_step=op_step, hparams=self.config.opt_hparams)
        max_val_acc = 0.
        self.saver = tf.train.Saver()
        with tf.Session(config=self.gpu_config) as sess:
            sess.run(tf.initialize_all_variables())
            sess.run(tf.local_variables_initializer())
            sess.run(tf.tables_initializer())
            for epoch_id in range(self.config._max_epoch):
                self.iterator.switch_to_train_data(sess)
                cur_step = 0
                cnt_acc = []
                while True:
                    try:
                        cur_step += 1
                        feed = {tx.global_mode(): tf.estimator.ModeKeys.TRAIN}
                        loss, acc_ = sess.run([train_op, acc], feed_dict=feed)
                        cnt_acc.append(acc_)
                        if cur_step % 200 == 0:
                            print('batch {}, loss={}, acc1={}'.format(cur_step, loss, np.mean(cnt_acc[-200:])))
                    except tf.errors.OutOfRangeError:
                        break
                op_step = op_step + 1
                self.iterator.switch_to_val_data(sess)
                cnt_acc = []
                while True:
                    try:
                        feed = {tx.global_mode(): tf.estimator.ModeKeys.EVAL}
                        acc_ = sess.run([acc], feed_dict=feed)
                        cnt_acc.append(acc_)
                    except tf.errors.OutOfRangeError:
                        mean_acc = np.mean(cnt_acc)
                        print('valid acc1={}'.format(mean_acc))
                        if mean_acc > max_val_acc:
                            max_val_acc = mean_acc
                            self.saver.save(sess, self.config._save_path)
                        break
//  [python]
def train(self):
        batch = self.iterator.get_next()
        loss, acc, _ = self.forward(batch)
        op_step = tf.Variable(0, name='op_step')
        train_op = tx.core.get_train_op(loss, global_step=op_step, hparams=self.config.opt_hparams)
        max_val_acc = 0.
        self.saver = tf.train.Saver()
        with tf.Session(config=self.gpu_config) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            sess.run(tf.tables_initializer())
            for epoch_id in range(self.config._max_epoch):
                self.iterator.switch_to_train_data(sess)
                cur_step = 0
                cnt_acc = []
                while True:
                    try:
                        cur_step += 1
                        feed = {tx.global_mode(): tf.estimator.ModeKeys.TRAIN}
                        loss, acc_ = sess.run([train_op, acc], feed_dict=feed)
                        cnt_acc.append(acc_)
                        if cur_step % 200 == 0:
                            print('batch {}, loss={}, acc1={}'.format(cur_step, loss, np.mean(cnt_acc[-200:])))
                    except tf.errors.OutOfRangeError:
                        break
                op_step = op_step + 1
                self.iterator.switch_to_val_data(sess)
                cnt_acc = []
                while True:
                    try:
                        feed = {tx.global_mode(): tf.estimator.ModeKeys.EVAL}
                        acc_ = sess.run([acc], feed_dict=feed)
                        cnt_acc.append(acc_)
                    except tf.errors.OutOfRangeError:
                        mean_acc = np.mean(cnt_acc)
                        print('valid acc1={}'.format(mean_acc))
                        if mean_acc > max_val_acc:
                            max_val_acc = mean_acc
                            self.saver.save(sess, self.config._save_path)
                        break
//  [python]
def restore(self, session, step='max'):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
    vars_to_restore = [v for v in tf.global_variables() if "module/" not in v.name]
    saver = tf.train.Saver(vars_to_restore)
    if step == 'max':
      path = "model.max.ckpt"
    else:
      path = "model-" + step
    checkpoint_path = os.path.join(self.config["log_dir"], path)
    print(f"Restoring from {checkpoint_path}")
    session.run(tf.initialize_all_variables())
    saver.restore(session, checkpoint_path)
//  [python]
def restore(self, session, step='max'):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
    vars_to_restore = [v for v in tf.global_variables() if "module/" not in v.name]
    saver = tf.train.Saver(vars_to_restore)
    if step == 'max':
      path = "model.max.ckpt"
    else:
      path = "model-" + step
    checkpoint_path = os.path.join(self.config["log_dir"], path)
    print(f"Restoring from {checkpoint_path}")
    session.run(tf.global_variables_initializer())
    saver.restore(session, checkpoint_path)
//  [python]
def _run_and_test(self, hparams, proc_shr=False, length_inc=None,
                      discard_src=False):
        # Construct database
        text_data = tx.data.PairedTextData(hparams)
        self.assertEqual(
            text_data.source_vocab.size,
            self._vocab_size + len(text_data.source_vocab.special_tokens))

        iterator = text_data.dataset.make_initializable_iterator()
        text_data_batch = iterator.get_next()

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            sess.run(tf.tables_initializer())
            sess.run(iterator.initializer)

            if proc_shr:
                tgt_eos = b'<EOS>'
            else:
                tgt_eos = b'<TARGET_EOS>'

            while True:
                try:
                    # Run the logics
                    data_batch_ = sess.run(text_data_batch)
                    self.assertEqual(set(data_batch_.keys()),
                                     set(text_data.list_items()))
                    # Test matching
                    src_text = data_batch_['source_text']
                    tgt_text = data_batch_['target_text']
                    if proc_shr:
                        for src, tgt in zip(src_text, tgt_text):
                            np.testing.assert_array_equal(src[:3], tgt[:3])
                    else:
                        for src, tgt in zip(src_text, tgt_text):
                            np.testing.assert_array_equal(src[:3], tgt[1:4])
                    self.assertTrue(
                        tgt_eos in data_batch_['target_text'][0])

                    if length_inc:
                        for i in range(len(data_batch_['source_text'])):
                            text_ = data_batch_['source_text'][i].tolist()
                            self.assertEqual(
                                text_.index(b'<EOS>') + 1,
                                data_batch_['source_length'][i] - length_inc[0])
                        for i in range(len(data_batch_['target_text'])):
                            text_ = data_batch_['target_text'][i].tolist()
                            self.assertEqual(
                                text_.index(tgt_eos) + 1,
                                data_batch_['target_length'][i] - length_inc[1])

                    if discard_src:
                        src_hparams = text_data.hparams.source_dataset
                        max_l = src_hparams.max_seq_length
                        max_l += text_data._decoder[0].added_length
                        for l in data_batch_[text_data.source_length_name]:
                            self.assertLessEqual(l, max_l)

                except tf.errors.OutOfRangeError:
                    print('Done -- epoch limit reached')
                    break
//  [python]
def graph_demo_training():
  """Not used in the benchmark. Used to confirm a functional model."""
  with tf.Graph().as_default():
    tf.set_random_seed(0)

    network = GraphPolicyNetwork(hidden_size=5)
    network.build((1, 4))
    env = GraphGymCartpoleEnv()
    opt = tf.train.AdamOptimizer(0.05)

    train_ops = graph_train_model(network, env, opt, iterations=5)

    with tf.Session() as sess:
      sess.run(tf.initialize_all_variables())
      sess.run(tf.local_variables_initializer())
      steps_per_iteration = sess.run(train_ops)
      for i, steps in enumerate(steps_per_iteration):
        print('Step {} iterations: {}'.format(i, steps))
//  [python]
def graph_demo_training():
  """Not used in the benchmark. Used to confirm a functional model."""
  with tf.Graph().as_default():
    tf.set_random_seed(0)

    network = GraphPolicyNetwork(hidden_size=5)
    network.build((1, 4))
    env = GraphGymCartpoleEnv()
    opt = tf.train.AdamOptimizer(0.05)

    train_ops = graph_train_model(network, env, opt, iterations=5)

    with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      sess.run(tf.local_variables_initializer())
      steps_per_iteration = sess.run(train_ops)
      for i, steps in enumerate(steps_per_iteration):
        print('Step {} iterations: {}'.format(i, steps))
//  [python]
def _single_image_write(self):
        # Create the queue operations
        img_key, img_tensor, _ = queue_single_images_from_folder(DIR_RESIZED)

        # Create a writer to write_image the images
        single_writer = SingleImageRecordWriter("single_images.tfrecord", DIR_TFRECORDS)

        # Start a new session to run the operations
        with tf.Session() as sess:
            sess.run(
                [tf.global_variables_initializer(), tf.local_variables_initializer()]
            )

            # Coordinate the loading of image files.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)

            count = 0
            start_time = time.time()
            try:
                while not coord.should_stop():
                    key, img = sess.run([img_key, img_tensor])
                    single_writer.write_image(key, img)
                    print("Written: {}".format(key))
                    count += 1
            except tf.errors.OutOfRangeError:
                # The string_input_producer queue ran out of strings
                pass
            finally:
                # Ask the threads (filename queue) to stop.
                coord.request_stop()
                print(
                    "Finished writing {} images in {:.2f}s".format(
                        count, time.time() - start_time
                    )
                )

            # Wait for threads to finish.
            coord.join(threads)

        single_writer.close()
//  [python]
def _single_image_read(self):
        # Important: read_batch MUST be called before start_queue_runners,
        # otherwise the internal shuffle queue gets created but its
        # threads won't start
        irr = SingleImageRecordReader("single_images.tfrecord", DIR_TFRECORDS)
        read_one_example = irr.read_operation
        read_batched_examples = irr.read_batch(10)

        with tf.Session() as sess:
            sess.run(
                [tf.initialize_all_variables(), tf.local_variables_initializer()]
            )

            # Coordinate the loading of image files.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)

            # Reading images sequentially one by one
            for i in range(6):
                res = sess.run(read_one_example)
                plt.subplot(2, 3, i + 1)
                plt.imshow(res["image"])
                plt.axis("off")
                print("Read", basename(res["key"]))
            plt.show()

            # Reading images in batch
            res = sess.run(read_batched_examples)
            print(res["key"], res["image"].shape)

            # Finish off the filename queue coordinator.
            coord.request_stop()
            coord.join(threads)
//  [python]
def _single_image_read(self):
        # Important: read_batch MUST be called before start_queue_runners,
        # otherwise the internal shuffle queue gets created but its
        # threads won't start
        irr = SingleImageRecordReader("single_images.tfrecord", DIR_TFRECORDS)
        read_one_example = irr.read_operation
        read_batched_examples = irr.read_batch(10)

        with tf.Session() as sess:
            sess.run(
                [tf.global_variables_initializer(), tf.local_variables_initializer()]
            )

            # Coordinate the loading of image files.
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)

            # Reading images sequentially one by one
            for i in range(6):
                res = sess.run(read_one_example)
                plt.subplot(2, 3, i + 1)
                plt.imshow(res["image"])
                plt.axis("off")
                print("Read", basename(res["key"]))
            plt.show()

            # Reading images in batch
            res = sess.run(read_batched_examples)
            print(res["key"], res["image"].shape)

            # Finish off the filename queue coordinator.
            coord.request_stop()
            coord.join(threads)
//  [python]
def restore(self, session):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
        vars_to_restore = [v for v in tf.global_variables() if "module/" not in v.name]
        saver = tf.train.Saver(vars_to_restore)
        checkpoint_path = os.path.join(self.config["log_dir"], "model.max.ckpt")
        print("Restoring from {}".format(checkpoint_path))
        session.run(tf.initialize_all_variables())
        saver.restore(session, checkpoint_path)
//  [python]
def restore(self, session):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
        vars_to_restore = [v for v in tf.global_variables() if "module/" not in v.name]
        saver = tf.train.Saver(vars_to_restore)
        checkpoint_path = os.path.join(self.config["log_dir"], "model.max.ckpt")
        print("Restoring from {}".format(checkpoint_path))
        session.run(tf.global_variables_initializer())
        saver.restore(session, checkpoint_path)
//  [python]
def benchmark_cartpole(self):

    def train_session(sess, ops):
      return lambda: sess.run(ops)

    def train_eager(network, env, opt):
      return lambda: eager_train_model(network, env, opt, iterations=10)

    for model_size in (10, 100, 1000):
      with tf.Graph().as_default():
        network = GraphPolicyNetwork(hidden_size=model_size)
        network.build((1, 4))
        env = GraphRandomCartpoleEnv()
        opt = tf.train.AdamOptimizer(0.05)
        train_ops = graph_train_model(network, env, opt, iterations=10)

        with tf.Session() as sess:
          sess.run(tf.global_variables_initializer())
          sess.run(tf.local_variables_initializer())

          self.time_execution(('cartpole', 'autograph', model_size),
                              train_session(sess, train_ops), 20)

      with context.eager_mode():
        network = EagerPolicyNetwork(hidden_size=model_size)
        network.build((1, 4))
        env = EagerRandomCartpoleEnv()
        opt = tf.train.AdamOptimizer(0.05)

        self.time_execution(('cartpole', 'eager', model_size),
                            train_eager(network, env, opt), 20)
//  [python]
def create_session(self):
        # Set up tf session and initialize variables.
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        
        global_init = tf.initialize_all_variables()
        local_init = tf.local_variables_initializer()
        
        self.sess = tf.Session(config=config)
        self.sess.run([global_init, local_init])
//  [python]
def create_session(self):
        # Set up tf session and initialize variables.
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        
        global_init = tf.global_variables_initializer()
        local_init = tf.local_variables_initializer()
        
        self.sess = tf.Session(config=config)
        self.sess.run([global_init, local_init])
//  [python]
def test():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 3
    learning_rate = 0.0001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 30
    num_sentences = 6  # number of sentences
    vocab_size = 10000
    embed_size = 100 #100
    hidden_size = 100
    is_training = True
    dropout_keep_prob = 1  # 0.5 #num_sentences
    textRNN = HierarchicalAttention(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                                    num_sentences, vocab_size, embed_size,
                                    hidden_size, is_training,multi_label_flag=False)
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for i in range(1500):
            # input_x should be:[batch_size, num_sentences,self.sequence_length]
            input_x = np.random.randn(batch_size, sequence_length) #num_sentences
            input_x[input_x > 0.5] = 1
            input_x[input_x <= 0.5] = 0
            input_y=get_input_y(i,batch_size)
            loss, acc, predict, W_projection_value, _ = sess.run(
                [textRNN.loss_val, textRNN.accuracy, textRNN.predictions, textRNN.W_projection, textRNN.train_op],
                feed_dict={textRNN.input_x: input_x, textRNN.input_y: input_y,
                           textRNN.dropout_keep_prob: dropout_keep_prob})
            print(i,"loss:", loss, "acc:", acc, "label:", input_y, "prediction:", predict)
//  [python]
def test():
    # below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes = 3
    learning_rate = 0.0001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 30
    num_sentences = 6  # number of sentences
    vocab_size = 10000
    embed_size = 100 #100
    hidden_size = 100
    is_training = True
    dropout_keep_prob = 1  # 0.5 #num_sentences
    textRNN = HierarchicalAttention(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                                    num_sentences, vocab_size, embed_size,
                                    hidden_size, is_training,multi_label_flag=False)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(1500):
            # input_x should be:[batch_size, num_sentences,self.sequence_length]
            input_x = np.random.randn(batch_size, sequence_length) #num_sentences
            input_x[input_x > 0.5] = 1
            input_x[input_x <= 0.5] = 0
            input_y=get_input_y(i,batch_size)
            loss, acc, predict, W_projection_value, _ = sess.run(
                [textRNN.loss_val, textRNN.accuracy, textRNN.predictions, textRNN.W_projection, textRNN.train_op],
                feed_dict={textRNN.input_x: input_x, textRNN.input_y: input_y,
                           textRNN.dropout_keep_prob: dropout_keep_prob})
            print(i,"loss:", loss, "acc:", acc, "label:", input_y, "prediction:", predict)
//  [python]
def __init__(self, mode, task_id, vocabulary):

        # Initialize parameter values
        self.max_node_seq_len = 32                          # Maximum number of node subtokens
        self.max_var_seq_len = 16                           # Maximum number of variable subtokens
        self.max_slots = 64                                 # Maximum number of variable occurrences
        self.batch_size = 20000                             # Number of nodes per batch sample
        self.enable_batching = True
        self.learning_rate = 0.001
        self.ggnn_params = self.get_gnn_params()
        self.vocabulary = vocabulary
        self.voc_size = len(vocabulary)
        self.slot_id = self.vocabulary.get_id_or_unk('<SLOT>')
        self.sos_token_id = self.vocabulary.get_id_or_unk('sos_token')
        self.pad_token_id = self.vocabulary.get_id_or_unk(self.vocabulary.get_pad())
        self.embedding_size = self.ggnn_params['hidden_size']
        self.ggnn_dropout = 1.0
        self.task_type = task_id

        if mode != 'train' and mode != 'infer':
            raise ValueError("Invalid mode. Please specify \'train\' or \'infer\'...")


        self.graph = tf.Graph()
        self.mode = mode

        with self.graph.as_default():

            self.placeholders = {}
            self.make_model()
            self.sess = tf.Session(graph=self.graph, config=tf.ConfigProto())

            if self.mode == 'train':
                self.make_train_step()
                self.sess.run(tf.initialize_all_variables())


        print ("Model built successfully...")
//  [python]
def __init__(self, mode, task_id, vocabulary):

        # Initialize parameter values
        self.max_node_seq_len = 32                          # Maximum number of node subtokens
        self.max_var_seq_len = 16                           # Maximum number of variable subtokens
        self.max_slots = 64                                 # Maximum number of variable occurrences
        self.batch_size = 20000                             # Number of nodes per batch sample
        self.enable_batching = True
        self.learning_rate = 0.001
        self.ggnn_params = self.get_gnn_params()
        self.vocabulary = vocabulary
        self.voc_size = len(vocabulary)
        self.slot_id = self.vocabulary.get_id_or_unk('<SLOT>')
        self.sos_token_id = self.vocabulary.get_id_or_unk('sos_token')
        self.pad_token_id = self.vocabulary.get_id_or_unk(self.vocabulary.get_pad())
        self.embedding_size = self.ggnn_params['hidden_size']
        self.ggnn_dropout = 1.0
        self.task_type = task_id

        if mode != 'train' and mode != 'infer':
            raise ValueError("Invalid mode. Please specify \'train\' or \'infer\'...")


        self.graph = tf.Graph()
        self.mode = mode

        with self.graph.as_default():

            self.placeholders = {}
            self.make_model()
            self.sess = tf.Session(graph=self.graph, config=tf.ConfigProto())

            if self.mode == 'train':
                self.make_train_step()
                self.sess.run(tf.global_variables_initializer())


        print ("Model built successfully...")