jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
train_op = tf.train.FtrlOptimizer(learning_rate).minimize(cost)
train_x,train_y,test_x,test_y = get_data()
time_s=time.time()
result = []
with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for epoch in range(0, 10000):
        f_dict = {x: train_x, y: train_y}

        _, cost_, predict_ = sess.run([train_op, cost, pred], feed_dict=f_dict)

        auc = roc_auc_score(train_y, predict_)
        time_t = time.time()
        if epoch % 100 == 0:
            f_dict = {x: test_x, y: test_y}
            _, cost_, predict_test = sess.run([train_op, cost, pred], feed_dict=f_dict)
            test_auc = roc_auc_score(test_y, predict_test)
            print("%d %ld cost:%f,train_auc:%f,test_auc:%f" % (epoch, (time_t - time_s), cost_, auc, test_auc))
            result.append([epoch,(time_t - time_s),auc,test_auc])
//  [python]
train_op = tf.train.FtrlOptimizer(learning_rate).minimize(cost)
train_x,train_y,test_x,test_y = get_data()
time_s=time.time()
result = []
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(0, 10000):
        f_dict = {x: train_x, y: train_y}

        _, cost_, predict_ = sess.run([train_op, cost, pred], feed_dict=f_dict)

        auc = roc_auc_score(train_y, predict_)
        time_t = time.time()
        if epoch % 100 == 0:
            f_dict = {x: test_x, y: test_y}
            _, cost_, predict_test = sess.run([train_op, cost, pred], feed_dict=f_dict)
            test_auc = roc_auc_score(test_y, predict_test)
            print("%d %ld cost:%f,train_auc:%f,test_auc:%f" % (epoch, (time_t - time_s), cost_, auc, test_auc))
            result.append([epoch,(time_t - time_s),auc,test_auc])
//  [python]
def initialize_model(self) -> None:
        with self.sess.graph.as_default():
            init_op = tf.group(tf.global_variables_initializer(),
                               tf.local_variables_initializer())
            self.sess.run(init_op)
//  [python]
def run_sess(classifier,datasets,filename,load_saver=''):
    
    if len(datasets) ==4:
        train_X, train_Y, test_X, test_Y = datasets    
    elif len(datasets) ==3:
        train_X, train_Y, test_X, = datasets
        test_Y = None
    else:
        train_X, train_Y = datasets
        test_X = None
        test_Y = None
        
    sess = tf.Session()
    sess.run(tf.initialize_all_variables()) # 初始化变量
    
    if classifier.tbd: summ = Summaries(filename,sess=sess)
    else: summ=None
    
    classifier.train_model(train_X = train_X, 
                           train_Y = train_Y, 
                           test_X = test_X, 
                           test_Y = test_Y,
                           sess=sess,
                           summ=summ,
                           load_saver=load_saver)
    
    classifier.show_and_save_result(filename)
    
    if classifier.tbd: 
        summ.train_writer.close()
        
    sess.close()
//  [python]
def run_sess(classifier,datasets,filename,load_saver=''):
    
    if len(datasets) ==4:
        train_X, train_Y, test_X, test_Y = datasets    
    elif len(datasets) ==3:
        train_X, train_Y, test_X, = datasets
        test_Y = None
    else:
        train_X, train_Y = datasets
        test_X = None
        test_Y = None
        
    sess = tf.Session()
    sess.run(tf.global_variables_initializer()) # 初始化变量
    
    if classifier.tbd: summ = Summaries(filename,sess=sess)
    else: summ=None
    
    classifier.train_model(train_X = train_X, 
                           train_Y = train_Y, 
                           test_X = test_X, 
                           test_Y = test_Y,
                           sess=sess,
                           summ=summ,
                           load_saver=load_saver)
    
    classifier.show_and_save_result(filename)
    
    if classifier.tbd: 
        summ.train_writer.close()
        
    sess.close()
//  [python]
def restore(self, session):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
    vars_to_restore = [v for v in tf.global_variables() ]
    saver = tf.train.Saver(vars_to_restore)
    checkpoint_path = os.path.join(self.config["log_dir"], "model.max.ckpt")
    print("Restoring from {}".format(checkpoint_path))
    session.run(tf.initialize_all_variables())
    saver.restore(session, checkpoint_path)
//  [python]
def restore(self, session):
    # Don't try to restore unused variables from the TF-Hub ELMo module.
    vars_to_restore = [v for v in tf.global_variables() ]
    saver = tf.train.Saver(vars_to_restore)
    checkpoint_path = os.path.join(self.config["log_dir"], "model.max.ckpt")
    print("Restoring from {}".format(checkpoint_path))
    session.run(tf.global_variables_initializer())
    saver.restore(session, checkpoint_path)
//  [python]
def test_top_k_mask(self):
        with self.test_session() as sess:
            a = tf.constant([9, 8, 0, 6, 7, 1])
            d = wmlt.top_k_mask(a, 4)
            sess.run(tf.compat.v1.global_variables_initializer())
            out = sess.run(d)
            self.assertAllEqual(out,[ True, True,False, True, True,False])
