jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def train():
  image_path, mask_path, truth_path, label_path = deploy_seg.inputs()
  
  with tf.Graph().as_default():
    global_step = tf.train.get_or_create_global_step()

    image_batch, mask_batch, truth_batch, label_batch = deploy_seg.get_batch_pre(image_path, mask_path, truth_path, label_path)

    images_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 3),name='image_plh')
    masks_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 1),name='mask_plh')
    truths_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 3),name='truth_plh')
    label_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE),name='label_plh')

    pred_label, composite = deploy_seg.inference(images_placeholder,masks_placeholder)

    parse_com = deploy_seg._parse_eval_(composite)
    tf.summary.image('parsed_composite', parse_com, max_outputs=20)
    parse_label = deploy_seg._parse_label_(pred_label)
    tf.summary.image('parsed_label', parse_label, max_outputs=20)

    loss = deploy_seg.loss_pre(pred_label, composite, label_placeholder, truths_placeholder)
    train_op = deploy_seg.train(loss,global_step)
    init = tf.initialize_all_variables()
    summary = tf.summary.merge_all()
    saver = tf.train.Saver(max_to_keep=7)
    checkpoint_file = os.path.join(TRAIN_DIR, 'model.ckpt')
    with tf.Session() as sess:
        sess.run(init)
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess,coord = coord)
        writer_train = tf.summary.FileWriter(TRAIN_DIR, sess.graph)
        sess.graph.finalize()
        print("Initialized")
        start_time = time.time()
        for e in range(FLAGS.epoch):
          for step in range(STEP_PER_TRAIN_EPOCH):
            image_batch_v, mask_batch_v, truth_batch_v,label_batch_v = sess.run([image_batch, mask_batch, truth_batch, label_batch])
            feed_dict_t={
                    images_placeholder:image_batch_v,
                    masks_placeholder:mask_batch_v,
                    truths_placeholder:truth_batch_v,
                    label_placeholder:label_batch_v
                    }
            _, loss_value, train_pre = sess.run(
                [[pred_label, composite], loss, train_op], feed_dict=feed_dict_t)
            if((step+1) % 200 ==0):
              duration = time.time() - start_time
              print('Epoch %d step %d: total loss = %.7f (%.3f sec)' %(e, step+1, loss_value, duration))
              summary_str = sess.run(summary,feed_dict=feed_dict_t)
              writer_train.add_summary(summary_str,step)
              writer_train.flush()
          if((e+1) %3 == 0):
              saver.save(sess, checkpoint_file, global_step=global_step) 
        coord.request_stop()
        coord.join(threads)
//  [python]
def train():
  image_path, mask_path, truth_path, label_path = deploy_seg.inputs()
  
  with tf.Graph().as_default():
    global_step = tf.train.get_or_create_global_step()

    image_batch, mask_batch, truth_batch, label_batch = deploy_seg.get_batch_pre(image_path, mask_path, truth_path, label_path)

    images_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 3),name='image_plh')
    masks_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 1),name='mask_plh')
    truths_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 3),name='truth_plh')
    label_placeholder = tf.placeholder(tf.float32,
                                    shape=(FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE),name='label_plh')

    pred_label, composite = deploy_seg.inference(images_placeholder,masks_placeholder)

    parse_com = deploy_seg._parse_eval_(composite)
    tf.summary.image('parsed_composite', parse_com, max_outputs=20)
    parse_label = deploy_seg._parse_label_(pred_label)
    tf.summary.image('parsed_label', parse_label, max_outputs=20)

    loss = deploy_seg.loss_pre(pred_label, composite, label_placeholder, truths_placeholder)
    train_op = deploy_seg.train(loss,global_step)
    init = tf.global_variables_initializer()
    summary = tf.summary.merge_all()
    saver = tf.train.Saver(max_to_keep=7)
    checkpoint_file = os.path.join(TRAIN_DIR, 'model.ckpt')
    with tf.Session() as sess:
        sess.run(init)
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess,coord = coord)
        writer_train = tf.summary.FileWriter(TRAIN_DIR, sess.graph)
        sess.graph.finalize()
        print("Initialized")
        start_time = time.time()
        for e in range(FLAGS.epoch):
          for step in range(STEP_PER_TRAIN_EPOCH):
            image_batch_v, mask_batch_v, truth_batch_v,label_batch_v = sess.run([image_batch, mask_batch, truth_batch, label_batch])
            feed_dict_t={
                    images_placeholder:image_batch_v,
                    masks_placeholder:mask_batch_v,
                    truths_placeholder:truth_batch_v,
                    label_placeholder:label_batch_v
                    }
            _, loss_value, train_pre = sess.run(
                [[pred_label, composite], loss, train_op], feed_dict=feed_dict_t)
            if((step+1) % 200 ==0):
              duration = time.time() - start_time
              print('Epoch %d step %d: total loss = %.7f (%.3f sec)' %(e, step+1, loss_value, duration))
              summary_str = sess.run(summary,feed_dict=feed_dict_t)
              writer_train.add_summary(summary_str,step)
              writer_train.flush()
          if((e+1) %3 == 0):
              saver.save(sess, checkpoint_file, global_step=global_step) 
        coord.request_stop()
        coord.join(threads)
