jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_mnist():
    A = 28  # img_h
    B = 28  # img_w
    C = 1
    T = 10
    n_enc = 256
    n_z = 100
    n_dec = 256
    read_n = 5
    write_n = 5
    batch_size = 64
    mnist = MNIST(split=[0.8, 0.1, 0.1])

    n_examples = batch_size
    zs = np.random.uniform(-1.0, 1.0, [4, n_z]).astype(np.float32)
    zs = utils.make_latent_manifold(zs, n_examples)

    # We create a session to use the graph
    g = tf.Graph()
    with tf.Session(graph=g) as sess:
        draw = create_model(
            A=A,
            B=B,
            C=C,
            T=T,
            batch_size=batch_size,
            n_enc=n_enc,
            n_z=n_z,
            n_dec=n_dec,
            read_n=read_n,
            write_n=write_n)
        opt = tf.train.AdamOptimizer(learning_rate=0.0001)
        grads = opt.compute_gradients(draw['cost'])
        for i, (g, v) in enumerate(grads):
            if g is not None:
                grads[i] = (tf.clip_by_norm(g, 5), v)
        train_op = opt.apply_gradients(grads)
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)
        saver = tf.train.Saver()

        # Fit all training data
        batch_i = 0
        n_epochs = 100
        test_xs = mnist.test.images[:n_examples]
        utils.montage(test_xs.reshape((-1, A, B)), 'test_xs.png')
        for epoch_i in range(n_epochs):
            for batch_xs, _ in mnist.train.next_batch(batch_size):
                noise = np.random.randn(batch_size, n_z)
                lx, lz = sess.run(
                    [draw['loss_x'], draw['loss_z'], train_op],
                    feed_dict={draw['x']: batch_xs,
                               draw['noise']: noise})[0:2]
                print('x:', lx, 'z:', lz)
                if batch_i % 1000 == 0:
                    # Plot example reconstructions
                    recon = sess.run(
                        draw['canvas'],
                        feed_dict={draw['x']: test_xs,
                                   draw['noise']: noise})
                    recon = [utils.montage(r.reshape(-1, A, B)) for r in recon]
                    gif.build_gif(
                        recon,
                        cmap='gray',
                        saveto='manifold_%08d.gif' % batch_i)

                    saver.save(sess, './draw.ckpt', global_step=batch_i)

                batch_i += 1
//  [python]
def __init__(self, x_dim, learning_rate=0.01,
                 random_seed=None):
        self.x_dim = x_dim
        self.learning_rate = learning_rate
        self.g = tf.Graph()
        ## build the model
        with self.g.as_default():
            ## set graph-level random-seed
            tf.set_random_seed(random_seed)
            
            self.build()
            ## create initializer
            self.init_op = tf.initialize_all_variables()
//  [python]
def __init__(self, x_dim, learning_rate=0.01,
                 random_seed=None):
        self.x_dim = x_dim
        self.learning_rate = learning_rate
        self.g = tf.Graph()
        ## build the model
        with self.g.as_default():
            ## set graph-level random-seed
            tf.set_random_seed(random_seed)
            
            self.build()
            ## create initializer
            self.init_op = tf.global_variables_initializer()
//  [python]
def load_model(addr,sess):
    """Loads a model from the address of a checkpoint.

    Args:
        addr (str): Address of a directory to save checkpoint for current epoch.
        sess (obj): Tensor flow session object.

    """
    sess.run(tf.initialize_all_variables())
    saver = tf.train.Saver()
    saver.restore(sess, join(addr, 'model.ckpt'))
//  [python]
def load_model(addr,sess):
    """Loads a model from the address of a checkpoint.

    Args:
        addr (str): Address of a directory to save checkpoint for current epoch.
        sess (obj): Tensor flow session object.

    """
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, join(addr, 'model.ckpt'))
//  [python]
def _initalize_variables(hypes):
    if hypes['load_pretrained']:
        logging.info("Pretrained weights are loaded.")
        logging.info("The model is fine-tuned from previous training.")
        restore = hypes['restore']
        init = tf.initialize_all_variables()
        sess = tf.get_default_session()
        sess.run(init)

        saver = tf.train.Saver(var_list=restore)

        layers = hypes['arch']['layers']

        assert layers in [50, 101, 152]

        filename = checkpoint_fn(layers)

        if 'TV_DIR_DATA' in os.environ:
            filename = os.path.join(os.environ['TV_DIR_DATA'], 'weights',
                                    "tensorflow_resnet", filename)
        else:
            filename = os.path.join('DATA', 'weights', "tensorflow_resnet",
                                    filename)

        if not os.path.exists(filename):
            logging.error("File not found: {}".format(filename))
            logging.error("Please download weights from here: {}"
                          .format('network_url'))
            exit(1)

        logging.info("Loading weights from disk.")
        saver.restore(sess, filename)
    else:
        logging.info("Random initialization performed.")
        sess = tf.get_default_session()
        init = tf.initialize_all_variables()
        sess.run(init)