//  [python]
def test_top_k_mask(self):
        with self.test_session() as sess:
            a = tf.constant([9, 8, 0, 6, 7, 1])
            d = wmlt.top_k_mask(a, 4)
            sess.run(tf.global_variables_initializer())
            out = sess.run(d)
            self.assertAllEqual(out,[ True, True,False, True, True,False])
//  [python]
def test_top_k_mask_nd(self):
        with self.test_session() as sess:
            a = tf.constant([[[9, 2, 3, 4, 19], [5, 6, 7, 8, 55], [11, 0, 9, 8, 0]]])
            d = wmlt.top_k_mask(a, 2)
            sess.run(tf.compat.v1.global_variables_initializer())
            out = sess.run(d)
            self.assertAllEqual(out,[[[ True,False,False,False, True],
                                     [False, False, False,  True,  True],
                                     [ True, False,  True, False, False]]])
//  [python]
def test_top_k_mask_nd(self):
        with self.test_session() as sess:
            a = tf.constant([[[9, 2, 3, 4, 19], [5, 6, 7, 8, 55], [11, 0, 9, 8, 0]]])
            d = wmlt.top_k_mask(a, 2)
            sess.run(tf.global_variables_initializer())
            out = sess.run(d)
            self.assertAllEqual(out,[[[ True,False,False,False, True],
                                     [False, False, False,  True,  True],
                                     [ True, False,  True, False, False]]])
//  [python]
def main(params):
    # Import data
    mnist = download_mnist_retry(params['data_dir'])

    # Create the model
    # Build the graph for the deep net
    mnist_network = MnistNetwork(params)
    mnist_network.build_network()
    print('build network done.')

    # Write log
    graph_location = tempfile.mkdtemp()
    #print('Saving graph to: %s' % graph_location)
    train_writer = tf.summary.FileWriter(graph_location)
    train_writer.add_graph(tf.get_default_graph())

    test_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for i in range(params['batch_num']):
            batch = mnist.train.next_batch(params['batch_size'])
            mnist_network.train_step.run(feed_dict={mnist_network.x: batch[0], mnist_network.y: batch[1]})

            if i % 100 == 0:
                train_accuracy = mnist_network.accuracy.eval(feed_dict={
                    mnist_network.x: batch[0], mnist_network.y: batch[1]})
                print('step %d, training accuracy %g' % (i, train_accuracy))

        test_acc = mnist_network.accuracy.eval(feed_dict={
            mnist_network.x: mnist.test.images, mnist_network.y: mnist.test.labels})

        nni.report_final_result(test_acc)
//  [python]
def main(params):
    # Import data
    mnist = download_mnist_retry(params['data_dir'])

    # Create the model
    # Build the graph for the deep net
    mnist_network = MnistNetwork(params)
    mnist_network.build_network()
    print('build network done.')

    # Write log
    graph_location = tempfile.mkdtemp()
    #print('Saving graph to: %s' % graph_location)
    train_writer = tf.summary.FileWriter(graph_location)
    train_writer.add_graph(tf.get_default_graph())

    test_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(params['batch_num']):
            batch = mnist.train.next_batch(params['batch_size'])
            mnist_network.train_step.run(feed_dict={mnist_network.x: batch[0], mnist_network.y: batch[1]})

            if i % 100 == 0:
                train_accuracy = mnist_network.accuracy.eval(feed_dict={
                    mnist_network.x: batch[0], mnist_network.y: batch[1]})
                print('step %d, training accuracy %g' % (i, train_accuracy))

        test_acc = mnist_network.accuracy.eval(feed_dict={
            mnist_network.x: mnist.test.images, mnist_network.y: mnist.test.labels})

        nni.report_final_result(test_acc)
//  [python]
def test():
    #below is a function test; if you use this for text classifiction, you need to transform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes=5
    learning_rate=0.001
    batch_size=8
    decay_steps=1000
    decay_rate=0.95
    sequence_length=5
    vocab_size=10000
    embed_size=100
    is_training=True
    dropout_keep_prob=1.0 #0.5
    filter_sizes=[2,3,4]
    num_filters=128
    multi_label_flag=True
    textRNN=TextCNN(filter_sizes,num_filters,num_classes, learning_rate, batch_size, decay_steps, decay_rate,sequence_length,vocab_size,embed_size,is_training,multi_label_flag=multi_label_flag)
    with tf.Session() as sess:
       sess.run(tf.initialize_all_variables())
       for i in range(500):
           input_x=np.random.randn(batch_size,sequence_length) #[None, self.sequence_length]
           input_x[input_x>=0]=1
           input_x[input_x <0] = 0
           input_y_multilabel=get_label_y(input_x)
           loss,possibility,W_projection_value,_=sess.run([textRNN.loss_val,textRNN.possibility,textRNN.W_projection,textRNN.train_op],
                                                    feed_dict={textRNN.input_x:input_x,textRNN.input_y_multilabel:input_y_multilabel,textRNN.dropout_keep_prob:dropout_keep_prob})
           print(i,"loss:",loss,"-------------------------------------------------------")
           print("label:",input_y_multilabel);print("possibility:",possibility)