//  [python]
def testCenterLoss(self):
        batch_size = 16
        nrof_features = 2
        nrof_classes = 16
        alfa = 0.5
        
        with tf.Graph().as_default():
        
            features = tf.placeholder(tf.float32, shape=(batch_size, nrof_features), name='features')
            labels = tf.placeholder(tf.int32, shape=(batch_size,), name='labels')

            # Define center loss
            center_loss, centers = facenet.center_loss(features, labels, alfa, nrof_classes)
            
            label_to_center = np.array( [ 
                 [-3,-3],  [-3,-1],  [-3,1],  [-3,3],
                 [-1,-3],  [-1,-1],  [-1,1],  [-1,3],
                 [ 1,-3],  [ 1,-1],  [ 1,1],  [ 1,3],
                 [ 3,-3],  [ 3,-1],  [ 3,1],  [ 3,3] 
                 ])
                
            sess = tf.Session()
            with sess.as_default():
                sess.run(tf.initialize_all_variables())
                np.random.seed(seed=666)
                
                for _ in range(0,100):
                    # Create array of random labels
                    lbls = np.random.randint(low=0, high=nrof_classes, size=(batch_size,))
                    feats = create_features(label_to_center, batch_size, nrof_features, lbls)

                    center_loss_, centers_ = sess.run([center_loss, centers], feed_dict={features:feats, labels:lbls})
                    
                # After a large number of updates the estimated centers should be close to the true ones
                np.testing.assert_almost_equal(centers_, label_to_center, decimal=5, err_msg='Incorrect estimated centers')
                np.testing.assert_almost_equal(center_loss_, 0.0, decimal=5, err_msg='Incorrect center loss')
//  [python]
def testCenterLoss(self):
        batch_size = 16
        nrof_features = 2
        nrof_classes = 16
        alfa = 0.5
        
        with tf.Graph().as_default():
        
            features = tf.placeholder(tf.float32, shape=(batch_size, nrof_features), name='features')
            labels = tf.placeholder(tf.int32, shape=(batch_size,), name='labels')

            # Define center loss
            center_loss, centers = facenet.center_loss(features, labels, alfa, nrof_classes)
            
            label_to_center = np.array( [ 
                 [-3,-3],  [-3,-1],  [-3,1],  [-3,3],
                 [-1,-3],  [-1,-1],  [-1,1],  [-1,3],
                 [ 1,-3],  [ 1,-1],  [ 1,1],  [ 1,3],
                 [ 3,-3],  [ 3,-1],  [ 3,1],  [ 3,3] 
                 ])
                
            sess = tf.Session()
            with sess.as_default():
                sess.run(tf.global_variables_initializer())
                np.random.seed(seed=666)
                
                for _ in range(0,100):
                    # Create array of random labels
                    lbls = np.random.randint(low=0, high=nrof_classes, size=(batch_size,))
                    feats = create_features(label_to_center, batch_size, nrof_features, lbls)

                    center_loss_, centers_ = sess.run([center_loss, centers], feed_dict={features:feats, labels:lbls})
                    
                # After a large number of updates the estimated centers should be close to the true ones
                np.testing.assert_almost_equal(centers_, label_to_center, decimal=5, err_msg='Incorrect estimated centers')
                np.testing.assert_almost_equal(center_loss_, 0.0, decimal=5, err_msg='Incorrect center loss')
//  [python]
def testUnknownBatchSize(self):
    batch = 2
    height, width = 65, 65
    global_pool = True
    num_classes = 10
    inputs = create_test_input(None, height, width, 3)
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
      logits, _ = self._resnet_small(inputs, num_classes,
                                     global_pool=global_pool,
                                     spatial_squeeze=False,
                                     scope='resnet')
    self.assertTrue(logits.op.name.startswith('resnet/logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [None, 1, 1, num_classes])
    images = create_test_input(batch, height, width, 3)
    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEqual(output.shape, (batch, 1, 1, num_classes))
//  [python]
def testUnknownBatchSize(self):
    batch = 2
    height, width = 65, 65
    global_pool = True
    num_classes = 10
    inputs = create_test_input(None, height, width, 3)
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
      logits, _ = self._resnet_small(inputs, num_classes,
                                     global_pool=global_pool,
                                     spatial_squeeze=False,
                                     scope='resnet')
    self.assertTrue(logits.op.name.startswith('resnet/logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [None, 1, 1, num_classes])
    images = create_test_input(batch, height, width, 3)
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEqual(output.shape, (batch, 1, 1, num_classes))
//  [python]
def testFullyConvolutionalUnknownHeightWidth(self):
    batch = 2
    height, width = 65, 65
    global_pool = False
    inputs = create_test_input(batch, None, None, 3)
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
      output, _ = self._resnet_small(inputs, None,
                                     global_pool=global_pool)
    self.assertListEqual(output.get_shape().as_list(),
                         [batch, None, None, 32])
    images = create_test_input(batch, height, width, 3)
    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(output, {inputs: images.eval()})
      self.assertEqual(output.shape, (batch, 3, 3, 32))
//  [python]
def testFullyConvolutionalUnknownHeightWidth(self):
    batch = 2
    height, width = 65, 65
    global_pool = False
    inputs = create_test_input(batch, None, None, 3)
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
      output, _ = self._resnet_small(inputs, None,
                                     global_pool=global_pool)
    self.assertListEqual(output.get_shape().as_list(),
                         [batch, None, None, 32])
    images = create_test_input(batch, height, width, 3)
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(output, {inputs: images.eval()})
      self.assertEqual(output.shape, (batch, 3, 3, 32))
//  [python]
def initialize(self):
        """Initialises a TensorFlow session.

        This method gets only called once for one agent instance, but since the variables are shared it applies for all
        agent instance. If the session restored from what's saved to hard disk, it must not get called, otherwise it
        will overwrite the restored values.
        """
        self.tf_session.run(tf.initialize_all_variables())
//  [python]
def initialize(self):
        """Initialises a TensorFlow session.

        This method gets only called once for one agent instance, but since the variables are shared it applies for all
        agent instance. If the session restored from what's saved to hard disk, it must not get called, otherwise it
        will overwrite the restored values.
        """
        self.tf_session.run(tf.global_variables_initializer())
//  [python]
def Initialize(self,sess):
        self.sess = sess
        self.sess.run(tf.initialize_all_variables())
        self.load_model()
//  [python]
def Initialize(self,sess):
        self.sess = sess
        self.sess.run(tf.global_variables_initializer())
        self.load_model()
//  [python]
def load_model(self, model: str, model_path: str, max_seq_length: int):
        g = tf.Graph()
        with g.as_default():
            self.input_ids = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])
            self.input_masks = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])
            self.segment_ids = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])

            hub_module = hub.Module(model_path)
            bert_inputs = dict(
                input_ids=self.input_ids,
                input_mask=self.input_masks,
                segment_ids=self.segment_ids
            )

            self.bert_outputs = hub_module(bert_inputs, signature="tokens", as_dict=True)
            tokenization_info = hub_module(signature="tokenization_info", 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)
        vocab_file, do_lower_case = self.sess.run(
            [
                tokenization_info["vocab_file"],
                tokenization_info["do_lower_case"],
            ]
        )
        Embeddings.tokenizer = FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
        self.max_seq_length = max_seq_length
        self.model_name = model

        print("Model loaded Successfully !")
