jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def main(_):
  hvd.init()

  if not FLAGS.output_file:
    raise ValueError('You must supply the path to save to with --output_file')

  tf.logging.set_verbosity(tf.logging.INFO)
  with tf.Graph().as_default() as graph:
    if FLAGS.input_format=='NCHW':
        input_shape = [FLAGS.batch_size, 3, FLAGS.image_size, FLAGS.image_size]
    else:
        input_shape = [FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3]
    input_images = tf.placeholder(name='input', dtype=tf.float32, shape=input_shape)
    
    resnet50_config = resnet.model_architectures[FLAGS.model_name]
    network = resnet.ResnetModel(FLAGS.model_name, 
                                 FLAGS.num_classes,
                                 resnet50_config['layers'],
                                 resnet50_config['widths'],
                                 resnet50_config['expansions'],
                                 FLAGS.compute_format,
                                 FLAGS.input_format)
    probs, logits = network.build_model(
                input_images,
                training=False,
                reuse=False,
                use_final_conv=FLAGS.use_final_conv)
    
    if FLAGS.quantize:
        tf.contrib.quantize.experimental_create_eval_graph(symmetric=FLAGS.symmetric, 
                                                           use_qdq=FLAGS.use_qdq)

    # Define the saver and restore the checkpoint
    saver = tf.train.Saver()
    with tf.Session() as sess:
        if FLAGS.checkpoint:
            saver.restore(sess, FLAGS.checkpoint)
        else:
            sess.run(tf.initialize_all_variables())
        graph_def = graph.as_graph_def()
        frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, [probs.op.name])

    # Write out the frozen graph
    tf.io.write_graph(
        frozen_graph_def,
        os.path.dirname(FLAGS.output_file),
        os.path.basename(FLAGS.output_file),
        as_text=FLAGS.write_text_graphdef)
//  [python]
def main(_):
  hvd.init()

  if not FLAGS.output_file:
    raise ValueError('You must supply the path to save to with --output_file')

  tf.logging.set_verbosity(tf.logging.INFO)
  with tf.Graph().as_default() as graph:
    if FLAGS.input_format=='NCHW':
        input_shape = [FLAGS.batch_size, 3, FLAGS.image_size, FLAGS.image_size]
    else:
        input_shape = [FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3]
    input_images = tf.placeholder(name='input', dtype=tf.float32, shape=input_shape)
    
    resnet50_config = resnet.model_architectures[FLAGS.model_name]
    network = resnet.ResnetModel(FLAGS.model_name, 
                                 FLAGS.num_classes,
                                 resnet50_config['layers'],
                                 resnet50_config['widths'],
                                 resnet50_config['expansions'],
                                 FLAGS.compute_format,
                                 FLAGS.input_format)
    probs, logits = network.build_model(
                input_images,
                training=False,
                reuse=False,
                use_final_conv=FLAGS.use_final_conv)
    
    if FLAGS.quantize:
        tf.contrib.quantize.experimental_create_eval_graph(symmetric=FLAGS.symmetric, 
                                                           use_qdq=FLAGS.use_qdq)

    # Define the saver and restore the checkpoint
    saver = tf.train.Saver()
    with tf.Session() as sess:
        if FLAGS.checkpoint:
            saver.restore(sess, FLAGS.checkpoint)
        else:
            sess.run(tf.global_variables_initializer())
        graph_def = graph.as_graph_def()
        frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, [probs.op.name])

    # Write out the frozen graph
    tf.io.write_graph(
        frozen_graph_def,
        os.path.dirname(FLAGS.output_file),
        os.path.basename(FLAGS.output_file),
        as_text=FLAGS.write_text_graphdef)
//  [python]
def init_op(self):
        self.init_op = tf.compat.v1.global_variables_initializer()
//  [python]
def init_op(self):
        self.init_op = tf.global_variables_initializer()
//  [python]
def execute(self, train_data, test_data):
        self.prepare_data(train_data, test_data)
        init = tf.initialize_all_variables()
        self.sess.run(init)
        for epoch in range(self.epochs):
            if self.verbose:
                print("Epoch: %04d;" % (epoch))
            self.train(train_data)
            if (epoch) % self.T == 0:
                print("Epoch: %04d; " % (epoch), end='')
                self.test(test_data)
//  [python]
def execute(self, train_data, test_data):
        self.prepare_data(train_data, test_data)
        init = tf.global_variables_initializer()
        self.sess.run(init)
        for epoch in range(self.epochs):
            if self.verbose:
                print("Epoch: %04d;" % (epoch))
            self.train(train_data)
            if (epoch) % self.T == 0:
                print("Epoch: %04d; " % (epoch), end='')
                self.test(test_data)