//  [python]
def test():
    #below is a function test; if you use this for text classifiction, you need to transform sentence to indices of vocabulary first. then feed data to the graph.
    num_classes=5
    learning_rate=0.001
    batch_size=8
    decay_steps=1000
    decay_rate=0.95
    sequence_length=5
    vocab_size=10000
    embed_size=100
    is_training=True
    dropout_keep_prob=1.0 #0.5
    filter_sizes=[2,3,4]
    num_filters=128
    multi_label_flag=True
    textRNN=TextCNN(filter_sizes,num_filters,num_classes, learning_rate, batch_size, decay_steps, decay_rate,sequence_length,vocab_size,embed_size,is_training,multi_label_flag=multi_label_flag)
    with tf.Session() as sess:
       sess.run(tf.global_variables_initializer())
       for i in range(500):
           input_x=np.random.randn(batch_size,sequence_length) #[None, self.sequence_length]
           input_x[input_x>=0]=1
           input_x[input_x <0] = 0
           input_y_multilabel=get_label_y(input_x)
           loss,possibility,W_projection_value,_=sess.run([textRNN.loss_val,textRNN.possibility,textRNN.W_projection,textRNN.train_op],
                                                    feed_dict={textRNN.input_x:input_x,textRNN.input_y_multilabel:input_y_multilabel,textRNN.dropout_keep_prob:dropout_keep_prob})
           print(i,"loss:",loss,"-------------------------------------------------------")
           print("label:",input_y_multilabel);print("possibility:",possibility)
//  [python]
def build_model(self):
        # Input placeholder
        self.s = tf.placeholder(
            tf.float32, [None, self.env.observation_space.shape[0]])
        self.a = tf.placeholder(tf.int32, [None, 1])
        self.G = tf.placeholder(tf.float32, [None])
        # Construct model
        self.acts_prob = tf.layers.dense(
            inputs=self.s,
            units=self.env.action_space.n,    # output units
            activation=tf.nn.softmax,   # get action probabilities
            kernel_initializer=tf.random_normal_initializer(
                0., 0.0001),  # weights
            bias_initializer=tf.constant_initializer(0.0001),  # biases
            name='acts_prob'
        )
        # cost = log(pi(s_t, a_t))G_t
        log_prob = tf.squeeze(tf.log(tf.batch_gather(self.acts_prob, self.a)))
        self.cost = tf.reduce_mean(tf.multiply(self.G, log_prob))
        tf.summary.scalar('Log prob return', self.cost)
        self.optimizer = tf.train.AdamOptimizer(self.lr).minimize(-self.cost)
        self.summary = tf.summary.merge_all()
        self.sess.run(tf.initialize_all_variables())
        self.train_writer = tf.summary.FileWriter(self.config.resource.graph_dir,
                                                  self.sess.graph)
//  [python]
def build_model(self):
        # Input placeholder
        self.s = tf.placeholder(
            tf.float32, [None, self.env.observation_space.shape[0]])
        self.a = tf.placeholder(tf.int32, [None, 1])
        self.G = tf.placeholder(tf.float32, [None])
        # Construct model
        self.acts_prob = tf.layers.dense(
            inputs=self.s,
            units=self.env.action_space.n,    # output units
            activation=tf.nn.softmax,   # get action probabilities
            kernel_initializer=tf.random_normal_initializer(
                0., 0.0001),  # weights
            bias_initializer=tf.constant_initializer(0.0001),  # biases
            name='acts_prob'
        )
        # cost = log(pi(s_t, a_t))G_t
        log_prob = tf.squeeze(tf.log(tf.batch_gather(self.acts_prob, self.a)))
        self.cost = tf.reduce_mean(tf.multiply(self.G, log_prob))
        tf.summary.scalar('Log prob return', self.cost)
        self.optimizer = tf.train.AdamOptimizer(self.lr).minimize(-self.cost)
        self.summary = tf.summary.merge_all()
        self.sess.run(tf.global_variables_initializer())
        self.train_writer = tf.summary.FileWriter(self.config.resource.graph_dir,
                                                  self.sess.graph)