//  [python]
def _initalize_variables(hypes):
    if hypes['load_pretrained']:
        logging.info("Pretrained weights are loaded.")
        logging.info("The model is fine-tuned from previous training.")
        restore = hypes['restore']
        init = tf.global_variables_initializer()
        sess = tf.get_default_session()
        sess.run(init)

        saver = tf.train.Saver(var_list=restore)

        layers = hypes['arch']['layers']

        assert layers in [50, 101, 152]

        filename = checkpoint_fn(layers)

        if 'TV_DIR_DATA' in os.environ:
            filename = os.path.join(os.environ['TV_DIR_DATA'], 'weights',
                                    "tensorflow_resnet", filename)
        else:
            filename = os.path.join('DATA', 'weights', "tensorflow_resnet",
                                    filename)

        if not os.path.exists(filename):
            logging.error("File not found: {}".format(filename))
            logging.error("Please download weights from here: {}"
                          .format('network_url'))
            exit(1)

        logging.info("Loading weights from disk.")
        saver.restore(sess, filename)
    else:
        logging.info("Random initialization performed.")
        sess = tf.get_default_session()
        init = tf.global_variables_initializer()
        sess.run(init)
//  [python]
def test_categorical_column_with_vocabulary_list():
    color_data = {'color': [['R', 'R'], ['G', 'R'], ['B', 'G'], ['A', 'A']]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_vocabulary_list(
        'color', ['R', 'G', 'B'], dtype=tf.string, default_value=-1
    )

    color_column_tensor = color_column._get_sparse_tensors(builder)
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print(session.run([color_column_tensor.id_tensor]))

    # 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
    color_column_identy = feature_column.indicator_column(color_column)
    color_dense_tensor = feature_column.input_layer(color_data, [color_column_identy])
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('use input_layer' + '_' * 40)
        print(session.run([color_dense_tensor]))
//  [python]
def supervised_pretrain(sess,
                        model,
                        train_data,
                        num_steps,
                        num_eval_steps=10,
                        batch_size=100,
                        logging_fn=None):
  """Pretrain a supervised model on the labeled split of the training data to get a reasonable
  embedding model for baselines.

  Args:
    sess: TensorFlow session object.
    model: SupervisedModel object.
    train_data: Training dataset object.
    test_data: Testing dataset object.
    num_steps: Int. Number of training steps.
  """
  train_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx,
      log_epoch=-1)
  train_eval_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx,
      log_epoch=-1)
  test_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx_test,
      log_epoch=-1)

  sess.run(tf.initialize_all_variables())

  it = tqdm(six.moves.xrange(num_steps), ncols=0)
  for ii in it:
    x_train, y_train = train_iter.next()
    sess.run(
        [model.train_op],
        feed_dict={
            model.inputs: x_train,
            model.labels: y_train
        })
    if (ii + 1) % 100 == 0 or ii == 0:
      train_cost = 0.0
      train_acc = 0.0
      for jj in six.moves.xrange(num_eval_steps):
        x_train, y_train = train_eval_iter.next()
        cost_, acc_ = sess.run(
            [model.cost, model.acc],
            feed_dict={
                model.inputs: x_train,
                model.labels: y_train
            })
        train_cost += cost_ / num_eval_steps
        train_acc += acc_ / num_eval_steps

      test_cost = 0.0
      test_acc = 0.0
      for jj in six.moves.xrange(num_eval_steps):
        x_train, y_train = test_iter.next()
        cost_, acc_ = sess.run(
            [model.cost, model.acc],
            feed_dict={
                model.inputs: x_train,
                model.labels: y_train
            })
        test_cost += cost_ / num_eval_steps
        test_acc += acc_ / num_eval_steps

      learn_rate = sess.run(model.learn_rate)
      if logging_fn is not None:
        logging_fn(
            ii + 1, {
                'train_cost': train_cost,
                'train_acc': train_acc,
                'test_cost': test_cost,
                'test_acc': test_acc,
                'learn_rate': learn_rate
            })
      it.set_postfix(
          ce='{:.3e}'.format(train_cost),
          train_acc='{:.3f}%'.format(train_acc * 100),
          test_acc='{:.3f}%'.format(test_acc * 100),
          lr='{:.3e}'.format(learn_rate))