//  [python]
def evaluate(features):
    do_use = 0
    use = None
    sim_thres = 0
    # evaluate with USE

    if do_use == 1:
        cache_path = ''
        import tensorflow as tf
        import tensorflow_hub as hub
    
        class USE(object):
            def __init__(self, cache_path):
                super(USE, self).__init__()

                self.embed = hub.Module(cache_path)
                config = tf.ConfigProto()
                config.gpu_options.allow_growth = True
                self.sess = tf.Session()
                self.build_graph()
                self.sess.run([tf.global_variables_initializer(), tf.tables_initializer()])

            def build_graph(self):
                self.sts_input1 = tf.placeholder(tf.string, shape=(None))
                self.sts_input2 = tf.placeholder(tf.string, shape=(None))

                sts_encode1 = tf.nn.l2_normalize(self.embed(self.sts_input1), axis=1)
                sts_encode2 = tf.nn.l2_normalize(self.embed(self.sts_input2), axis=1)
                self.cosine_similarities = tf.reduce_sum(tf.multiply(sts_encode1, sts_encode2), axis=1)
                clip_cosine_similarities = tf.clip_by_value(self.cosine_similarities, -1.0, 1.0)
                self.sim_scores = 1.0 - tf.acos(clip_cosine_similarities)

            def semantic_sim(self, sents1, sents2):
                sents1 = [s.lower() for s in sents1]
                sents2 = [s.lower() for s in sents2]
                scores = self.sess.run(
                    [self.sim_scores],
                    feed_dict={
                        self.sts_input1: sents1,
                        self.sts_input2: sents2,
                    })
                return scores[0]

            use = USE(cache_path)


    acc = 0
    origin_success = 0
    total = 0
    total_q = 0
    total_change = 0
    total_word = 0
    for feat in features:
        if feat.success > 2:

            if do_use == 1:
                sim = float(use.semantic_sim([feat.seq], [feat.final_adverse]))
                if sim < sim_thres:
                    continue
            
            acc += 1
            total_q += feat.query
            total_change += feat.change
            total_word += len(feat.seq.split(' '))

            if feat.success == 3:
                origin_success += 1

        total += 1

    suc = float(acc / total)

    query = float(total_q / acc)
    change_rate = float(total_change / total_word)

    origin_acc = 1 - origin_success / total
    after_atk = 1 - suc

    print('acc/aft-atk-acc {:.6f}/ {:.6f}, query-num {:.4f}, change-rate {:.4f}'.format(origin_acc, after_atk, query, change_rate))
//  [python]
def test_get_predictions_with_feature_maps_of_dynamic_shape(
      self):
    image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64])
    conv_box_predictor = (
        box_predictor_builder.build_convolutional_keras_box_predictor(
            is_training=False,
            num_classes=0,
            conv_hyperparams=self._build_conv_hyperparams(),
            freeze_batchnorm=False,
            inplace_batchnorm_update=False,
            num_predictions_per_location_list=[5],
            min_depth=0,
            max_depth=32,
            num_layers_before_predictor=1,
            use_dropout=True,
            dropout_keep_prob=0.8,
            kernel_size=1,
            box_code_size=4
        ))
    box_predictions = conv_box_predictor([image_features])
    box_encodings = tf.concat(
        box_predictions[box_predictor.BOX_ENCODINGS], axis=1)
    objectness_predictions = tf.concat(
        box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND],
        axis=1)
    init_op = tf.initialize_all_variables()

    resolution = 32
    expected_num_anchors = resolution*resolution*5
    with self.test_session() as sess:
      sess.run(init_op)
      (box_encodings_shape,
       objectness_predictions_shape) = sess.run(
           [tf.shape(box_encodings), tf.shape(objectness_predictions)],
           feed_dict={image_features:
                      np.random.rand(4, resolution, resolution, 64)})
      actual_variable_set = set(
          [var.op.name for var in tf.trainable_variables()])
      self.assertAllEqual(box_encodings_shape, [4, expected_num_anchors, 1, 4])
      self.assertAllEqual(objectness_predictions_shape,
                          [4, expected_num_anchors, 1])
    expected_variable_set = set([
        'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/bias',
        'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/kernel',
        'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/bias',
        'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/kernel',
        'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/bias',
        'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/kernel'])
    self.assertEqual(expected_variable_set, actual_variable_set)
    self.assertEqual(conv_box_predictor._sorted_head_names,
                     ['box_encodings', 'class_predictions_with_background'])
//  [python]
def test_get_predictions_with_feature_maps_of_dynamic_shape(
      self):
    image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64])
    conv_box_predictor = (
        box_predictor_builder.build_convolutional_keras_box_predictor(
            is_training=False,
            num_classes=0,
            conv_hyperparams=self._build_conv_hyperparams(),
            freeze_batchnorm=False,
            inplace_batchnorm_update=False,
            num_predictions_per_location_list=[5],
            min_depth=0,
            max_depth=32,
            num_layers_before_predictor=1,
            use_dropout=True,
            dropout_keep_prob=0.8,
            kernel_size=1,
            box_code_size=4
        ))
    box_predictions = conv_box_predictor([image_features])
    box_encodings = tf.concat(
        box_predictions[box_predictor.BOX_ENCODINGS], axis=1)
    objectness_predictions = tf.concat(
        box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND],
        axis=1)
    init_op = tf.global_variables_initializer()

    resolution = 32
    expected_num_anchors = resolution*resolution*5
    with self.test_session() as sess:
      sess.run(init_op)
      (box_encodings_shape,
       objectness_predictions_shape) = sess.run(
           [tf.shape(box_encodings), tf.shape(objectness_predictions)],
           feed_dict={image_features:
                      np.random.rand(4, resolution, resolution, 64)})
      actual_variable_set = set(
          [var.op.name for var in tf.trainable_variables()])
      self.assertAllEqual(box_encodings_shape, [4, expected_num_anchors, 1, 4])
      self.assertAllEqual(objectness_predictions_shape,
                          [4, expected_num_anchors, 1])
    expected_variable_set = set([
        'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/bias',
        'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/kernel',
        'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/bias',
        'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/kernel',
        'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/bias',
        'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/kernel'])
    self.assertEqual(expected_variable_set, actual_variable_set)
    self.assertEqual(conv_box_predictor._sorted_head_names,
                     ['box_encodings', 'class_predictions_with_background'])