//  [python]
def load_model(self, model: str, model_path: str, max_seq_length: int):
        g = tf.Graph()
        with g.as_default():
            self.input_ids = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])
            self.input_masks = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])
            self.segment_ids = tf.placeholder(dtype=tf.int32, shape=[None, max_seq_length])

            hub_module = hub.Module(model_path)
            bert_inputs = dict(
                input_ids=self.input_ids,
                input_mask=self.input_masks,
                segment_ids=self.segment_ids
            )

            self.bert_outputs = hub_module(bert_inputs, signature="tokens", as_dict=True)
            tokenization_info = hub_module(signature="tokenization_info", 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)
        vocab_file, do_lower_case = self.sess.run(
            [
                tokenization_info["vocab_file"],
                tokenization_info["do_lower_case"],
            ]
        )
        Embeddings.tokenizer = FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
        self.max_seq_length = max_seq_length
        self.model_name = model

        print("Model loaded Successfully !")
//  [python]
def test_model_runs(self):
    """Tests if the model runs without crashing."""
    batch = _get_vertex_model_batch()
    pred_dist = self.model(batch, is_training=False)
    logits = pred_dist.logits
    with self.session() as sess:
      sess.run(tf.compat.v1.global_variables_initializer())
      vertices_flat = np.random.randint(
          2**_QUANTIZATION_BITS + 1,
          size=[_BATCH_SIZE, _NUM_INPUT_VERTS * 3 + 1])
      sess.run(logits, {batch['vertices_flat']: vertices_flat})
//  [python]
def test_model_runs(self):
    """Tests if the model runs without crashing."""
    batch = _get_vertex_model_batch()
    pred_dist = self.model(batch, is_training=False)
    logits = pred_dist.logits
    with self.session() as sess:
      sess.run(tf.global_variables_initializer())
      vertices_flat = np.random.randint(
          2**_QUANTIZATION_BITS + 1,
          size=[_BATCH_SIZE, _NUM_INPUT_VERTS * 3 + 1])
      sess.run(logits, {batch['vertices_flat']: vertices_flat})
//  [python]
def main(output_dir, summaries_every, num_steps):
  graph = tf.Graph()


  with graph.as_default():
    features = tf.placeholder(tf.float32, shape=[4, 2])
    labels = tf.placeholder(tf.int32, shape=[4])

    train_op, loss, gs, update_acc = make_graph(features, labels)
    init = tf.initialize_all_variables()
    init_local = tf.local_variables_initializer()
    summary_op = tf.summary.merge_all()


  writer = tf.summary.FileWriter(output_dir, graph=graph, flush_secs=1)

  with tf.Session(graph=graph) as sess:
    init.run()
    init_local.run()
    step = 0
    xy = np.array([
        [True, False],
        [True, True],
        [False, False],
        [False, True]
    ], dtype=np.float)
    y_ = np.array([True, False, False, True], dtype=np.int32)
    while step < num_steps:

      _, _, step, loss_value, summaries = sess.run(
          [train_op, update_acc, gs, loss, summary_op],
          feed_dict={features: xy, labels: y_}
      )
      if step % summaries_every == 0:
        writer.add_summary(summaries, global_step=step)