//  [python]
def supervised_pretrain(sess,
                        model,
                        train_data,
                        num_steps,
                        num_eval_steps=10,
                        batch_size=100,
                        logging_fn=None):
  """Pretrain a supervised model on the labeled split of the training data to get a reasonable
  embedding model for baselines.

  Args:
    sess: TensorFlow session object.
    model: SupervisedModel object.
    train_data: Training dataset object.
    test_data: Testing dataset object.
    num_steps: Int. Number of training steps.
  """
  train_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx,
      log_epoch=-1)
  train_eval_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx,
      log_epoch=-1)
  test_iter = BatchIterator(
      train_data.get_size(),
      batch_size=batch_size,
      cycle=True,
      shuffle=True,
      get_fn=train_data.get_batch_idx_test,
      log_epoch=-1)

  sess.run(tf.global_variables_initializer())

  it = tqdm(six.moves.xrange(num_steps), ncols=0)
  for ii in it:
    x_train, y_train = train_iter.next()
    sess.run(
        [model.train_op],
        feed_dict={
            model.inputs: x_train,
            model.labels: y_train
        })
    if (ii + 1) % 100 == 0 or ii == 0:
      train_cost = 0.0
      train_acc = 0.0
      for jj in six.moves.xrange(num_eval_steps):
        x_train, y_train = train_eval_iter.next()
        cost_, acc_ = sess.run(
            [model.cost, model.acc],
            feed_dict={
                model.inputs: x_train,
                model.labels: y_train
            })
        train_cost += cost_ / num_eval_steps
        train_acc += acc_ / num_eval_steps

      test_cost = 0.0
      test_acc = 0.0
      for jj in six.moves.xrange(num_eval_steps):
        x_train, y_train = test_iter.next()
        cost_, acc_ = sess.run(
            [model.cost, model.acc],
            feed_dict={
                model.inputs: x_train,
                model.labels: y_train
            })
        test_cost += cost_ / num_eval_steps
        test_acc += acc_ / num_eval_steps

      learn_rate = sess.run(model.learn_rate)
      if logging_fn is not None:
        logging_fn(
            ii + 1, {
                'train_cost': train_cost,
                'train_acc': train_acc,
                'test_cost': test_cost,
                'test_acc': test_acc,
                'learn_rate': learn_rate
            })
      it.set_postfix(
          ce='{:.3e}'.format(train_cost),
          train_acc='{:.3f}%'.format(train_acc * 100),
          test_acc='{:.3f}%'.format(test_acc * 100),
          lr='{:.3e}'.format(learn_rate))