//  [python]
def get_scaffold(saver, tune_from, name):
    """ Get scaffold containing initializer operation for MonitoredTrainingSession.
    """
    if name == 'train':
        iterator_init = [
            tf.get_collection('TRAIN_ITERATOR'),
            tf.get_collection('VALID_ITERATOR')
        ]
    elif name == 'eval':
        iterator_init = [tf.get_collection('EVAL_ITERATOR')]
    else:
        raise NotImplementedError

    summaries = tf.get_collection(tf.GraphKeys.SUMMARIES)
    summary_op = tf.summary.merge([s for s in summaries if name in s.name])
    init_op = tf.group(
        tf.initialize_all_variables(),
        tf.local_variables_initializer(),
    )
    local_init_op = tf.group(tf.tables_initializer(),
                             tf.local_variables_initializer(), *iterator_init)
    scaffold = tf.train.Scaffold(saver=saver,
                                 init_op=init_op,
                                 local_init_op=local_init_op,
                                 summary_op=summary_op,
                                 init_fn=_get_init_pretrained(tune_from))
    return scaffold
//  [python]
def get_scaffold(saver, tune_from, name):
    """ Get scaffold containing initializer operation for MonitoredTrainingSession.
    """
    if name == 'train':
        iterator_init = [
            tf.get_collection('TRAIN_ITERATOR'),
            tf.get_collection('VALID_ITERATOR')
        ]
    elif name == 'eval':
        iterator_init = [tf.get_collection('EVAL_ITERATOR')]
    else:
        raise NotImplementedError

    summaries = tf.get_collection(tf.GraphKeys.SUMMARIES)
    summary_op = tf.summary.merge([s for s in summaries if name in s.name])
    init_op = tf.group(
        tf.global_variables_initializer(),
        tf.local_variables_initializer(),
    )
    local_init_op = tf.group(tf.tables_initializer(),
                             tf.local_variables_initializer(), *iterator_init)
    scaffold = tf.train.Scaffold(saver=saver,
                                 init_op=init_op,
                                 local_init_op=local_init_op,
                                 summary_op=summary_op,
                                 init_fn=_get_init_pretrained(tune_from))
    return scaffold
//  [python]
def train(model_path):
    n_samples = _train_x.shape[0]

    # tf Graph Input
    x = tf.placeholder(tf.float32, name="X")
    y = tf.placeholder(tf.float32, name="Y")

    # Set model weights
    w = tf.Variable(np.random.randn(), name="W")
    b = tf.Variable(np.random.randn(), name="b")

    pred = tf.add(tf.multiply(x, w), b)
    pred = tf.identity(pred, name="pred")
    cost = tf.reduce_sum(tf.pow(pred - y, 2)) / (2 * n_samples)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    saver = tf.train.Saver()

    # Launch the graph
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        # Fit all training data
        for _ in range(training_epochs):
            for (ix, iy) in zip(_train_x, _train_y):
                sess.run(optimizer, feed_dict={x: ix, y: iy})
        training_cost = sess.run(cost, feed_dict={x: _train_x, y: _train_y})
        testing_cost = sess.run(cost, feed_dict={x: _test_x, y: _test_y})
        print("train_cost={}, test_cost={}, diff={}"
              .format(training_cost, testing_cost, abs(training_cost - testing_cost)))

        p = os.path.abspath(os.path.join(model_path, "checkpoint"))
        os.makedirs(p, exist_ok=True)
        saver.save(sess, os.path.join(p, "model"))

        frozen_graph = freeze_session(sess, output_names=["pred:0"])
        p = os.path.abspath(os.path.join(model_path, "graphdef"))
        tf.train.write_graph(frozen_graph, p, "frozen.pb", as_text=False)

        p = os.path.abspath(os.path.join(model_path, "saved_model"))
        tf.saved_model.simple_save(sess, p, inputs={"X": x}, outputs={"pred": pred})
//  [python]
def train(model_path):
    n_samples = _train_x.shape[0]

    # tf Graph Input
    x = tf.placeholder(tf.float32, name="X")
    y = tf.placeholder(tf.float32, name="Y")

    # Set model weights
    w = tf.Variable(np.random.randn(), name="W")
    b = tf.Variable(np.random.randn(), name="b")

    pred = tf.add(tf.multiply(x, w), b)
    pred = tf.identity(pred, name="pred")
    cost = tf.reduce_sum(tf.pow(pred - y, 2)) / (2 * n_samples)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
    saver = tf.train.Saver()

    # Launch the graph
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        # Fit all training data
        for _ in range(training_epochs):
            for (ix, iy) in zip(_train_x, _train_y):
                sess.run(optimizer, feed_dict={x: ix, y: iy})
        training_cost = sess.run(cost, feed_dict={x: _train_x, y: _train_y})
        testing_cost = sess.run(cost, feed_dict={x: _test_x, y: _test_y})
        print("train_cost={}, test_cost={}, diff={}"
              .format(training_cost, testing_cost, abs(training_cost - testing_cost)))

        p = os.path.abspath(os.path.join(model_path, "checkpoint"))
        os.makedirs(p, exist_ok=True)
        saver.save(sess, os.path.join(p, "model"))

        frozen_graph = freeze_session(sess, output_names=["pred:0"])
        p = os.path.abspath(os.path.join(model_path, "graphdef"))
        tf.train.write_graph(frozen_graph, p, "frozen.pb", as_text=False)

        p = os.path.abspath(os.path.join(model_path, "saved_model"))
        tf.saved_model.simple_save(sess, p, inputs={"X": x}, outputs={"pred": pred})