//  [python]
def main(output_dir, summaries_every, num_steps):
  graph = tf.Graph()


  with graph.as_default():
    features = tf.placeholder(tf.float32, shape=[4, 2])
    labels = tf.placeholder(tf.int32, shape=[4])

    train_op, loss, gs, update_acc = make_graph(features, labels)
    init = tf.global_variables_initializer()
    init_local = tf.local_variables_initializer()
    summary_op = tf.summary.merge_all()


  writer = tf.summary.FileWriter(output_dir, graph=graph, flush_secs=1)

  with tf.Session(graph=graph) as sess:
    init.run()
    init_local.run()
    step = 0
    xy = np.array([
        [True, False],
        [True, True],
        [False, False],
        [False, True]
    ], dtype=np.float)
    y_ = np.array([True, False, False, True], dtype=np.int32)
    while step < num_steps:

      _, _, step, loss_value, summaries = sess.run(
          [train_op, update_acc, gs, loss, summary_op],
          feed_dict={features: xy, labels: y_}
      )
      if step % summaries_every == 0:
        writer.add_summary(summaries, global_step=step)
//  [python]
def test_sample_outputs_range(self):
    """Tests if the model produces samples in the correct range."""
    context = {'class_label': tf.zeros((_BATCH_SIZE,), dtype=tf.int32)}
    sample_dict = self.model.sample(
        _BATCH_SIZE, max_sample_length=_MAX_SAMPLE_LENGTH_VERTS,
        context=context)
    with self.session() as sess:
      sess.run(tf.compat.v1.global_variables_initializer())
      sample_dict_np = sess.run(sample_dict)
      in_range = np.logical_and(
          0 <= sample_dict_np['vertices'],
          sample_dict_np['vertices'] <= 2**_QUANTIZATION_BITS).all()
      self.assertTrue(in_range)
//  [python]
def test_sample_outputs_range(self):
    """Tests if the model produces samples in the correct range."""
    context = {'class_label': tf.zeros((_BATCH_SIZE,), dtype=tf.int32)}
    sample_dict = self.model.sample(
        _BATCH_SIZE, max_sample_length=_MAX_SAMPLE_LENGTH_VERTS,
        context=context)
    with self.session() as sess:
      sess.run(tf.global_variables_initializer())
      sample_dict_np = sess.run(sample_dict)
      in_range = np.logical_and(
          0 <= sample_dict_np['vertices'],
          sample_dict_np['vertices'] <= 2**_QUANTIZATION_BITS).all()
      self.assertTrue(in_range)
//  [python]
def test_model_runs(self):
    """Tests if the model runs without crashing."""
    batch = _get_face_model_batch()
    pred_dist = self.model(batch, is_training=False)
    logits = pred_dist.logits
    with self.session() as sess:
      sess.run(tf.global_variables_initializer())
      vertices = np.random.rand(_BATCH_SIZE, _NUM_INPUT_VERTS, 3) - 0.5
      vertices_mask = np.ones([_BATCH_SIZE, _NUM_INPUT_VERTS])
      faces = np.random.randint(
          _NUM_INPUT_VERTS + 2, size=[_BATCH_SIZE, _NUM_INPUT_FACE_INDICES])
      sess.run(
          logits,
          {batch['vertices']: vertices,
           batch['vertices_mask']: vertices_mask,
           batch['faces']: faces}
          )
//  [python]
def train(train_file = "data/local_train_splitByUser",
        test_file = "data/local_test_splitByUser",
        uid_voc = "data/uid_voc.pkl",
        mid_voc = "data/mid_voc.pkl",
        cat_voc = "data/cat_voc.pkl",
        batch_size = 128,
        maxlen = 100,
        test_iter = 100,
        save_iter = 100,
        model_type = 'DNN',
	    seed = 2,
    ):

    model_path = "dnn_save_path/ckpt_noshuff" + model_type + str(seed)
    best_model_path = "dnn_bast_model/ckpt_noshuff" + model_type + str(seed)
    gpu_options = tf.GPUOptions(allow_growth=True)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        train_data = DataIterator(train_file,uid_voc,mid_voc,cat_voc,batch_size,maxlen)
        test_data = DataIterator(test_file,uid_voc,mid_voc,cat_voc,batch_size,maxlen)

        n_uid,n_mid,n_cat = train_data.get_n()
        model = Model_DIN_V2_Gru_Vec_attGru_Neg(n_uid,n_mid,n_cat,EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE)

        sess.run(tf.initialize_all_variables())
        sess.run(tf.local_variables_initializer())
        iter = 0
        lr = 0.001
        for itr in range(3):
            loss_sum = 0.0
            accuracy_sum = 0.0
            aux_loss_sum = 0.0

            for src,tgt in train_data:
                uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src,tgt,maxlen,return_neg=True)
                loss, acc, aux_loss = model.train(sess, [uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, lr,noclk_mids, noclk_cats])
                loss_sum += loss
                accuracy_sum += acc
                aux_loss_sum += aux_loss
                iter += 1
                if (iter % test_iter) == 0:
                    print('iter: %d ----> train_loss: %.8f ---- train_accuracy: %.4f ---- tran_aux_loss: %.4f' % \
                          (iter, loss_sum / test_iter, accuracy_sum / test_iter, aux_loss_sum / test_iter))
                    print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' %
                          eval(sess, test_data, model, best_model_path))
                    loss_sum = 0.0
                    accuracy_sum = 0.0
                    aux_loss_sum = 0.0
                if (iter % save_iter) == 0:
                    print('save model iter: %d' % (iter))
                    model.save(sess, model_path + "--" + str(iter))
            lr *= 0.5