//  [python]
def test_embedding():
    tf.set_random_seed(1)
    color_data = {'color': [['R', 'G'], ['G', 'A'], ['B', 'B'], ['A', 'A']]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_vocabulary_list(
        'color', ['R', 'G', 'B'], dtype=tf.string, default_value=-1
    )
    color_column_tensor = color_column._get_sparse_tensors(builder)

    color_embeding = feature_column.embedding_column(color_column, 4, combiner='sum')
    color_embeding_dense_tensor = feature_column.input_layer(color_data, [color_embeding])

    with tf.Session() as session:
        session.run(tf.initialize_all_variables())
        session.run(tf.tables_initializer())
        print(session.run([color_column_tensor.id_tensor]))
        print('embeding' + '_' * 40)
        print(session.run([color_embeding_dense_tensor]))
//  [python]
def test_embedding():
    tf.set_random_seed(1)
    color_data = {'color': [['R', 'G'], ['G', 'A'], ['B', 'B'], ['A', 'A']]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_vocabulary_list(
        'color', ['R', 'G', 'B'], dtype=tf.string, default_value=-1
    )
    color_column_tensor = color_column._get_sparse_tensors(builder)

    color_embeding = feature_column.embedding_column(color_column, 4, combiner='sum')
    color_embeding_dense_tensor = feature_column.input_layer(color_data, [color_embeding])

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print(session.run([color_column_tensor.id_tensor]))
        print('embeding' + '_' * 40)
        print(session.run([color_embeding_dense_tensor]))
//  [python]
def test_categorical_column_with_hash_bucket():
    color_data = {'color': [[2], [5], [-1], [0]]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_hash_bucket('color', 7, dtype=tf.int32)
    color_column_tensor = color_column._get_sparse_tensors(builder)
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print(session.run([color_column_tensor.id_tensor]))

    # 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
    color_column_identy = feature_column.indicator_column(color_column)
    color_dense_tensor = feature_column.input_layer(color_data, [color_column_identy])

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('use input_layer' + '_' * 40)
        print(session.run([color_dense_tensor]))
//  [python]
def test_embedding_column_with_hash_bucket():
    color_data = {'color': [[2, 2], [5, 5], [0, -1], [0, 0]]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_hash_bucket('color', 7, dtype=tf.int32)
    color_column_tensor = color_column._get_sparse_tensors(builder)
    with tf.Session() as session:
        session.run(tf.initialize_all_variables())
        session.run(tf.tables_initializer())
        print('not use input_layer' + '_' * 40)
        print(session.run([color_column_tensor.id_tensor]))

    # 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
    color_column_embed = feature_column.embedding_column(color_column, 4, combiner='sum')
    color_dense_tensor = feature_column.input_layer(color_data, [color_column_embed])

    with tf.Session() as session:
        session.run(tf.initialize_all_variables())
        session.run(tf.tables_initializer())
        print('use input_layer' + '_' * 40)
        print(session.run([color_dense_tensor]))
//  [python]
def test_embedding_column_with_hash_bucket():
    color_data = {'color': [[2, 2], [5, 5], [0, -1], [0, 0]]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_hash_bucket('color', 7, dtype=tf.int32)
    color_column_tensor = color_column._get_sparse_tensors(builder)
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('not use input_layer' + '_' * 40)
        print(session.run([color_column_tensor.id_tensor]))

    # 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
    color_column_embed = feature_column.embedding_column(color_column, 4, combiner='sum')
    color_dense_tensor = feature_column.input_layer(color_data, [color_column_embed])

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('use input_layer' + '_' * 40)
        print(session.run([color_dense_tensor]))
//  [python]
def test_shared_embedding_column_with_hash_bucket():
    print('*' * 40)
    color_data = {'color': [[2, 2], [5, 5], [0, -1], [0, 0]],
                  'color2': [[2], [5], [-1], [0]]}  # 4行样本
    builder = _LazyBuilder(color_data)
    color_column = feature_column.categorical_column_with_hash_bucket('color', 7, dtype=tf.int32)
    color_column_tensor = color_column._get_sparse_tensors(builder)
    color_column2 = feature_column.categorical_column_with_hash_bucket('color2', 7, dtype=tf.int32)
    color_column_tensor2 = color_column2._get_sparse_tensors(builder)
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('not use input_layer' + '_' * 40)
        print(session.run([color_column_tensor.id_tensor]))
        print(session.run([color_column_tensor2.id_tensor]))

    # 将稀疏的转换成dense，也就是one-hot形式，只是multi-hot
    color_column_embed = feature_column.shared_embedding_columns([color_column2, color_column], 3, combiner='sum')
    print(type(color_column_embed))
    color_dense_tensor = feature_column.input_layer(color_data, color_column_embed)

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        session.run(tf.tables_initializer())
        print('use input_layer' + '_' * 40)
        print(session.run(color_dense_tensor))
//  [python]
def testBuildCell(self):
    with self.test_session(graph=tf.Graph()) as sess:
      features = tf.random_normal([2, 33, 33, 5])
      concat_logits = self.segmentation_layer.build_cell(
          features,
          output_stride=8,
          crop_size=[257, 257])
      sess.run(tf.compat.v1.global_variables_initializer())
      concat_logits = sess.run(concat_logits)
      self.assertTrue(concat_logits.any())
//  [python]
def testBuildCell(self):
    with self.test_session(graph=tf.Graph()) as sess:
      features = tf.random_normal([2, 33, 33, 5])
      concat_logits = self.segmentation_layer.build_cell(
          features,
          output_stride=8,
          crop_size=[257, 257])
      sess.run(tf.global_variables_initializer())
      concat_logits = sess.run(concat_logits)
      self.assertTrue(concat_logits.any())
//  [python]
def test_lstm_example():
    import tensorflow as tf
    from baselines.common import policies, models, cmd_util
    from baselines.common.vec_env.dummy_vec_env import DummyVecEnv

    # create vectorized environment
    venv = DummyVecEnv([lambda: cmd_util.make_mujoco_env('Reacher-v2', seed=0)])

    with tf.Session() as sess:
        # build policy based on lstm network with 128 units
        policy = policies.build_policy(venv, models.lstm(128))(nbatch=1, nsteps=1)

        # initialize tensorflow variables
        sess.run(tf.global_variables_initializer())

        # prepare environment variables
        ob = venv.reset()
        state = policy.initial_state
        done = [False]
        step_counter = 0

        # run a single episode until the end (i.e. until done)
        while True:
            action, _, state, _ = policy.step(ob, S=state, M=done)
            ob, reward, done, _ = venv.step(action)
            step_counter += 1
            if done:
                break


        assert step_counter > 5
//  [python]
def testBuildCellWithImagePoolingCropSize(self):
    with self.test_session(graph=tf.Graph()) as sess:
      features = tf.random_normal([2, 33, 33, 5])
      concat_logits = self.segmentation_layer.build_cell(
          features,
          output_stride=8,
          crop_size=[257, 257],
          image_pooling_crop_size=[129, 129])
      sess.run(tf.initialize_all_variables())
      concat_logits = sess.run(concat_logits)
      self.assertTrue(concat_logits.any())
//  [python]
def testBuildCellWithImagePoolingCropSize(self):
    with self.test_session(graph=tf.Graph()) as sess:
      features = tf.random_normal([2, 33, 33, 5])
      concat_logits = self.segmentation_layer.build_cell(
          features,
          output_stride=8,
          crop_size=[257, 257],
          image_pooling_crop_size=[129, 129])
      sess.run(tf.global_variables_initializer())
      concat_logits = sess.run(concat_logits)
      self.assertTrue(concat_logits.any())
//  [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.01
    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(100):
            # input_x should be:[batch_size, num_sentences,self.sequence_length]
            input_x = np.zeros((batch_size, sequence_length)) #num_sentences
            input_x[input_x > 0.5] = 1
            input_x[input_x <= 0.5] = 0
            input_y = np.array(
                [1, 0, 1, 1, 1, 2, 1, 1])  # np.zeros((batch_size),dtype=np.int32) #[None, self.sequence_length]
            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("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.01
    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(100):
            # input_x should be:[batch_size, num_sentences,self.sequence_length]
            input_x = np.zeros((batch_size, sequence_length)) #num_sentences
            input_x[input_x > 0.5] = 1
            input_x[input_x <= 0.5] = 0
            input_y = np.array(
                [1, 0, 1, 1, 1, 2, 1, 1])  # np.zeros((batch_size),dtype=np.int32) #[None, self.sequence_length]
            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("loss:", loss, "acc:", acc, "label:", input_y, "prediction:", predict)
//  [python]
def compile(self):
        self.emb_l2_norm_op = None
        self.sess = UTILS.get_session()
        self.make_io()
        self.make_model()
        self.sess.run(tf.initialize_all_variables())
        if self.emb_l2_norm_op is not None:
            self.sess.run(self.emb_l2_norm_op)
//  [python]
def compile(self):
        self.emb_l2_norm_op = None
        self.sess = UTILS.get_session()
        self.make_io()
        self.make_model()
        self.sess.run(tf.global_variables_initializer())
        if self.emb_l2_norm_op is not None:
            self.sess.run(self.emb_l2_norm_op)
//  [python]
def __init__(self):
        self.sess = tf.Session()
        self.sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
        self.use_outputs = None
        self.model_name = None
        self.max_seq_length = None

        # placeholder for dan and large model
        self.sentences = None

        # sentencepiece and place holder model for lite version
        self.sp_model = spm.SentencePieceProcessor()
        self.input_placeholder = None
//  [python]
def __init__(self, model, restore_snap=None):

        # Add training ops
        self.add_train_ops(model)

        # Tensorflow Saver definition
        my_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='KernelPointNetwork')
        self.saver = tf.train.Saver(my_vars, max_to_keep=100)

        # Create a session for running Ops on the Graph.
        on_CPU = False
        if on_CPU:
            cProto = tf.ConfigProto(device_count={'GPU': 0})
        else:
            cProto = tf.ConfigProto()
            cProto.gpu_options.allow_growth = True
        self.sess = tf.Session(config=cProto)

        # Init variables
        self.sess.run(tf.initialize_all_variables())

        # Name of the snapshot to restore to (None if you want to start from beginning)
        if (restore_snap is not None):
            self.saver.restore(self.sess, restore_snap)
            print("Model restored from " + restore_snap)
//  [python]
def __init__(self, model, restore_snap=None):

        # Add training ops
        self.add_train_ops(model)

        # Tensorflow Saver definition
        my_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='KernelPointNetwork')
        self.saver = tf.train.Saver(my_vars, max_to_keep=100)

        # Create a session for running Ops on the Graph.
        on_CPU = False
        if on_CPU:
            cProto = tf.ConfigProto(device_count={'GPU': 0})
        else:
            cProto = tf.ConfigProto()
            cProto.gpu_options.allow_growth = True
        self.sess = tf.Session(config=cProto)

        # Init variables
        self.sess.run(tf.global_variables_initializer())

        # Name of the snapshot to restore to (None if you want to start from beginning)
        if (restore_snap is not None):
            self.saver.restore(self.sess, restore_snap)
            print("Model restored from " + restore_snap)
//  [python]
def __init__(self,
                 num_users,
                 num_items,
                 embed_dim,
                 batch_size=10000,
                 margin=1.0,
                 clip_norm=1.0,
                 cov_loss_weight=0.01,
                 **unused):
        self.num_users = num_users
        self.num_items = num_items
        self.embed_dim = embed_dim
        self.batch_size = batch_size
        self.margin = margin
        self.clip_norm = clip_norm
        self.cov_loss_weight = cov_loss_weight
        self.get_graph()
        self.sess = tf.Session()
        self.sess.run(tf.initialize_all_variables())
//  [python]
def __init__(self,
                 num_users,
                 num_items,
                 embed_dim,
                 batch_size=10000,
                 margin=1.0,
                 clip_norm=1.0,
                 cov_loss_weight=0.01,
                 **unused):
        self.num_users = num_users
        self.num_items = num_items
        self.embed_dim = embed_dim
        self.batch_size = batch_size
        self.margin = margin
        self.clip_norm = clip_norm
        self.cov_loss_weight = cov_loss_weight
        self.get_graph()
        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())
//  [python]
def load_model(self, model: str, model_path: str, max_seq_length: int):
        spm_path_info = None
        g = tf.Graph()
        with g.as_default():
            hub_module = hub.Module(model_path)
            if model == 'use_transformer_lite':
                self.input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
                self.use_outputs = hub_module(
                    inputs=dict(
                        values=self.input_placeholder.values,
                        indices=self.input_placeholder.indices,
                        dense_shape=self.input_placeholder.dense_shape)
                )
                spm_path_info = hub_module(signature="spm_path")
            else:
                self.sentences = tf.placeholder(tf.string, shape=[None])
                self.use_outputs = hub_module(self.sentences, as_dict=True)
            init_op = tf.group([tf.initialize_all_variables(), tf.tables_initializer()])

        g.finalize()
        self.sess = tf.Session(graph=g)
        self.sess.run(init_op)

        if model == 'use_transformer_lite':
            spm_path = self.sess.run(spm_path_info)
            self.sp_model.Load(spm_path)

        self.model_name = model
        self.max_seq_length = max_seq_length
//  [python]
def load_model(self, model: str, model_path: str, max_seq_length: int):
        spm_path_info = None
        g = tf.Graph()
        with g.as_default():
            hub_module = hub.Module(model_path)
            if model == 'use_transformer_lite':
                self.input_placeholder = tf.sparse_placeholder(tf.int64, shape=[None, None])
                self.use_outputs = hub_module(
                    inputs=dict(
                        values=self.input_placeholder.values,
                        indices=self.input_placeholder.indices,
                        dense_shape=self.input_placeholder.dense_shape)
                )
                spm_path_info = hub_module(signature="spm_path")
            else:
                self.sentences = tf.placeholder(tf.string, shape=[None])
                self.use_outputs = hub_module(self.sentences, as_dict=True)
            init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])

        g.finalize()
        self.sess = tf.Session(graph=g)
        self.sess.run(init_op)

        if model == 'use_transformer_lite':
            spm_path = self.sess.run(spm_path_info)
            self.sp_model.Load(spm_path)

        self.model_name = model
        self.max_seq_length = max_seq_length
//  [python]
def build(self, vocab_list: List[str], **kwargs) -> (List[str], List[str], Dict[str, Dict[str, Any]]):
        page_size = kwargs.get('page_size', 15000)
        init_weight = kwargs.get('init_weight', 0.1)

        logger.info('  Loading TensorFlow HUB module...')
        environ["TFHUB_CACHE_DIR"] = ".tfhub_modules"
        embedder = hub.Module(self._embedding_type.url)

        num_pages = len(vocab_list) // page_size
        oov, iov = [], []
        vec_dict = {}
        with tf.Session() as sess:
            sess.run([tf.global_variables_initializer(), tf.tables_initializer()])

            for i in range(num_pages + 1):
                lb = i * page_size
                ub = min((i + 1) * page_size, len(vocab_list))

                page = vocab_list[lb:ub]
                embedding_vectors = sess.run(embedder(page))

                for i, word in enumerate(page):
                    is_oov = sum(embedding_vectors[i]) == 0
                    if is_oov:
                        vec = np.random.uniform(-init_weight, init_weight, self._embedding_type.dim)
                    else:
                        vec = embedding_vectors[i]

                    vec_dict[word] = {
                        "vec": vec,
                        "trainable": is_oov
                    }

                    if is_oov:
                        oov.append(word)
                    else:
                        iov.append(word)

        return oov, iov, vec_dict
//  [python]
def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.005,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=200,
            memory_size=3000,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
            double_q=True,
            sess=None,
    ):

        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        # 决定是否要使用double - q -network
        self.double_q = double_q

        self.learn_step_counter = 0
        self.memory = np.zeros((self.memory_size, n_features * 2 + 2))

        self._build_net()

        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')

        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]


        if sess is None:
            self.sess = tf.Session()
            self.sess.run(tf.initialize_all_variables())
        else:
            self.sess = sess


        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)
        self.cost_his = []