//  [python]
def __init__(self, D, M):
    # represents a batch of training data
    self.X = tf.placeholder(tf.float32, shape=(None, D))

    # input -> hidden
    self.W = tf.Variable(tf.random_normal(shape=(D, M)) * np.sqrt(2.0 / M))
    self.b = tf.Variable(np.zeros(M).astype(np.float32))

    # hidden -> output
    self.V = tf.Variable(tf.random_normal(shape=(M, D)) * np.sqrt(2.0 / D))
    self.c = tf.Variable(np.zeros(D).astype(np.float32))

    # construct the reconstruction
    self.Z = tf.nn.relu(tf.matmul(self.X, self.W) + self.b)
    logits = tf.matmul(self.Z, self.V) + self.c
    self.X_hat = tf.nn.sigmoid(logits)

    # compute the cost
    self.cost = tf.reduce_sum(
      tf.nn.sigmoid_cross_entropy_with_logits(
        labels=self.X,
        logits=logits
      )
    )

    # make the trainer
    self.train_op = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(self.cost)

    # set up session and variables for later
    self.init_op = tf.initialize_all_variables()
    self.sess = tf.InteractiveSession()
    self.sess.run(self.init_op)
//  [python]
def __init__(self, D, M):
    # represents a batch of training data
    self.X = tf.placeholder(tf.float32, shape=(None, D))

    # input -> hidden
    self.W = tf.Variable(tf.random_normal(shape=(D, M)) * np.sqrt(2.0 / M))
    self.b = tf.Variable(np.zeros(M).astype(np.float32))

    # hidden -> output
    self.V = tf.Variable(tf.random_normal(shape=(M, D)) * np.sqrt(2.0 / D))
    self.c = tf.Variable(np.zeros(D).astype(np.float32))

    # construct the reconstruction
    self.Z = tf.nn.relu(tf.matmul(self.X, self.W) + self.b)
    logits = tf.matmul(self.Z, self.V) + self.c
    self.X_hat = tf.nn.sigmoid(logits)

    # compute the cost
    self.cost = tf.reduce_sum(
      tf.nn.sigmoid_cross_entropy_with_logits(
        labels=self.X,
        logits=logits
      )
    )

    # make the trainer
    self.train_op = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(self.cost)

    # set up session and variables for later
    self.init_op = tf.global_variables_initializer()
    self.sess = tf.InteractiveSession()
    self.sess.run(self.init_op)
//  [python]
def create_or_load_model(model, model_dir, session, name):
    """Create translation model and initialize or load parameters in session."""
    latest_ckpt = tf.train.latest_checkpoint(model_dir)
    if latest_ckpt:
        model = load_model(model, latest_ckpt, session, name)
    else:
        start_time = time.time()
        session.run(tf.initialize_all_variables())
        session.run(tf.tables_initializer())
        utils.print_out("  created %s model with fresh parameters, time %.2fs" % (name, time.time() - start_time))

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

    global_step = model.global_step.eval(session=session)
    return model, global_step
//  [python]
def main(_):
  # Create model.

  batch_size = 2
  image_size = 256

  vid_placeholder = tf.placeholder(tf.float32,
                                   (batch_size*FLAGS.num_frames, image_size, image_size, 3))  # pylint: disable=line-too-long
  object_placeholder = tf.placeholder(tf.float32,
                                      (batch_size*FLAGS.num_frames, image_size, image_size, FLAGS.num_object_classes))  # pylint: disable=line-too-long
  input_placeholder = (vid_placeholder, object_placeholder)

  # We are using the full_asnp50_structure, since we feed both video and object.
  FLAGS.model_structure = json.dumps(model_structures.full_asnp50_structure)  # pylint: disable=line-too-long
  FLAGS.model_edge_weights = json.dumps(model_structures.full_asnp_structure_weights)  # pylint: disable=line-too-long

  network = assemblenet_plus.assemblenet_plus(
      assemblenet_depth=50,
      num_classes=FLAGS.num_classes,
      data_format='channels_last')

  # The model function takes the inputs and is_training.
  outputs = network(input_placeholder, False)

  with tf.Session() as sess:
    # Generate a random video to run on.
    # This should be replaced by a real video.
    sess.run(tf.initialize_all_variables())
    vid = np.random.rand(*vid_placeholder.shape)
    obj = np.random.rand(*object_placeholder.shape)
    logits = sess.run(outputs, feed_dict={input_placeholder: (vid, obj)})
    print(logits)
    print(np.argmax(logits, axis=1))