//  [python]
def train(train_file = "data/local_train_splitByUser",
        test_file = "data/local_test_splitByUser",
        uid_voc = "data/uid_voc.pkl",
        mid_voc = "data/mid_voc.pkl",
        cat_voc = "data/cat_voc.pkl",
        batch_size = 128,
        maxlen = 100,
        test_iter = 100,
        save_iter = 100,
        model_type = 'DNN',
	    seed = 2,
    ):

    model_path = "dnn_save_path/ckpt_noshuff" + model_type + str(seed)
    best_model_path = "dnn_bast_model/ckpt_noshuff" + model_type + str(seed)
    gpu_options = tf.GPUOptions(allow_growth=True)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        train_data = DataIterator(train_file,uid_voc,mid_voc,cat_voc,batch_size,maxlen)
        test_data = DataIterator(test_file,uid_voc,mid_voc,cat_voc,batch_size,maxlen)

        n_uid,n_mid,n_cat = train_data.get_n()
        model = Model_DIN_V2_Gru_Vec_attGru_Neg(n_uid,n_mid,n_cat,EMBEDDING_DIM, HIDDEN_SIZE, ATTENTION_SIZE)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        iter = 0
        lr = 0.001
        for itr in range(3):
            loss_sum = 0.0
            accuracy_sum = 0.0
            aux_loss_sum = 0.0

            for src,tgt in train_data:
                uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, noclk_mids, noclk_cats = prepare_data(src,tgt,maxlen,return_neg=True)
                loss, acc, aux_loss = model.train(sess, [uids, mids, cats, mid_his, cat_his, mid_mask, target, sl, lr,noclk_mids, noclk_cats])
                loss_sum += loss
                accuracy_sum += acc
                aux_loss_sum += aux_loss
                iter += 1
                if (iter % test_iter) == 0:
                    print('iter: %d ----> train_loss: %.8f ---- train_accuracy: %.4f ---- tran_aux_loss: %.4f' % \
                          (iter, loss_sum / test_iter, accuracy_sum / test_iter, aux_loss_sum / test_iter))
                    print('test_auc: %.4f ----test_loss: %.4f ---- test_accuracy: %.4f ---- test_aux_loss: %.4f' %
                          eval(sess, test_data, model, best_model_path))
                    loss_sum = 0.0
                    accuracy_sum = 0.0
                    aux_loss_sum = 0.0
                if (iter % save_iter) == 0:
                    print('save model iter: %d' % (iter))
                    model.save(sess, model_path + "--" + str(iter))
            lr *= 0.5
//  [python]
def test_sample_outputs_range(self):
    """Tests if the model produces samples in the correct range."""
    context = _get_face_model_batch()
    del context['faces']
    sample_dict = self.model.sample(
        context, max_sample_length=_MAX_SAMPLE_LENGTH_FACES)
    with self.session() as sess:
      sess.run(tf.global_variables_initializer())
      # Pad the vertices in order to test that the face model only outputs
      # vertex indices in the unpadded range
      vertices = np.pad(
          np.random.rand(_BATCH_SIZE, _NUM_INPUT_VERTS, 3) - 0.5,
          [[0, 0], [0, _NUM_PAD_VERTS], [0, 0]], mode='constant')
      vertices_mask = np.pad(
          np.ones([_BATCH_SIZE, _NUM_INPUT_VERTS]),
          [[0, 0], [0, _NUM_PAD_VERTS]], mode='constant')
      sample_dict_np = sess.run(
          sample_dict,
          {context['vertices']: vertices,
           context['vertices_mask']: vertices_mask})
      in_range = np.logical_and(
          0 <= sample_dict_np['faces'],
          sample_dict_np['faces'] <= _NUM_INPUT_VERTS + 1).all()
      self.assertTrue(in_range)
//  [python]
def test():
    with tf.Session() as sess:
        result=test_position_wise_feed_forward_fn()
        sess.run(tf.initialize_all_variables())
        result_=sess.run(result)
        print("result_:",result_)
//  [python]
def test():
    with tf.Session() as sess:
        result=test_position_wise_feed_forward_fn()
        sess.run(tf.global_variables_initializer())
        result_=sess.run(result)
        print("result_:",result_)
//  [python]
def start_bundle(self):
    tf.compat.v1.disable_eager_execution()
    tf.reset_default_graph()
    self.module = hub.Module(_UNIVERSAL_SENTENCE_ENCODER_PATH)

    self.session = tf.Session()
    self.session.run(tf.compat.v1.global_variables_initializer())
    self.session.run(tf.tables_initializer())

    self.messages_placeholder = tf.placeholder(tf.string)
    self.feature_tensor = self.module(self.messages_placeholder)
//  [python]
def start_bundle(self):
    tf.compat.v1.disable_eager_execution()
    tf.reset_default_graph()
    self.module = hub.Module(_UNIVERSAL_SENTENCE_ENCODER_PATH)

    self.session = tf.Session()
    self.session.run(tf.global_variables_initializer())
    self.session.run(tf.tables_initializer())

    self.messages_placeholder = tf.placeholder(tf.string)
    self.feature_tensor = self.module(self.messages_placeholder)