//  [python]
def train(self, data_source, cfg, stage=1, ckpt=None, restore_ckpt=None, num_gpus=1):

        batch_size = num_gpus * cfg.TRAIN.BATCH[stage-1]
        max_steps = cfg.TRAIN.ITERS[stage-1]
        self.training_steps = max_steps

        print ("batch size: %d \t max steps: %d"%(batch_size, max_steps))
        if isinstance(data_source, str):
            self.dl = DataLayer(data_source, batch_size=batch_size)
        else:
            self.dl = DBDataLayer(data_source, batch_size=batch_size)

        if stage == 1:
            self.build_train_graph_stage1(cfg, num_gpus=num_gpus)

        elif stage == 2:
            self.build_train_graph_stage2(cfg, num_gpus=num_gpus)


        self.summary_op = tf.summary.merge_all()

        saver = tf.train.Saver([var for var in tf.model_variables()], max_to_keep=10)
        train_writer = tf.summary.FileWriter(cfg.LOG_DIR+'_stage_%s'%str(stage))

        init_op = tf.group(tf.global_variables_initializer(),
                   tf.local_variables_initializer())

        SUMMARY_FREQ = 10
        LOG_FREQ = 100
        CHECKPOINT_FREQ = 5000

        with tf.Session() as sess:
            sess.run(init_op)

            # train with tfrecords
            if isinstance(self.dl, DataLayer):
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord=coord)

            # train with python data loader
            elif isinstance(self.dl, DBDataLayer):
                self.dl.init(sess)

            kwargs = {}

            if stage >= 2:
                motion_vars = tf.get_collection(tf.GraphKeys.MODEL_VARIABLES, scope="motion")
                motion_saver = tf.train.Saver(motion_vars)

                if ckpt is not None:
                    motion_saver.restore(sess, ckpt)

                if restore_ckpt is not None:
                    saver.restore(sess, restore_ckpt)

            running_loss = 0.0
            for step in range(1, max_steps):

                kwargs = {}
                fetches = {}
                fetches['train_op'] = self.train_op
                fetches['loss'] = self.total_loss
                if self.write_op is not None:
                    fetches['write_op'] = self.write_op

                if step % SUMMARY_FREQ == 0:
                    fetches['summary'] = self.summary_op

                result = sess.run(fetches, **kwargs)

                if step % SUMMARY_FREQ == 0:
                    train_writer.add_summary(result['summary'], step)

                if step % LOG_FREQ == 0:
                    print('[stage=%d, %5d] loss: %.3f'%(stage, step, running_loss / LOG_FREQ))
                    running_loss = 0.0

                if step % CHECKPOINT_FREQ == 0:
                    checkpoint_file = os.path.join(cfg.CHECKPOINT_DIR, '_stage_%s.ckpt'%str(stage))
                    saver.save(sess, checkpoint_file, step)

                running_loss += result['loss']

            checkpoint_file = os.path.join(cfg.CHECKPOINT_DIR, '_stage_%s.ckpt'%str(stage))
            saver.save(sess, checkpoint_file)

        return checkpoint_file
//  [python]
def elmo_strings(batches, filename, batchsize=32):
    """
    Compute and save vector embeddings of lists of strings in batches
    Parameters
    ----------
        batches : iterable of strings to be embedded
        filename : str
            filename to store embeddings            
        (optional)
        batchsize : int
            size of batches
    """
    g = tf.Graph()
    with g.as_default():
        module = hub.Module(ELMO_URL, **ELMO_MODULE_KWARGS)
        text_input = tf.placeholder(dtype=tf.string, shape=[None])
        embeddings = module(text_input, **ELMO_KWARGS)
        init_op = tf.group([tf.global_variables_initializer(),
                            tf.tables_initializer()])
    g.finalize()

    with tf.Session(graph=g) as sess:
        sess.run(init_op)

        for i, batch in enumerate(batches):
            # grab mean-pooling of contextualized word reps
            logger.info("Computing/saving batch {}".format(i))
            with open(filename, 'ab') as fout:
                pickle.dump(sess.run(
                    embeddings, feed_dict={text_input: batch}
                )[ELMO_DICTKEY], fout)
//  [python]
def build_graph(self):
    """Build the graph for the full model."""
    opts = self._options
    # The training data. A text file.
    (words, counts, words_per_epoch, self._epoch, self._words, examples,
     labels) = word2vec.skipgram_word2vec(filename=opts.train_data,
                                          batch_size=opts.batch_size,
                                          window_size=opts.window_size,
                                          min_count=opts.min_count,
                                          subsample=opts.subsample)
    (opts.vocab_words, opts.vocab_counts,
     opts.words_per_epoch) = self._session.run([words, counts, words_per_epoch])
    opts.vocab_size = len(opts.vocab_words)
    print("Data file: ", opts.train_data)
    print("Vocab size: ", opts.vocab_size - 1, " + UNK")
    print("Words per epoch: ", opts.words_per_epoch)
    self._examples = examples
    self._labels = labels
    self._id2word = opts.vocab_words
    for i, w in enumerate(self._id2word):
      self._word2id[w] = i
    true_logits, sampled_logits = self.forward(examples, labels)
    loss = self.nce_loss(true_logits, sampled_logits)
    tf.summary.scalar("NCE loss", loss)
    self._loss = loss
    self.optimize(loss)

    # Properly initialize all variables.
    tf.initialize_all_variables().run()

    self.saver = tf.train.Saver()