//  [python]
def main(_):
  # Create model.

  batch_size = 2
  image_size = 256

  vid_placeholder = tf.placeholder(tf.float32,
                                   (batch_size*FLAGS.num_frames, image_size, image_size, 3))  # pylint: disable=line-too-long
  object_placeholder = tf.placeholder(tf.float32,
                                      (batch_size*FLAGS.num_frames, image_size, image_size, FLAGS.num_object_classes))  # pylint: disable=line-too-long
  input_placeholder = (vid_placeholder, object_placeholder)

  # We are using the full_asnp50_structure, since we feed both video and object.
  FLAGS.model_structure = json.dumps(model_structures.full_asnp50_structure)  # pylint: disable=line-too-long
  FLAGS.model_edge_weights = json.dumps(model_structures.full_asnp_structure_weights)  # pylint: disable=line-too-long

  network = assemblenet_plus.assemblenet_plus(
      assemblenet_depth=50,
      num_classes=FLAGS.num_classes,
      data_format='channels_last')

  # The model function takes the inputs and is_training.
  outputs = network(input_placeholder, False)

  with tf.Session() as sess:
    # Generate a random video to run on.
    # This should be replaced by a real video.
    sess.run(tf.global_variables_initializer())
    vid = np.random.rand(*vid_placeholder.shape)
    obj = np.random.rand(*object_placeholder.shape)
    logits = sess.run(outputs, feed_dict={input_placeholder: (vid, obj)})
    print(logits)
    print(np.argmax(logits, axis=1))
//  [python]
def testLocalTrainOp(self):
    g = tf.Graph()
    with g.as_default():
      tf.set_random_seed(0)
      tf_inputs = tf.constant(self._inputs, dtype=tf.float32)
      tf_labels = tf.constant(self._labels, dtype=tf.float32)

      model_fn = BatchNormClassifier
      model_args = (tf_inputs, tf_labels)
      deploy_config = model_deploy.DeploymentConfig(num_clones=2,
                                                    clone_on_cpu=True)

      optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0)

      self.assertEqual(slim.get_variables(), [])
      model = model_deploy.deploy(deploy_config, model_fn, model_args,
                                  optimizer=optimizer)

      update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
      self.assertEqual(len(update_ops), 4)
      self.assertEqual(len(model.clones), 2)
      self.assertEqual(model.total_loss.op.name, 'total_loss')
      self.assertEqual(model.summary_op.op.name, 'summary_op/summary_op')
      self.assertEqual(model.train_op.op.name, 'train_op')

      with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        moving_mean = tf.contrib.framework.get_variables_by_name(
            'moving_mean')[0]
        moving_variance = tf.contrib.framework.get_variables_by_name(
            'moving_variance')[0]
        initial_loss = sess.run(model.total_loss)
        initial_mean, initial_variance = sess.run([moving_mean,
                                                   moving_variance])
        self.assertAllClose(initial_mean, [0.0, 0.0, 0.0, 0.0])
        self.assertAllClose(initial_variance, [1.0, 1.0, 1.0, 1.0])
        for _ in range(10):
          sess.run(model.train_op)
        final_loss = sess.run(model.total_loss)
        self.assertLess(final_loss, initial_loss / 5.0)

        final_mean, final_variance = sess.run([moving_mean,
                                               moving_variance])
        expected_mean = np.array([0.125, 0.25, 0.375, 0.25])
        expected_var = np.array([0.109375, 0.1875, 0.234375, 0.1875])
        expected_var = self._addBesselsCorrection(16, expected_var)
        self.assertAllClose(final_mean, expected_mean)
        self.assertAllClose(final_variance, expected_var)
//  [python]
def testLocalTrainOp(self):
    g = tf.Graph()
    with g.as_default():
      tf.set_random_seed(0)
      tf_inputs = tf.constant(self._inputs, dtype=tf.float32)
      tf_labels = tf.constant(self._labels, dtype=tf.float32)

      model_fn = BatchNormClassifier
      model_args = (tf_inputs, tf_labels)
      deploy_config = model_deploy.DeploymentConfig(num_clones=2,
                                                    clone_on_cpu=True)

      optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0)

      self.assertEqual(slim.get_variables(), [])
      model = model_deploy.deploy(deploy_config, model_fn, model_args,
                                  optimizer=optimizer)

      update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
      self.assertEqual(len(update_ops), 4)
      self.assertEqual(len(model.clones), 2)
      self.assertEqual(model.total_loss.op.name, 'total_loss')
      self.assertEqual(model.summary_op.op.name, 'summary_op/summary_op')
      self.assertEqual(model.train_op.op.name, 'train_op')

      with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        moving_mean = tf.contrib.framework.get_variables_by_name(
            'moving_mean')[0]
        moving_variance = tf.contrib.framework.get_variables_by_name(
            'moving_variance')[0]
        initial_loss = sess.run(model.total_loss)
        initial_mean, initial_variance = sess.run([moving_mean,
                                                   moving_variance])
        self.assertAllClose(initial_mean, [0.0, 0.0, 0.0, 0.0])
        self.assertAllClose(initial_variance, [1.0, 1.0, 1.0, 1.0])
        for _ in range(10):
          sess.run(model.train_op)
        final_loss = sess.run(model.total_loss)
        self.assertLess(final_loss, initial_loss / 5.0)

        final_mean, final_variance = sess.run([moving_mean,
                                               moving_variance])
        expected_mean = np.array([0.125, 0.25, 0.375, 0.25])
        expected_var = np.array([0.109375, 0.1875, 0.234375, 0.1875])
        expected_var = self._addBesselsCorrection(16, expected_var)
        self.assertAllClose(final_mean, expected_mean)
        self.assertAllClose(final_variance, expected_var)