//  [python]
def buildModel(self):
        y = tf.reduce_sum(tf.multiply(self.u_embedding, self.v_embedding), 1) \
            - tf.reduce_sum(tf.multiply(self.u_embedding, self.neg_item_embedding), 1)

        loss = -tf.reduce_sum(tf.log(tf.sigmoid(y))) + self.regU * (tf.nn.l2_loss(self.u_embedding) +
                                                                    tf.nn.l2_loss(self.v_embedding) +
                                                                    tf.nn.l2_loss(self.neg_item_embedding))
        opt = tf.train.AdamOptimizer(self.lRate)

        train = opt.minimize(loss)

        init = tf.initialize_all_variables()
        self.sess.run(init)
        for iteration in range(self.maxIter):
            for n, batch in enumerate(self.next_batch_pairwise()):
                user_idx, i_idx, j_idx = batch
                _, l = self.sess.run([train, loss],
                                feed_dict={self.u_idx: user_idx, self.neg_idx: j_idx, self.v_idx: i_idx})
                print('training:', iteration + 1, 'batch', n, 'loss:', l)
//  [python]
def buildModel(self):
        y = tf.reduce_sum(tf.multiply(self.u_embedding, self.v_embedding), 1) \
            - tf.reduce_sum(tf.multiply(self.u_embedding, self.neg_item_embedding), 1)

        loss = -tf.reduce_sum(tf.log(tf.sigmoid(y))) + self.regU * (tf.nn.l2_loss(self.u_embedding) +
                                                                    tf.nn.l2_loss(self.v_embedding) +
                                                                    tf.nn.l2_loss(self.neg_item_embedding))
        opt = tf.train.AdamOptimizer(self.lRate)

        train = opt.minimize(loss)

        init = tf.global_variables_initializer()
        self.sess.run(init)
        for iteration in range(self.maxIter):
            for n, batch in enumerate(self.next_batch_pairwise()):
                user_idx, i_idx, j_idx = batch
                _, l = self.sess.run([train, loss],
                                feed_dict={self.u_idx: user_idx, self.neg_idx: j_idx, self.v_idx: i_idx})
                print('training:', iteration + 1, 'batch', n, 'loss:', l)
//  [python]
def _init_session(self):
        # https://zhuanlan.zhihu.com/p/78998468
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        self.sess.run(tf.global_variables_initializer())
        self.sess.run(tf.tables_initializer())
        # saver = tf.train.Saver(max_to_keep=None)
        self.saver = tf.train.Saver()
//  [python]
def __init__(self, sess, model, data, config):
    self.model = model
    self.config = config
    self.sess = sess
    self.data = data
    self.init = tf.group(tf.global_variables_initializer(),
                         tf.local_variables_initializer())
    self.sess.run(self.init)

    # keep a record of the training result
    self.train_loss = []
    self.val_loss = []
    self.train_loss_ave_epoch = []
    self.val_loss_ave_epoch = []
    self.recons_loss_train = []
    self.recons_loss_val = []
    self.KL_loss_train = []
    self.KL_loss_val = []
    self.sample_std_dev_train = []
    self.sample_std_dev_val = []
    self.iter_epochs_list = []
    self.test_sigma2 = []
//  [python]
def main(*args):

    # Train model
    print('Training model...')
    mnist = input_data.read_data_sets(FLAGS.data_url, one_hot=True)
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),}
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'], name='x')
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.initialize_all_variables())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))

    tf.summary.scalar('cross_entropy', cross_entropy)

    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    tf.summary.scalar('accuracy', accuracy)
    merged = tf.summary.merge_all()
    test_writer = tf.summary.FileWriter(FLAGS.train_url, flush_secs=1)

    for step in range(FLAGS.max_steps):
      batch = mnist.train.next_batch(50)
      train_step.run(feed_dict={x: batch[0], y_: batch[1]})
      if step % 10 == 0:
        summary, acc = sess.run([merged, accuracy], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
        test_writer.add_summary(summary, step)
        print('training accuracy is:', acc)
    print('Done training!')

    builder = tf.saved_model.builder.SavedModelBuilder(os.path.join(FLAGS.train_url, 'model'))

    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'images': tensor_info_x},
            outputs={'scores': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
                prediction_signature,
        },
        main_op=tf.tables_initializer(),
        strip_default_attrs=True)

    builder.save()

    print('Done exporting!')
//  [python]
def main(*args):

    # Train model
    print('Training model...')
    mnist = input_data.read_data_sets(FLAGS.data_url, one_hot=True)
    sess = tf.InteractiveSession()
    serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
    feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),}
    tf_example = tf.parse_example(serialized_tf_example, feature_configs)
    x = tf.identity(tf_example['x'], name='x')
    y_ = tf.placeholder('float', shape=[None, 10])
    w = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer())
    y = tf.nn.softmax(tf.matmul(x, w) + b, name='y')
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y))

    tf.summary.scalar('cross_entropy', cross_entropy)

    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
    tf.summary.scalar('accuracy', accuracy)
    merged = tf.summary.merge_all()
    test_writer = tf.summary.FileWriter(FLAGS.train_url, flush_secs=1)

    for step in range(FLAGS.max_steps):
      batch = mnist.train.next_batch(50)
      train_step.run(feed_dict={x: batch[0], y_: batch[1]})
      if step % 10 == 0:
        summary, acc = sess.run([merged, accuracy], feed_dict={x: mnist.test.images, y_: mnist.test.labels})
        test_writer.add_summary(summary, step)
        print('training accuracy is:', acc)
    print('Done training!')

    builder = tf.saved_model.builder.SavedModelBuilder(os.path.join(FLAGS.train_url, 'model'))

    tensor_info_x = tf.saved_model.utils.build_tensor_info(x)
    tensor_info_y = tf.saved_model.utils.build_tensor_info(y)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'images': tensor_info_x},
            outputs={'scores': tensor_info_y},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'predict_images':
                prediction_signature,
        },
        main_op=tf.tables_initializer(),
        strip_default_attrs=True)

    builder.save()

    print('Done exporting!')