//  [python]
def build_graph(self):
    """Build the graph for the full model."""
    opts = self._options
    # The training data. A text file.
    (words, counts, words_per_epoch, self._epoch, self._words, examples,
     labels) = word2vec.skipgram_word2vec(filename=opts.train_data,
                                          batch_size=opts.batch_size,
                                          window_size=opts.window_size,
                                          min_count=opts.min_count,
                                          subsample=opts.subsample)
    (opts.vocab_words, opts.vocab_counts,
     opts.words_per_epoch) = self._session.run([words, counts, words_per_epoch])
    opts.vocab_size = len(opts.vocab_words)
    print("Data file: ", opts.train_data)
    print("Vocab size: ", opts.vocab_size - 1, " + UNK")
    print("Words per epoch: ", opts.words_per_epoch)
    self._examples = examples
    self._labels = labels
    self._id2word = opts.vocab_words
    for i, w in enumerate(self._id2word):
      self._word2id[w] = i
    true_logits, sampled_logits = self.forward(examples, labels)
    loss = self.nce_loss(true_logits, sampled_logits)
    tf.summary.scalar("NCE loss", loss)
    self._loss = loss
    self.optimize(loss)

    # Properly initialize all variables.
    tf.global_variables_initializer().run()

    self.saver = tf.train.Saver()
//  [python]
def universal_sentence_encoder_lite(batches, filename, spm_path, batchsize=32):
    """
    Compute and save vector embeddings of lists of strings in batches
    Parameters
    ----------
        batches : iterable of strings to be embedded
        filename : str
            filename to store embeddings            
        spm_path : str
            path to sentencepiece model from `get_sentence_piece_model`
        (optional)
        batchsize : int
            size of batches
    """
    sp = spm.SentencePieceProcessor()
    sp.Load(spm_path)

    g = tf.Graph()
    with g.as_default():
        module = hub.Module(UNIV_SENTENCE_LITE)
        input_placeholder = tf.sparse_placeholder(
            tf.int64, shape=(None, None)
        )
        embeddings = module(
            inputs=dict(
                values=input_placeholder.values, indices=input_placeholder.indices,
                dense_shape=input_placeholder.dense_shape
            )
        )
        init_op = tf.group([tf.initialize_all_variables(),
                            tf.tables_initializer()])
    g.finalize()

    with tf.Session(graph=g) as sess:
        sess.run(init_op)
        for i, batch in enumerate(batches):
            values, indices, dense_shape = process_to_IDs_in_sparse_format(sp, batch)
            logger.info("Computing/saving batch {}".format(i))
            emb = sess.run(
                embeddings, 
                feed_dict={
                    input_placeholder.values: values, 
                    input_placeholder.indices: indices, 
                    input_placeholder.dense_shape: dense_shape
                }
            )
            with open(filename, 'ab') as fout:
                    pickle.dump(emb, fout)
//  [python]
def universal_sentence_encoder_lite(batches, filename, spm_path, batchsize=32):
    """
    Compute and save vector embeddings of lists of strings in batches
    Parameters
    ----------
        batches : iterable of strings to be embedded
        filename : str
            filename to store embeddings            
        spm_path : str
            path to sentencepiece model from `get_sentence_piece_model`
        (optional)
        batchsize : int
            size of batches
    """
    sp = spm.SentencePieceProcessor()
    sp.Load(spm_path)

    g = tf.Graph()
    with g.as_default():
        module = hub.Module(UNIV_SENTENCE_LITE)
        input_placeholder = tf.sparse_placeholder(
            tf.int64, shape=(None, None)
        )
        embeddings = module(
            inputs=dict(
                values=input_placeholder.values, indices=input_placeholder.indices,
                dense_shape=input_placeholder.dense_shape
            )
        )
        init_op = tf.group([tf.global_variables_initializer(),
                            tf.tables_initializer()])
    g.finalize()

    with tf.Session(graph=g) as sess:
        sess.run(init_op)
        for i, batch in enumerate(batches):
            values, indices, dense_shape = process_to_IDs_in_sparse_format(sp, batch)
            logger.info("Computing/saving batch {}".format(i))
            emb = sess.run(
                embeddings, 
                feed_dict={
                    input_placeholder.values: values, 
                    input_placeholder.indices: indices, 
                    input_placeholder.dense_shape: dense_shape
                }
            )
            with open(filename, 'ab') as fout:
                    pickle.dump(emb, fout)
//  [python]
def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.001,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=200,
            memory_size=500,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
            dueling=True,
            sess=None,
    ):
        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        self.dueling = dueling      # decide to use dueling DQN or not

        self.learn_step_counter = 0
        self.memory = np.zeros((self.memory_size, n_features*2+2))
        self._build_net()
        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')
        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        if sess is None:
            self.sess = tf.Session()
            self.sess.run(tf.initialize_all_variables())
        else:
            self.sess = sess
        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)
        self.cost_his = []