//  [python]
def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.005,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=200,
            memory_size=3000,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
            double_q=True,
            sess=None,
    ):

        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        # 决定是否要使用double - q -network
        self.double_q = double_q

        self.learn_step_counter = 0
        self.memory = np.zeros((self.memory_size, n_features * 2 + 2))

        self._build_net()

        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')

        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]


        if sess is None:
            self.sess = tf.Session()
            self.sess.run(tf.global_variables_initializer())
        else:
            self.sess = sess


        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)
        self.cost_his = []
//  [python]
def test_extract_box_classifier_features_returns_expected_size(self):
    feature_extractor = self._build_feature_extractor(
        first_stage_features_stride=16)
    proposal_feature_maps = tf.random_uniform(
        [3, 14, 14, 576], maxval=255, dtype=tf.float32)
    proposal_classifier_features = (
        feature_extractor.extract_box_classifier_features(
            proposal_feature_maps, scope='TestScope'))
    features_shape = tf.shape(proposal_classifier_features)

    init_op = tf.initialize_all_variables()
    with self.test_session() as sess:
      sess.run(init_op)
      features_shape_out = sess.run(features_shape)
      self.assertAllEqual(features_shape_out, [3, 7, 7, 1024])
//  [python]
def test_extract_box_classifier_features_returns_expected_size(self):
    feature_extractor = self._build_feature_extractor(
        first_stage_features_stride=16)
    proposal_feature_maps = tf.random_uniform(
        [3, 14, 14, 576], maxval=255, dtype=tf.float32)
    proposal_classifier_features = (
        feature_extractor.extract_box_classifier_features(
            proposal_feature_maps, scope='TestScope'))
    features_shape = tf.shape(proposal_classifier_features)

    init_op = tf.global_variables_initializer()
    with self.test_session() as sess:
      sess.run(init_op)
      features_shape_out = sess.run(features_shape)
      self.assertAllEqual(features_shape_out, [3, 7, 7, 1024])