//  [python]
def testApply(self):
    # Use small batches for tests because the OSS version runs
    # in constrained GPU environment with 1-2GB of memory.
    batch_size = 8
    with tf.Graph().as_default():
      images = tf.placeholder(tf.float32, image_shape(None))
      model = resnet50.ResNet50(data_format())
      predictions = model(images, training=False)

      init = tf.initialize_all_variables()

      with tf.Session() as sess:
        sess.run(init)
        np_images, _ = random_batch(batch_size)
        out = sess.run(predictions, feed_dict={images: np_images})
        self.assertAllEqual([batch_size, 1000], out.shape)
//  [python]
def testApply(self):
    # Use small batches for tests because the OSS version runs
    # in constrained GPU environment with 1-2GB of memory.
    batch_size = 8
    with tf.Graph().as_default():
      images = tf.placeholder(tf.float32, image_shape(None))
      model = resnet50.ResNet50(data_format())
      predictions = model(images, training=False)

      init = tf.global_variables_initializer()

      with tf.Session() as sess:
        sess.run(init)
        np_images, _ = random_batch(batch_size)
        out = sess.run(predictions, feed_dict={images: np_images})
        self.assertAllEqual([batch_size, 1000], out.shape)
//  [python]
def train_model(self):
        init = tf.initialize_all_variables()
        sess = self.sess
        #sess = tf_debug.LocalCLIDebugWrapperSession(sess)
        #sess = self.restore_tf_model(1000)

        if self.epoch == 0:
            sess.run(init)
        while (1):
            self.epoch += 1
            epoch = self.epoch
            epoch_loss = 0.0
            epoch_reg_loss = 0.0
            epoch_cls_loss = 0.0
            for batch in self.train_data.next_train_batch(self.batch_size):
                batch['is_train'] = True
                feed_dict = {}
                for key in self.placeholders:
                    feed_dict[self.placeholders[key]] = batch[key]
                if self.epoch == 1:
                    opt_vars = [self.variables[var_name] for var_name in
                                ['total_loss', 'reg_loss', 'cls_loss', 'cls_loss']]
                else:
                    opt_vars = [self.variables[var_name] for var_name in
                                ['total_loss', 'reg_loss', 'cls_loss', 'train_step']]
                loss, reg_loss, cls_loss, _ = sess.run(opt_vars, feed_dict=feed_dict)
                #print "start clip"
                #_ = sess.run(self.variables['clip_op'])
                #print "end clip"
                # print loss
                epoch_loss += loss * batch['cnt']
                epoch_reg_loss += reg_loss * batch['cnt']
                epoch_cls_loss += cls_loss * batch['cnt']

            print(("epoch ", epoch, "loss: ", epoch_loss, epoch_reg_loss, epoch_cls_loss))
            if epoch !=1:

                #print "Dev data performance at epoch %d" % (epoch)
                results = self.core_evaluate(self.decode(self.dev_data)[0], self.dev_data)
                s_p,s_r,s_f = results[0]
                t_p,t_r,t_f = results[1]
                #print "Span result: ",s_p,s_r,s_f
                #print "Type Result: ",t_p,t_r,t_f
                #print
                #print "Test data performance at epoch %d" %(epoch)
                results = self.core_evaluate(self.decode(self.test_data)[0], self.test_data)
                s_p,s_r,s_f = results[0]
                t_p,t_r,t_f = results[1]
                #print "Span result: ",s_p,s_r,s_f
                #print "Type Result: ",t_p,t_r,t_f
                #print "---------------------"

                if epoch % 10 == 0:
                    self.save_tf_model()
            sys.stdout.flush()
//  [python]
def train_model(self):
        init = tf.global_variables_initializer()
        sess = self.sess
        #sess = tf_debug.LocalCLIDebugWrapperSession(sess)
        #sess = self.restore_tf_model(1000)

        if self.epoch == 0:
            sess.run(init)
        while (1):
            self.epoch += 1
            epoch = self.epoch
            epoch_loss = 0.0
            epoch_reg_loss = 0.0
            epoch_cls_loss = 0.0
            for batch in self.train_data.next_train_batch(self.batch_size):
                batch['is_train'] = True
                feed_dict = {}
                for key in self.placeholders:
                    feed_dict[self.placeholders[key]] = batch[key]
                if self.epoch == 1:
                    opt_vars = [self.variables[var_name] for var_name in
                                ['total_loss', 'reg_loss', 'cls_loss', 'cls_loss']]
                else:
                    opt_vars = [self.variables[var_name] for var_name in
                                ['total_loss', 'reg_loss', 'cls_loss', 'train_step']]
                loss, reg_loss, cls_loss, _ = sess.run(opt_vars, feed_dict=feed_dict)
                #print "start clip"
                #_ = sess.run(self.variables['clip_op'])
                #print "end clip"
                # print loss
                epoch_loss += loss * batch['cnt']
                epoch_reg_loss += reg_loss * batch['cnt']
                epoch_cls_loss += cls_loss * batch['cnt']

            print(("epoch ", epoch, "loss: ", epoch_loss, epoch_reg_loss, epoch_cls_loss))
            if epoch !=1:

                #print "Dev data performance at epoch %d" % (epoch)
                results = self.core_evaluate(self.decode(self.dev_data)[0], self.dev_data)
                s_p,s_r,s_f = results[0]
                t_p,t_r,t_f = results[1]
                #print "Span result: ",s_p,s_r,s_f
                #print "Type Result: ",t_p,t_r,t_f
                #print
                #print "Test data performance at epoch %d" %(epoch)
                results = self.core_evaluate(self.decode(self.test_data)[0], self.test_data)
                s_p,s_r,s_f = results[0]
                t_p,t_r,t_f = results[1]
                #print "Span result: ",s_p,s_r,s_f
                #print "Type Result: ",t_p,t_r,t_f
                #print "---------------------"

                if epoch % 10 == 0:
                    self.save_tf_model()
            sys.stdout.flush()