//  [python]
def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.001,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=200,
            memory_size=500,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
            dueling=True,
            sess=None,
    ):
        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        self.dueling = dueling      # decide to use dueling DQN or not

        self.learn_step_counter = 0
        self.memory = np.zeros((self.memory_size, n_features*2+2))
        self._build_net()
        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')
        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        if sess is None:
            self.sess = tf.Session()
            self.sess.run(tf.global_variables_initializer())
        else:
            self.sess = sess
        if output_graph:
            tf.summary.FileWriter("logs/", self.sess.graph)
        self.cost_his = []
//  [python]
def testGlobalPoolUnknownImageShape(self):
    batch_size = 1
    height, width = 330, 400
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
      logits, end_points = inception.inception_resnet_v2(
          inputs, num_classes, create_aux_logits=False)
      self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [batch_size, num_classes])
      pre_pool = end_points['Conv2d_7b_1x1']
      images = tf.random_uniform((batch_size, height, width, 3))
      sess.run(tf.initialize_all_variables())
      logits_out, pre_pool_out = sess.run([logits, pre_pool],
                                          {inputs: images.eval()})
      self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
      self.assertTupleEqual(pre_pool_out.shape, (batch_size, 8, 11, 1536))
//  [python]
def testGlobalPoolUnknownImageShape(self):
    batch_size = 1
    height, width = 330, 400
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
      logits, end_points = inception.inception_resnet_v2(
          inputs, num_classes, create_aux_logits=False)
      self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [batch_size, num_classes])
      pre_pool = end_points['Conv2d_7b_1x1']
      images = tf.random_uniform((batch_size, height, width, 3))
      sess.run(tf.global_variables_initializer())
      logits_out, pre_pool_out = sess.run([logits, pre_pool],
                                          {inputs: images.eval()})
      self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
      self.assertTupleEqual(pre_pool_out.shape, (batch_size, 8, 11, 1536))
//  [python]
def step_train(train_op,loss,accuracy,
               train_images_batch,train_labels_batch,train_nums,train_log_step,
               val_images_batch,val_labels_batch,val_nums,val_log_step,
               snapshot_prefix,snapshot):
    '''
    循环迭代训练过程
    :param train_op: 训练op
    :param loss:     loss函数
    :param accuracy: 准确率函数
    :param train_images_batch: 训练images数据
    :param train_labels_batch: 训练labels数据
    :param train_nums:         总训练数据
    :param train_log_step:   训练log显示间隔
    :param val_images_batch: 验证images数据
    :param val_labels_batch: 验证labels数据
    :param val_nums:         总验证数据
    :param val_log_step:     验证log显示间隔
    :param snapshot_prefix: 模型保存的路径
    :param snapshot:        模型保存间隔
    :return: None
    '''
    saver = tf.train.Saver(max_to_keep=5)
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_input_images, batch_input_labels = sess.run([train_images_batch, train_labels_batch])
            _, train_loss = sess.run([train_op, loss], feed_dict={input_images: batch_input_images,
                                                                  input_labels: batch_input_labels,
                                                                  keep_prob: 0.8, is_training: True})
            # train测试(这里仅测试训练集的一个batch)
            if i % train_log_step == 0:
                train_acc = sess.run(accuracy, feed_dict={input_images: batch_input_images,
                                                          input_labels: batch_input_labels,
                                                          keep_prob: 1.0, is_training: False})
                print("%s: Step [%d]  train Loss : %f, training accuracy :  %g" % (
                datetime.now(), i, train_loss, train_acc))

            # val测试(测试全部val数据)
            if i % val_log_step == 0:
                mean_loss, mean_acc = net_evaluation(sess, loss, accuracy, val_images_batch, val_labels_batch, val_nums)
                print("%s: Step [%d]  val Loss : %f, val accuracy :  %g" % (datetime.now(), i, mean_loss, mean_acc))

            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # 保存val准确率最高的模型
            if mean_acc > max_acc and mean_acc > 0.7:
                max_acc = mean_acc
                path = os.path.dirname(snapshot_prefix)
                best_models = os.path.join(path, 'best_models_{}_{:.4f}.ckpt'.format(i, max_acc))
                print('------save:{}'.format(best_models))
                saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
//  [python]
def testEvaluation(self):
    batch_size = 2
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      eval_inputs = tf.random_uniform((batch_size, height, width, 3))
      logits, _ = inception.inception_resnet_v2(eval_inputs,
                                                num_classes,
                                                is_training=False)
      predictions = tf.argmax(logits, 1)
      sess.run(tf.initialize_all_variables())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