//  [python]
def predict():
    num_classes = 15
    learning_rate = 0.001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 10
    vocab_size = 10000
    embed_size = 100
    hidden_size = 100
    is_training = False
    story_length = 3
    dropout_keep_prob = 1
    model = DynamicMemoryNetwork(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                          story_length, vocab_size, embed_size, hidden_size, is_training,
                          multi_label_flag=False, block_size=20)
    ckpt_dir = 'checkpoint_dmn/dummy_test/'
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        for i in range(100):
            story = np.random.randn(batch_size, story_length, sequence_length)
            story[story > 0] = 1
            story[story <= 0] = 0
            query = np.random.randn(batch_size, sequence_length)  # [batch_size, sequence_length]
            query[query > 0] = 1
            query[query <= 0] = 0
            answer_single = np.sum(query, axis=1) + np.round(0.1 * np.sum(np.sum(story, axis=1),axis=1))  # [batch_size].e.g. np.array([1, 0, 1, 1, 1, 2, 1, 1])
            predict = sess.run([model.predictions], feed_dict={model.query: query, model.story: story,
                                                               model.dropout_keep_prob: dropout_keep_prob})
            print(i, "query:", query, "=====================>")
            print(i, "label:", answer_single, "prediction:", predict)
//  [python]
def predict():
    num_classes = 15
    learning_rate = 0.001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 10
    vocab_size = 10000
    embed_size = 100
    hidden_size = 100
    is_training = False
    story_length = 3
    dropout_keep_prob = 1
    model = DynamicMemoryNetwork(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                          story_length, vocab_size, embed_size, hidden_size, is_training,
                          multi_label_flag=False, block_size=20)
    ckpt_dir = 'checkpoint_dmn/dummy_test/'
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_dir))
        for i in range(100):
            story = np.random.randn(batch_size, story_length, sequence_length)
            story[story > 0] = 1
            story[story <= 0] = 0
            query = np.random.randn(batch_size, sequence_length)  # [batch_size, sequence_length]
            query[query > 0] = 1
            query[query <= 0] = 0
            answer_single = np.sum(query, axis=1) + np.round(0.1 * np.sum(np.sum(story, axis=1),axis=1))  # [batch_size].e.g. np.array([1, 0, 1, 1, 1, 2, 1, 1])
            predict = sess.run([model.predictions], feed_dict={model.query: query, model.story: story,
                                                               model.dropout_keep_prob: dropout_keep_prob})
            print(i, "query:", query, "=====================>")
            print(i, "label:", answer_single, "prediction:", predict)
//  [python]
def test_generator_run(self):
    tf.set_random_seed(1234)
    noise = tf.random_normal([100, 64])
    image, _ = dcgan.generator(noise)
    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      image.eval()
//  [python]
def test_generator_run(self):
    tf.set_random_seed(1234)
    noise = tf.random_normal([100, 64])
    image, _ = dcgan.generator(noise)
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      image.eval()
//  [python]
def __init__(
            self,
            n_a,
            n_s,
            lr=0.01,
            gamma=0.98,
            epsilon=0.95,
            replace_target_iter=300,
            memory_size=10000,
            batch_size=128,
            output_graph=False,
    ):
        self.n_a = n_a
        self.n_s = n_s
        self.lr = lr
        self.gamma = gamma
        self.epsilon = epsilon
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size

        # total learning step
        self.learn_step_counter = 0
        self.memory_counter = 0

        # initialize zero memory [s, a, r, s_]
        self.memory = np.zeros((self.memory_size, n_s * 2 + 2))
        self.tfs, self.tfa, self.tfr, self.tfs_, self.dyn_train, self.dqn_train, self.q, self.int_r = \
            self._build_nets()

        t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net')
        e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='eval_net')

        with tf.variable_scope('hard_replacement'):
            self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        self.sess = tf.Session()

        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)

        self.sess.run(tf.initialize_all_variables())
//  [python]
def __init__(
            self,
            n_a,
            n_s,
            lr=0.01,
            gamma=0.98,
            epsilon=0.95,
            replace_target_iter=300,
            memory_size=10000,
            batch_size=128,
            output_graph=False,
    ):
        self.n_a = n_a
        self.n_s = n_s
        self.lr = lr
        self.gamma = gamma
        self.epsilon = epsilon
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size

        # total learning step
        self.learn_step_counter = 0
        self.memory_counter = 0

        # initialize zero memory [s, a, r, s_]
        self.memory = np.zeros((self.memory_size, n_s * 2 + 2))
        self.tfs, self.tfa, self.tfr, self.tfs_, self.dyn_train, self.dqn_train, self.q, self.int_r = \
            self._build_nets()

        t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target_net')
        e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='eval_net')

        with tf.variable_scope('hard_replacement'):
            self.target_replace_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        self.sess = tf.Session()

        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)

        self.sess.run(tf.global_variables_initializer())
//  [python]
def test_discriminator_run(self):
    image = tf.random_uniform([5, 32, 32, 3], -1, 1)
    output, _ = dcgan.discriminator(image)
    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output.eval()