//  [python]
def test_extract_proposal_features_returns_expected_size(self):
    feature_extractor = self._build_feature_extractor(
        first_stage_features_stride=16)
    preprocessed_inputs = tf.random_uniform(
        [4, 224, 224, 3], maxval=255, dtype=tf.float32)
    rpn_feature_map, _ = feature_extractor.extract_proposal_features(
        preprocessed_inputs, scope='TestScope')
    features_shape = tf.shape(rpn_feature_map)

    init_op = tf.initialize_all_variables()
    with self.test_session() as sess:
      sess.run(init_op)
      features_shape_out = sess.run(features_shape)
      self.assertAllEqual(features_shape_out, [4, 14, 14, 512])
//  [python]
def test_extract_proposal_features_returns_expected_size(self):
    feature_extractor = self._build_feature_extractor(
        first_stage_features_stride=16)
    preprocessed_inputs = tf.random_uniform(
        [4, 224, 224, 3], maxval=255, dtype=tf.float32)
    rpn_feature_map, _ = feature_extractor.extract_proposal_features(
        preprocessed_inputs, scope='TestScope')
    features_shape = tf.shape(rpn_feature_map)

    init_op = tf.global_variables_initializer()
    with self.test_session() as sess:
      sess.run(init_op)
      features_shape_out = sess.run(features_shape)
      self.assertAllEqual(features_shape_out, [4, 14, 14, 512])