//  [python]
def testEvaluation(self):
    batch_size = 2
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      eval_inputs = tf.random_uniform((batch_size, height, width, 3))
      logits, _ = inception.inception_resnet_v2(eval_inputs,
                                                num_classes,
                                                is_training=False)
      predictions = tf.argmax(logits, 1)
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
//  [python]
def testUnknownBatchSize(self):
    batch_size = 1
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.placeholder(tf.float32, (None, height, width, 3))
      logits, _ = inception.inception_resnet_v2(inputs, num_classes)
      self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [None, num_classes])
      images = tf.random_uniform((batch_size, height, width, 3))
      sess.run(tf.initialize_all_variables())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))
//  [python]
def testUnknownBatchSize(self):
    batch_size = 1
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.placeholder(tf.float32, (None, height, width, 3))
      logits, _ = inception.inception_resnet_v2(inputs, num_classes)
      self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [None, num_classes])
      images = tf.random_uniform((batch_size, height, width, 3))
      sess.run(tf.global_variables_initializer())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))
//  [python]
def train(self, learning_rate, save_path, resume_path=None):
        model_x = tf.placeholder(dtype=tf.float32, shape=(None, 19, 19, 21))
        model_y = tf.placeholder(dtype=tf.float32, shape=(None, 1, 1, 1))
        is_training = tf.placeholder(dtype=tf.bool)

        out_logits, out = self.inference(model_x, is_training=is_training)
        loss = self.loss(labels=model_y, predictions=out)

        all_trainable = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
        kernels = [var for var in all_trainable if 'kernel' in var.name]
        lossL2 = tf.reduce_mean([tf.reduce_mean(tf.square(v)) for v in kernels]) * 1e-3

        all_loss = loss + lossL2

        train_op = tf.train.AdamOptimizer(learning_rate).minimize(all_loss)
        loader = tf.train.Saver()
        saver = tf.train.Saver(max_to_keep=20)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with tf.Session(config=config) as sess:
            # sess.run(tf.variables_initializer(tf.global_variables()))
            if resume_path is None:
                sess.run(tf.initialize_all_variables())
            else:
                print('resume:', resume_path)
                sess.run(tf.initialize_all_variables())
                loader.restore(sess, resume_path)
            step = 0
            epoch = self.train_data.epoch
            while True:
                step += 1
                x, y = self.train_data.next_batch()
                if len(x) == 0:
                    continue
                x, y = ValueNet.pre_process(x, y)
                feed_dict = {model_x: x, model_y: y, is_training: True}
                out_, _, loss_, l2_ = sess.run([out, train_op, loss, lossL2], feed_dict=feed_dict)
                if step % 100 == 0:
                    for oo, yy in zip(out_, y)[:10]:
                        print(oo, yy)
                print('step:', step, 'loss:', loss_, l2_, 'epoch:', epoch)
                if self.train_data.epoch > epoch:
                    epoch = self.train_data.epoch
                    print('save:')
                    saver.save(sess, save_path, global_step=epoch)
//  [python]
def train(self, learning_rate, save_path, resume_path=None):
        model_x = tf.placeholder(dtype=tf.float32, shape=(None, 19, 19, 21))
        model_y = tf.placeholder(dtype=tf.float32, shape=(None, 1, 1, 1))
        is_training = tf.placeholder(dtype=tf.bool)

        out_logits, out = self.inference(model_x, is_training=is_training)
        loss = self.loss(labels=model_y, predictions=out)

        all_trainable = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
        kernels = [var for var in all_trainable if 'kernel' in var.name]
        lossL2 = tf.reduce_mean([tf.reduce_mean(tf.square(v)) for v in kernels]) * 1e-3

        all_loss = loss + lossL2

        train_op = tf.train.AdamOptimizer(learning_rate).minimize(all_loss)
        loader = tf.train.Saver()
        saver = tf.train.Saver(max_to_keep=20)

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with tf.Session(config=config) as sess:
            # sess.run(tf.variables_initializer(tf.global_variables()))
            if resume_path is None:
                sess.run(tf.global_variables_initializer())
            else:
                print('resume:', resume_path)
                sess.run(tf.global_variables_initializer())
                loader.restore(sess, resume_path)
            step = 0
            epoch = self.train_data.epoch
            while True:
                step += 1
                x, y = self.train_data.next_batch()
                if len(x) == 0:
                    continue
                x, y = ValueNet.pre_process(x, y)
                feed_dict = {model_x: x, model_y: y, is_training: True}
                out_, _, loss_, l2_ = sess.run([out, train_op, loss, lossL2], feed_dict=feed_dict)
                if step % 100 == 0:
                    for oo, yy in zip(out_, y)[:10]:
                        print(oo, yy)
                print('step:', step, 'loss:', loss_, l2_, 'epoch:', epoch)
                if self.train_data.epoch > epoch:
                    epoch = self.train_data.epoch
                    print('save:')
                    saver.save(sess, save_path, global_step=epoch)