//  [python]
def test_discriminator_run(self):
    image = tf.random_uniform([5, 32, 32, 3], -1, 1)
    output, _ = dcgan.discriminator(image)
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output.eval()
//  [python]
def train():
    # 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 = 15
    learning_rate = 0.001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 10
    vocab_size = 10000
    embed_size = 100
    hidden_size = 100
    is_training = True
    story_length = 3
    dropout_keep_prob = 1
    model = DynamicMemoryNetwork(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                          story_length, vocab_size, embed_size, hidden_size, is_training,
                          multi_label_flag=False)
    ckpt_dir = 'checkpoint_dmn/dummy_test/'
    saver = tf.train.Saver()
    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]
            story = np.random.randn(batch_size, story_length, sequence_length)
            story[story > 0] = 1
            story[story <= 0] = 0
            query = np.random.randn(batch_size, sequence_length)  # [batch_size, sequence_length]
            query[query > 0] = 1
            query[query <= 0] = 0
            answer_single = np.sum(query, axis=1) + np.round(0.1 * np.sum(np.sum(story, axis=1),
                                                                          axis=1))  # [batch_size].e.g. np.array([1, 0, 1, 1, 1, 2, 1, 1])
            loss, acc, predict, _ = sess.run(
                [model.loss_val, model.accuracy, model.predictions, model.train_op],
                feed_dict={model.query: query, model.story: story, model.answer_single: answer_single,
                           model.dropout_keep_prob: dropout_keep_prob})
            print(i, "query:", query, "=====================>")
            print(i, "loss:", loss, "acc:", acc, "label:", answer_single, "prediction:", predict)
            if i % 300 == 0:
                save_path = ckpt_dir + "model.ckpt"
                saver.save(sess, save_path, global_step=i * 300)
//  [python]
def train():
    # 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 = 15
    learning_rate = 0.001
    batch_size = 8
    decay_steps = 1000
    decay_rate = 0.9
    sequence_length = 10
    vocab_size = 10000
    embed_size = 100
    hidden_size = 100
    is_training = True
    story_length = 3
    dropout_keep_prob = 1
    model = DynamicMemoryNetwork(num_classes, learning_rate, batch_size, decay_steps, decay_rate, sequence_length,
                          story_length, vocab_size, embed_size, hidden_size, is_training,
                          multi_label_flag=False)
    ckpt_dir = 'checkpoint_dmn/dummy_test/'
    saver = tf.train.Saver()
    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]
            story = np.random.randn(batch_size, story_length, sequence_length)
            story[story > 0] = 1
            story[story <= 0] = 0
            query = np.random.randn(batch_size, sequence_length)  # [batch_size, sequence_length]
            query[query > 0] = 1
            query[query <= 0] = 0
            answer_single = np.sum(query, axis=1) + np.round(0.1 * np.sum(np.sum(story, axis=1),
                                                                          axis=1))  # [batch_size].e.g. np.array([1, 0, 1, 1, 1, 2, 1, 1])
            loss, acc, predict, _ = sess.run(
                [model.loss_val, model.accuracy, model.predictions, model.train_op],
                feed_dict={model.query: query, model.story: story, model.answer_single: answer_single,
                           model.dropout_keep_prob: dropout_keep_prob})
            print(i, "query:", query, "=====================>")
            print(i, "loss:", loss, "acc:", acc, "label:", answer_single, "prediction:", predict)
            if i % 300 == 0:
                save_path = ckpt_dir + "model.ckpt"
                saver.save(sess, save_path, global_step=i * 300)
//  [python]
def build_model(self, sess):
        self.init_opt()

        sess.run(tf.initialize_all_variables())

        if len(self.model_path) > 0:
            print("Reading model parameters from %s" % self.model_path)
            restore_vars = tf.global_variables()
            # all_vars = tf.all_variables()
            # restore_vars = [var for var in all_vars if
            #                 var.name.startswith('g_') or
            #                 var.name.startswith('d_')]
            saver = tf.train.Saver(restore_vars)
            saver.restore(sess, self.model_path)

            istart = self.model_path.rfind('_') + 1
            iend = self.model_path.rfind('.')
            counter = self.model_path[istart:iend]
            counter = int(counter)
        else:
            print("Created model with fresh parameters.")
            counter = 0
        return counter
//  [python]
def build_model(self, sess):
        self.init_opt()

        sess.run(tf.global_variables_initializer())

        if len(self.model_path) > 0:
            print("Reading model parameters from %s" % self.model_path)
            restore_vars = tf.global_variables()
            # all_vars = tf.all_variables()
            # restore_vars = [var for var in all_vars if
            #                 var.name.startswith('g_') or
            #                 var.name.startswith('d_')]
            saver = tf.train.Saver(restore_vars)
            saver.restore(sess, self.model_path)

            istart = self.model_path.rfind('_') + 1
            iend = self.model_path.rfind('.')
            counter = self.model_path[istart:iend]
            counter = int(counter)
        else:
            print("Created model with fresh parameters.")
            counter = 0
        return counter