//  [python]
def restore(self, sess, restore_first=True):
      if restore_first:
          sess.run(tf.global_variables_initializer())
          self.init_fn(sess)
      else:
          self.cont_fn(sess)
//  [python]
def main(gpu_id = None):

    if gpu_id is not None:
        os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id

    # Print key parameters
    print_important_params()

    # Reset TensorFlow before running anything
    tf.reset_default_graph()

    #Create the stimulus class to generate trial paramaters and input activity
    stim = stimulus.Stimulus()

    # Define all placeholder
    m = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size']], 'mask')
    x = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size'], par['n_input']], 'input')
    t = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size'], par['n_output']], 'target')

    # enter "config=tf.ConfigProto(log_device_placement=True)" inside Session to check whether CPU/GPU in use
    with tf.Session(config=tf.ConfigProto()) as sess:

        device = '/cpu:0' if gpu_id is None else '/gpu:0'
        with tf.device(device):
            model = Model(x, t, m)

        sess.run(tf.initialize_all_variables())

        # keep track of the model performance across training
        model_performance = {'accuracy': [], 'loss': [], 'perf_loss': [], 'spike_loss': [], \
            'weight_loss': [], 'iteration': []}


        for i in range(par['num_iterations']):

            # generate batch of batch_train_size
            trial_info = stim.generate_trial(set_rule = None)

            # Run the model
            _, loss, perf_loss, spike_loss, weight_loss, y, h, syn_x, syn_u = \
                sess.run([model.train_op, model.loss, model.perf_loss, model.spike_loss, \
                model.weight_loss, model.y, model.h, model.syn_x, model.syn_u], \
                {x: trial_info['neural_input'], t: trial_info['desired_output'], m: trial_info['train_mask']})

            accuracy, _, _ = analysis.get_perf(trial_info['desired_output'], y, trial_info['train_mask'])

            model_performance = append_model_performance(model_performance, accuracy, loss, perf_loss, spike_loss, weight_loss, i)

            # Save the network model and output model performance to screen
            if i%par['iters_between_outputs']==0:
                print_results(i, perf_loss, spike_loss, weight_loss, h, accuracy)

        # Save model and results
        weights = sess.run(model.var_dict)
        save_results(model_performance, weights)