//  [python]
def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 150, 150
    num_classes = 1000
    with self.test_session() as sess:
      train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
      inception.inception_resnet_v2(train_inputs, num_classes)
      eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
      logits, _ = inception.inception_resnet_v2(eval_inputs,
                                                num_classes,
                                                is_training=False,
                                                reuse=True)
      predictions = tf.argmax(logits, 1)
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
//  [python]
def build(self, mode):
    """Construct the cifar model."""
    assert mode in ['train', 'eval']
    self.mode = mode
    self._setup_misc(mode)
    self._setup_images_and_labels()
    self._build_graph(self.images, self.labels, mode)

    self.init = tf.group(tf.global_variables_initializer(),
                         tf.local_variables_initializer())
//  [python]
def _test_pipeline(self, mode, params=None):
    """Helper function to test the full model pipeline.
    """
    # Create source and target example
    source_len = self.sequence_length + 5
    target_len = self.sequence_length + 10
    source = " ".join(np.random.choice(self.vocab_list, source_len))
    target = " ".join(np.random.choice(self.vocab_list, target_len))
    sources_file, targets_file = test_utils.create_temp_parallel_data(
        sources=[source], targets=[target])

    # Build model graph
    model = self.create_model(mode, params)
    input_pipeline_ = input_pipeline.ParallelTextInputPipeline(
        params={
            "source_files": [sources_file.name],
            "target_files": [targets_file.name]
        },
        mode=mode)
    input_fn = training_utils.create_input_fn(
        pipeline=input_pipeline_, batch_size=self.batch_size)
    features, labels = input_fn()
    fetches = model(features, labels, None)
    fetches = [_ for _ in fetches if _ is not None]

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      sess.run(tf.local_variables_initializer())
      sess.run(tf.tables_initializer())
      with tf.contrib.slim.queues.QueueRunners(sess):
        fetches_ = sess.run(fetches)

    sources_file.close()
    targets_file.close()

    return model, fetches_
//  [python]
def _test_pipeline(self, mode, params=None):
    """Helper function to test the full model pipeline.
    """
    # Create source and target example
    source_len = self.sequence_length + 5
    target_len = self.sequence_length + 10
    source = " ".join(np.random.choice(self.vocab_list, source_len))
    target = " ".join(np.random.choice(self.vocab_list, target_len))
    sources_file, targets_file = test_utils.create_temp_parallel_data(
        sources=[source], targets=[target])

    # Build model graph
    model = self.create_model(mode, params)
    input_pipeline_ = input_pipeline.ParallelTextInputPipeline(
        params={
            "source_files": [sources_file.name],
            "target_files": [targets_file.name]
        },
        mode=mode)
    input_fn = training_utils.create_input_fn(
        pipeline=input_pipeline_, batch_size=self.batch_size)
    features, labels = input_fn()
    fetches = model(features, labels, None)
    fetches = [_ for _ in fetches if _ is not None]

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      sess.run(tf.local_variables_initializer())
      sess.run(tf.tables_initializer())
      with tf.contrib.slim.queues.QueueRunners(sess):
        fetches_ = sess.run(fetches)

    sources_file.close()
    targets_file.close()

    return model, fetches_
//  [python]
def create_or_load_model(model, model_dir, session, name):
    """Create a 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())
        log.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 _build_graph(self, images, labels, mode):
    """Constructs the TF graph for the cifar model.

    Args:
      images: A 4-D image Tensor
      labels: A 2-D labels Tensor.
      mode: string indicating training mode ( e.g., 'train', 'valid', 'test').
    """
    is_training = 'train' in mode
    if is_training:
      self.global_step = tf.train.get_or_create_global_step()

    logits = build_model(
        images,
        self.num_classes,
        is_training,
        self.hparams)
    self.predictions, self.cost = helper_utils.setup_loss(
        logits, labels)
    self.accuracy, self.eval_op = tf.metrics.accuracy(
        tf.argmax(labels, 1), tf.argmax(self.predictions, 1))
    self._calc_num_trainable_params()

    # Adds L2 weight decay to the cost
    self.cost = helper_utils.decay_weights(self.cost,
                                           self.hparams.weight_decay_rate)

    if is_training:
      self._build_train_op()

    # Setup checkpointing for this child model
    # Keep 2 or more checkpoints around during training.
    with tf.device('/cpu:0'):
      self.saver = tf.train.Saver(max_to_keep=2)

    self.init = tf.group(tf.global_variables_initializer(),
                         tf.local_variables_initializer())
//  [python]
def test_extract_proposal_features_stride_eight(self):
    feature_extractor = self._build_feature_extractor(
        first_stage_features_stride=8)
    preprocessed_inputs = tf.random_uniform(
        [4, 224, 224, 3], maxval=255, dtype=tf.float32)
    rpn_feature_map, _ = feature_extractor.extract_proposal_features(
        preprocessed_inputs, scope='TestScope')
    features_shape = tf.shape(rpn_feature_map)

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

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

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

      init_op = tf.global_variables_initializer()
      with self.test_session() as sess:
        sess.run(init_op)
        features_shape_out = sess.run(features_shape)
        self.assertAllEqual(features_shape_out, [4, 14, 14, 1024])