//  [python]
def benchmark_graph_apply(self):
    with tf.Graph().as_default():
      images = tf.placeholder(tf.float32, image_shape(None))
      model = resnet50.ResNet50(data_format())
      predictions = model(images, training=False)

      init = tf.global_variables_initializer()

      batch_size = 64
      with tf.Session() as sess:
        sess.run(init)
        np_images, _ = random_batch(batch_size)
        num_burn, num_iters = (3, 30)
        for _ in range(num_burn):
          sess.run(predictions, feed_dict={images: np_images})
        start = time.time()
        for _ in range(num_iters):
          # Comparison with the eager execution benchmark in resnet50_test.py
          # isn't entirely fair as the time here includes the cost of copying
          # the feeds from CPU memory to GPU.
          sess.run(predictions, feed_dict={images: np_images})
        self._report('apply', start, num_iters, batch_size)
//  [python]
def initialize_model(self) -> None:
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        self.sess.run(init_op)
//  [python]
def benchmark_graph_train(self):
    for batch_size in [16, 32, 64]:
      with tf.Graph().as_default():
        np_images, np_labels = random_batch(batch_size)
        dataset = tf.data.Dataset.from_tensors((np_images, np_labels)).repeat()
        images, labels = tf.compat.v1.data.make_one_shot_iterator(
            dataset).get_next()

        model = resnet50.ResNet50(data_format())
        logits = model(images, training=True)
        loss = tf.losses.softmax_cross_entropy(
            logits=logits, onehot_labels=labels)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0)
        train_op = optimizer.minimize(loss)

        init = tf.compat.v1.global_variables_initializer()
        with tf.Session() as sess:
          sess.run(init)
          (num_burn, num_iters) = (5, 10)
          for _ in range(num_burn):
            sess.run(train_op)
          start = time.time()
          for _ in range(num_iters):
            sess.run(train_op)
          self._report('train', start, num_iters, batch_size)
//  [python]
def benchmark_graph_train(self):
    for batch_size in [16, 32, 64]:
      with tf.Graph().as_default():
        np_images, np_labels = random_batch(batch_size)
        dataset = tf.data.Dataset.from_tensors((np_images, np_labels)).repeat()
        images, labels = tf.compat.v1.data.make_one_shot_iterator(
            dataset).get_next()

        model = resnet50.ResNet50(data_format())
        logits = model(images, training=True)
        loss = tf.losses.softmax_cross_entropy(
            logits=logits, onehot_labels=labels)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0)
        train_op = optimizer.minimize(loss)

        init = tf.global_variables_initializer()
        with tf.Session() as sess:
          sess.run(init)
          (num_burn, num_iters) = (5, 10)
          for _ in range(num_burn):
            sess.run(train_op)
          start = time.time()
          for _ in range(num_iters):
            sess.run(train_op)
          self._report('train', start, num_iters, batch_size)
//  [python]
def testTrainWithSummary(self):
    with tf.Graph().as_default():
      images = tf.placeholder(tf.float32, image_shape(None), name='images')
      labels = tf.placeholder(tf.float32, [None, 1000], name='labels')

      tf.train.get_or_create_global_step()
      logdir = tempfile.mkdtemp()
      with tf.contrib.summary.always_record_summaries():
        with tf.contrib.summary.create_file_writer(
            logdir, max_queue=0,
            name='t0').as_default():
          model = resnet50.ResNet50(data_format())
          logits = model(images, training=True)
          loss = tf.losses.softmax_cross_entropy(
              logits=logits, onehot_labels=labels)
          tf.contrib.summary.scalar(name='loss', tensor=loss)
          optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
          train_op = optimizer.minimize(loss)

      init = tf.global_variables_initializer()
      self.assertEqual(321, len(tf.global_variables()))

      # Use small batches for tests because the OSS version runs
      # in constrained GPU environment with 1-2GB of memory.
      batch_size = 2
      with tf.Session() as sess:
        sess.run(init)
        sess.run(tf.contrib.summary.summary_writer_initializer_op())
        np_images, np_labels = random_batch(batch_size)
        sess.run([train_op, tf.contrib.summary.all_summary_ops()],
                 feed_dict={images: np_images, labels: np_labels})

      events = summary_test_util.events_from_logdir(logdir)
      self.assertEqual(len(events), 2)
      self.assertEqual(events[1].summary.value[0].tag, 'loss')
//  [python]
def initialize_global_variables(sess=None):
    """Excute ``sess.run(tf.global_variables_initializer())`` for TF12+ or
    sess.run(tf.initialize_all_variables()) for TF11.

    Parameters
    ----------
    sess : a Session
    """
    assert sess is not None
    # try:    # TF12+
    sess.run(tf.initialize_all_variables())