//  [python]
def main(gpu_id = None):

    if gpu_id is not None:
        os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id

    # Print key parameters
    print_important_params()

    # Reset TensorFlow before running anything
    tf.reset_default_graph()

    #Create the stimulus class to generate trial paramaters and input activity
    stim = stimulus.Stimulus()

    # Define all placeholder
    m = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size']], 'mask')
    x = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size'], par['n_input']], 'input')
    t = tf.placeholder(tf.float32, [par['num_time_steps'], par['batch_size'], par['n_output']], 'target')

    # enter "config=tf.ConfigProto(log_device_placement=True)" inside Session to check whether CPU/GPU in use
    with tf.Session(config=tf.ConfigProto()) as sess:

        device = '/cpu:0' if gpu_id is None else '/gpu:0'
        with tf.device(device):
            model = Model(x, t, m)

        sess.run(tf.global_variables_initializer())

        # keep track of the model performance across training
        model_performance = {'accuracy': [], 'loss': [], 'perf_loss': [], 'spike_loss': [], \
            'weight_loss': [], 'iteration': []}


        for i in range(par['num_iterations']):

            # generate batch of batch_train_size
            trial_info = stim.generate_trial(set_rule = None)

            # Run the model
            _, loss, perf_loss, spike_loss, weight_loss, y, h, syn_x, syn_u = \
                sess.run([model.train_op, model.loss, model.perf_loss, model.spike_loss, \
                model.weight_loss, model.y, model.h, model.syn_x, model.syn_u], \
                {x: trial_info['neural_input'], t: trial_info['desired_output'], m: trial_info['train_mask']})

            accuracy, _, _ = analysis.get_perf(trial_info['desired_output'], y, trial_info['train_mask'])

            model_performance = append_model_performance(model_performance, accuracy, loss, perf_loss, spike_loss, weight_loss, i)

            # Save the network model and output model performance to screen
            if i%par['iters_between_outputs']==0:
                print_results(i, perf_loss, spike_loss, weight_loss, h, accuracy)

        # Save model and results
        weights = sess.run(model.var_dict)
        save_results(model_performance, weights)
//  [python]
def restore(self, sess, restore_first=True, saver=None, checkpoint=None):
        if restore_first:
            sess.run(self.init_op)
            sess.run(tf.initialize_all_variables())
            self.init_fn(sess)
        else:
            if checkpoint != None:
                ckpt_path = checkpoint.model_checkpoint_path
            else:
                ckpt = tf.train.get_checkpoint_state(self._checkpoint_path)
                ckpt_path = ckpt.model_checkpoint_path
            saver.restore(sess, ckpt_path)
//  [python]
def restore(self, sess, restore_first=True, saver=None, checkpoint=None):
        if restore_first:
            sess.run(self.init_op)
            sess.run(tf.global_variables_initializer())
            self.init_fn(sess)
        else:
            if checkpoint != None:
                ckpt_path = checkpoint.model_checkpoint_path
            else:
                ckpt = tf.train.get_checkpoint_state(self._checkpoint_path)
                ckpt_path = ckpt.model_checkpoint_path
            saver.restore(sess, ckpt_path)
//  [python]
def test(params):
    """Test function."""

    dataloader = MonodepthDataloader(args.data_path, args.filenames_file, params, args.dataset, args.mode)
    left  = dataloader.left_image_batch
    right = dataloader.right_image_batch

    model = MonodepthModel(params, args.mode, left, right)

    # SESSION
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)

    # SAVER
    train_saver = tf.train.Saver()

    # INIT
    sess.run(tf.initialize_all_variables())
    sess.run(tf.local_variables_initializer())
    coordinator = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coordinator)

    # RESTORE
    if args.checkpoint_path == '':
        restore_path = tf.train.latest_checkpoint(args.log_directory + '/' + args.model_name)
    else:
        restore_path = args.checkpoint_path.split(".")[0]
    train_saver.restore(sess, restore_path)

    num_test_samples = count_text_lines(args.filenames_file)

    print('now testing {} files'.format(num_test_samples))
    disparities    = np.zeros((num_test_samples, params.height, params.width), dtype=np.float32)
    disparities_pp = np.zeros((num_test_samples, params.height, params.width), dtype=np.float32)
    for step in range(num_test_samples):
        disp = sess.run(model.disp_left_est[0])
        disparities[step] = disp[0].squeeze()
        disparities_pp[step] = post_process_disparity(disp.squeeze())

    print('done.')

    print('writing disparities.')
    if args.output_directory == '':
        output_directory = os.path.dirname(args.checkpoint_path)
    else:
        output_directory = args.output_directory
    np.save(output_directory + '/disparities.npy',    disparities)
    np.save(output_directory + '/disparities_pp.npy', disparities_pp)

    print('done.')