//  [python]
def testMissingBatchSize(self, out_channels, kernel_shape, padding,
                           use_bias, in_shape, out_shape, stride_shape):
    """Check functionality with unknown batch size at build time."""

    conv1 = snt.Conv1DTranspose(output_channels=out_channels,
                                output_shape=out_shape,
                                kernel_shape=kernel_shape,
                                padding=padding,
                                stride=stride_shape,
                                name="conv1",
                                use_bias=use_bias)

    # Pass in an image with its batch size set to `None`:
    image = tf.placeholder(tf.float32, shape=(None,) + in_shape[1:])
    output = conv1(image)
    self.assertTrue(output.get_shape().is_compatible_with(
        [None, out_shape, out_channels]))

    with self.test_session() as sess:
      tf.initialize_all_variables().run()
      sess.run(output, feed_dict={image: np.zeros((10,) + in_shape[1:])})
//  [python]
def testMissingBatchSize(self, out_channels, kernel_shape, padding,
                           use_bias, in_shape, out_shape, stride_shape):
    """Check functionality with unknown batch size at build time."""

    conv1 = snt.Conv1DTranspose(output_channels=out_channels,
                                output_shape=out_shape,
                                kernel_shape=kernel_shape,
                                padding=padding,
                                stride=stride_shape,
                                name="conv1",
                                use_bias=use_bias)

    # Pass in an image with its batch size set to `None`:
    image = tf.placeholder(tf.float32, shape=(None,) + in_shape[1:])
    output = conv1(image)
    self.assertTrue(output.get_shape().is_compatible_with(
        [None, out_shape, out_channels]))

    with self.test_session() as sess:
      tf.global_variables_initializer().run()
      sess.run(output, feed_dict={image: np.zeros((10,) + in_shape[1:])})
//  [python]
def testOutputShapeInteger(self, use_bias):
    """Tests if output shapes are valid when specified as an integer."""
    inputs = tf.zeros(shape=[3, 5, 5, 2], dtype=tf.float32)
    inputs_2 = tf.zeros(shape=[3, 5, 7, 2], dtype=tf.float32)

    conv1 = snt.Conv2DTranspose(name="conv2d_1",
                                output_channels=10,
                                output_shape=10,
                                kernel_shape=5,
                                padding=snt.SAME,
                                stride=2,
                                use_bias=use_bias)

    outputs = conv1(inputs)
    outputs_2 = conv1(inputs_2)

    self.assertTrue(outputs.get_shape().is_compatible_with((3, 10, 10, 10)))

    with self.test_session() as sess:
      tf.compat.v1.global_variables_initializer().run()
      sess.run(outputs)
      with self.assertRaises(tf.errors.InvalidArgumentError):
        sess.run(outputs_2)
//  [python]
def testOutputShapeInteger(self, use_bias):
    """Tests if output shapes are valid when specified as an integer."""
    inputs = tf.zeros(shape=[3, 5, 5, 2], dtype=tf.float32)
    inputs_2 = tf.zeros(shape=[3, 5, 7, 2], dtype=tf.float32)

    conv1 = snt.Conv2DTranspose(name="conv2d_1",
                                output_channels=10,
                                output_shape=10,
                                kernel_shape=5,
                                padding=snt.SAME,
                                stride=2,
                                use_bias=use_bias)

    outputs = conv1(inputs)
    outputs_2 = conv1(inputs_2)

    self.assertTrue(outputs.get_shape().is_compatible_with((3, 10, 10, 10)))

    with self.test_session() as sess:
      tf.global_variables_initializer().run()
      sess.run(outputs)
      with self.assertRaises(tf.errors.InvalidArgumentError):
        sess.run(outputs_2)
//  [python]
def testOutputShapeInteger(self, use_bias):
    """Tests if output shapes are valid when specified as an integer."""
    inputs = tf.zeros(shape=[3, 5, 5, 5, 2], dtype=tf.float32)
    inputs_2 = tf.zeros(shape=[3, 5, 7, 5, 2], dtype=tf.float32)

    conv1 = snt.Conv3DTranspose(name="conv3d_1",
                                output_channels=10,
                                output_shape=10,
                                kernel_shape=5,
                                padding=snt.SAME,
                                stride=2,
                                use_bias=use_bias)

    outputs = conv1(inputs)
    outputs_2 = conv1(inputs_2)

    self.assertTrue(outputs.get_shape().is_compatible_with((3, 10, 10, 10, 10)))

    with self.test_session() as sess:
      tf.compat.v1.global_variables_initializer().run()
      sess.run(outputs)
      with self.assertRaises(tf.errors.InvalidArgumentError):
        sess.run(outputs_2)
//  [python]
def testOutputShapeInteger(self, use_bias):
    """Tests if output shapes are valid when specified as an integer."""
    inputs = tf.zeros(shape=[3, 5, 5, 5, 2], dtype=tf.float32)
    inputs_2 = tf.zeros(shape=[3, 5, 7, 5, 2], dtype=tf.float32)

    conv1 = snt.Conv3DTranspose(name="conv3d_1",
                                output_channels=10,
                                output_shape=10,
                                kernel_shape=5,
                                padding=snt.SAME,
                                stride=2,
                                use_bias=use_bias)

    outputs = conv1(inputs)
    outputs_2 = conv1(inputs_2)

    self.assertTrue(outputs.get_shape().is_compatible_with((3, 10, 10, 10, 10)))

    with self.test_session() as sess:
      tf.global_variables_initializer().run()
      sess.run(outputs)
      with self.assertRaises(tf.errors.InvalidArgumentError):
        sess.run(outputs_2)