當前位置: 首頁>>代碼示例>>Python>>正文


Python sugartensor.Session方法代碼示例

本文整理匯總了Python中sugartensor.Session方法的典型用法代碼示例。如果您正苦於以下問題:Python sugartensor.Session方法的具體用法?Python sugartensor.Session怎麽用?Python sugartensor.Session使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sugartensor的用法示例。


在下文中一共展示了sugartensor.Session方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: sg_restore

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def sg_restore(sess, save_path, category=''):
    r""" Restores previously saved variables.

    Args:
      sess: A `Session` to use to restore the parameters.
      save_path: Path where parameters were previously saved.
      category: A `String` to filter variables starts with given category.

    Returns:

    """
    # to list
    if not isinstance(category, (tuple, list)):
        category = [category]

    # make variable list to load
    var_list = {}
    for cat in category:
        for t in tf.global_variables():
            if t.name.startswith(cat):
                var_list[t.name[:-2]] = t

    # restore parameters
    saver = tf.train.Saver(var_list)
    saver.restore(sess, save_path) 
開發者ID:buriburisuri,項目名稱:sugartensor,代碼行數:27,代碼來源:sg_train.py

示例2: generate

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def generate(sample_image): 
    start_time = time.time() 
 
    g = ModelGraph()
         
    with tf.Session() as sess:
        # We need to initialize variables in this case because the Variable `generator/x` will not restored.
        tf.sg_init(sess)
         
        vars = [v for v in tf.global_variables() if "generator" not in v.name]
        saver = tf.train.Saver(vars)
        saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
          
        i = 0
        while True:
            mse, _ = sess.run([g.mse, g.train_gen], {g.y: transform_image(sample_image)}) # (16, 28)
               
            if time.time() - start_time > 60: # Save every 60 seconds
                gen_image = sess.run(g.x)
                gen_image = np.squeeze(gen_image)
                misc.imsave('gen_images/%s/gen_%.2f.jpg' % (label, mse), gen_image)
                   
                start_time = time.time()
                i += 1
                if i == 60: break # Finish after 1 hour 
開發者ID:Kyubyong,項目名稱:texture_generation,代碼行數:27,代碼來源:gen.py

示例3: run_generator

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
    with tf.Session() as sess:

        tf.sg_init(sess)

        # restore parameters
        tf.sg_restore(sess, tf.train.latest_checkpoint('asset/train/infogan'), category='generator')

        # run generator
        imgs = sess.run(gen, {target_num: num,
                              target_cval_1: x1,
                              target_cval_2: x2})

        # plot result
        _, ax = plt.subplots(10, 10, sharex=True, sharey=True)
        for i in range(10):
            for j in range(10):
                ax[i][j].imshow(imgs[i * 10 + j], 'gray')
                ax[i][j].set_axis_off()
        plt.savefig('asset/train/infogan/' + fig_name, dpi=600)
        tf.sg_info('Sample image saved to "asset/train/infogan/%s"' % fig_name)
        plt.close()


#
# draw sample by categorical division
#

# fake image 
開發者ID:buriburisuri,項目名稱:sugartensor,代碼行數:31,代碼來源:mnist_info_gan_eval.py

示例4: sg_init

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def sg_init(sess):
    r""" Initializes session variables.
    
    Args:
      sess: Session to initialize. 
    """
    # initialize variables
    sess.run(tf.group(tf.global_variables_initializer(),
                      tf.local_variables_initializer())) 
開發者ID:buriburisuri,項目名稱:sugartensor,代碼行數:11,代碼來源:sg_train.py

示例5: sg_print

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def sg_print(tensor_list):
    r"""Simple tensor printing function for debugging.
    Prints the value, shape, and data type of each tensor in the list.
    
    Args:
      tensor_list: A list/tuple of tensors or a single tensor.
      
    Returns:
      The value of the tensors.
      
    For example,
    
    ```python
    import sugartensor as tf
    a = tf.constant([1.])
    b = tf.constant([2.])
    out = tf.sg_print([a, b])
    # Should print [ 1.] (1,) float32
    #              [ 2.] (1,) float32
    print(out)
    # Should print [array([ 1.], dtype=float32), array([ 2.], dtype=float32)]
    ``` 
    """
    # to list
    if type(tensor_list) is not list and type(tensor_list) is not tuple:
        tensor_list = [tensor_list]

    # evaluate tensor list with queue runner
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        sg_init(sess)
        with tf.sg_queue_context():
            res = sess.run(tensor_list)
            for r in res:
                print(r, r.shape, r.dtype)

    if len(res) == 1:
        return res[0]
    else:
        return res 
開發者ID:buriburisuri,項目名稱:sugartensor,代碼行數:41,代碼來源:sg_train.py

示例6: run_generator

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
    with tf.Session() as sess:
        tf.sg_init(sess)
        # restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train'))

        # run generator
        imgs = sess.run(gen, {target_num: num,
                              target_cval_1: x1,
                              target_cval_2: x2})

        # plot result
        _, ax = plt.subplots(10, 10, sharex=True, sharey=True)
        for i in range(10):
            for j in range(10):
                ax[i][j].imshow(imgs[i * 10 + j], 'gray')
                ax[i][j].set_axis_off()
        plt.savefig('asset/train/' + fig_name, dpi=600)
        tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
        plt.close()


#
# draw sample by categorical division
#

# fake image 
開發者ID:buriburisuri,項目名稱:ac-gan,代碼行數:30,代碼來源:generate.py

示例7: run_generator

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
    with tf.Session() as sess:
        tf.sg_init(sess)
        # restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))

        # run generator
        imgs = sess.run(gen, {target_num: num,
                              target_cval_1: x1,
                              target_cval_2: x2})

        # plot result
        _, ax = plt.subplots(10, 10, sharex=True, sharey=True)
        for i in range(10):
            for j in range(10):
                ax[i][j].plot(imgs[i * 10 + j, :, 0], color='b', linewidth=0.25)
                # Turn off tick labels only
                # ax[i][j].set_axis_off()
                ax[i][j].set_xticks([])
                ax[i][j].set_yticks([])

        plt.savefig('asset/train/' + fig_name, dpi=600)
        tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
        plt.close()


#
# draw sample by categorical division
#

# fake image 
開發者ID:jeanjerome,項目名稱:semisupervised_timeseries_infogan,代碼行數:34,代碼來源:generate.py

示例8: run_generator

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
    with tf.Session() as sess:
        tf.sg_init(sess)
        # restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))

        # run generator
        imgs = sess.run(gen, {target_num: num,
                              target_cval_1: x1,
                              target_cval_2: x2})

        # plot result
        _, ax = plt.subplots(10, 10, sharex=True, sharey=True)
        for i in range(10):
            for j in range(10):
                ax[i][j].plot(imgs[i * 10 + j, :, 0])
                ax[i][j].plot(imgs[i * 10 + j, :, 1])
                ax[i][j].set_axis_off()
        plt.savefig('asset/train/' + fig_name, dpi=600)
        tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
        plt.close()


#
# draw sample by categorical division
#

# fake image 
開發者ID:buriburisuri,項目名稱:timeseries_gan,代碼行數:31,代碼來源:generate.py

示例9: run_generator

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def run_generator(num, x1, x2, fig_name='sample.png'):
    with tf.Session() as sess:
        tf.sg_init(sess)
        # restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))

        # run generator
        imgs = sess.run(gen, {target_num: num,
                              target_cval_1: x1,
                              target_cval_2: x2})

        # plot result
        _, ax = plt.subplots(10, 10, sharex=True, sharey=True)
        for i in range(10):
            for j in range(10):
                ax[i][j].imshow(imgs[i * 10 + j], 'gray')
                ax[i][j].set_axis_off()
        plt.savefig('asset/train/' + fig_name, dpi=600)
        tf.sg_info('Sample image saved to "asset/train/%s"' % fig_name)
        plt.close()


#
# draw sample by categorical division
#

# fake image 
開發者ID:buriburisuri,項目名稱:supervised_infogan,代碼行數:30,代碼來源:generate.py

示例10: main

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def main():  
    g = ModelGraph()
        
    with tf.Session() as sess:
        tf.sg_init(sess)

        # restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train/ckpt'))
        
        hits = 0
        num_imgs = 0
        
        with tf.sg_queue_context(sess):
            # loop end-of-queue
            while True:
                try:
                    logits, y = sess.run([g.logits, g.y]) # (16, 28) 
                    preds = np.squeeze(np.argmax(logits, -1)) # (16,)
                     
                    hits += np.equal(preds, y).astype(np.int32).sum()
                    num_imgs += len(y)
                    print "%d/%d = %.02f" % (hits, num_imgs, float(hits) / num_imgs)
                except:
                    break
                
        print "\nFinal result is\n%d/%d = %.02f" % (hits, num_imgs, float(hits) / num_imgs)
                    
                    
                             
#                     fout.write(u"▌file_name: {}\n".format(f))
#                     fout.write(u"▌Expected: {}\n".format(label2cls[]))
#                     fout.write(u"▌file_name: {}\n".format(f))
#                     fout.write(u"▌Got: " + predicted + "\n\n") 
開發者ID:Kyubyong,項目名稱:texture_generation,代碼行數:36,代碼來源:test.py

示例11: sg_regularizer_loss

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def sg_regularizer_loss(scale=1.0):
    r""" Get regularizer losss

    Args:
      scale: A scalar. A weight applied to regularizer loss
    """
    return scale * tf.reduce_mean(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))

# Under construction
# def sg_tsne(tensor, meta_file='metadata.tsv', save_dir='asset/tsne'):
#     r""" Manages arguments of `tf.sg_opt`.
#
#     Args:
#         save_dir: A string. The root path to which checkpoint and log files are saved.
#           Default is `asset/train`.
#     """
#
#     # make directory if not exist
#     if not os.path.exists(save_dir):
#         os.makedirs(save_dir)
#
#     # checkpoint saver
#     saver = tf.train.Saver()
#
#     # summary writer
#     summary_writer = tf.summary.FileWriter(save_dir, graph=tf.get_default_graph())
#
#     # embedding visualizer
#     config = projector.ProjectorConfig()
#     emb = config.embeddings.add()
#     emb.tensor_name = tensor.name   # tensor
#     # emb.metadata_path = os.path.join(save_dir, meta_file)   # metadata file
#     projector.visualize_embeddings(summary_writer, config)
#
#     # create session
#     sess = tf.Session()
#     # initialize variables
#     sg_init(sess)
#
#     # save tsne
#     saver.save(sess, save_dir + '/model-tsne')
#
#     # logging
#     tf.sg_info('Tsne saved at %s' % (save_dir + '/model-tsne'))
#
#     # close session
#     sess.close() 
開發者ID:buriburisuri,項目名稱:sugartensor,代碼行數:49,代碼來源:sg_train.py

示例12: eval

# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import Session [as 別名]
def eval(): 
    # Load graph
    g = Graph(mode="inference"); print("Graph Loaded")
        
    with tf.Session() as sess:
        # Initialize variables
        tf.sg_init(sess)

        # Restore parameters
        saver = tf.train.Saver()
        saver.restore(sess, tf.train.latest_checkpoint('asset/train'))
        print("Restored!")
        mname = open('asset/train/checkpoint', 'r').read().split('"')[1] # model name
        
        # Load data
        X, Sources, Targets = load_test_data()
        char2idx, idx2char = load_vocab()
        
        with codecs.open(mname, "w", "utf-8") as fout:
            list_of_refs, hypotheses = [], []
            for i in range(len(X) // Hp.batch_size):
                # Get mini-batches
                x = X[i*Hp.batch_size: (i+1)*Hp.batch_size] # mini-batch
                sources = Sources[i*Hp.batch_size: (i+1)*Hp.batch_size]
                targets = Targets[i*Hp.batch_size: (i+1)*Hp.batch_size]
                
                preds_prev = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)
                preds = np.zeros((Hp.batch_size, Hp.maxlen), np.int32)        
                for j in range(Hp.maxlen):
                    # predict next character
                    outs = sess.run(g.preds, {g.x: x, g.y_src: preds_prev})
                    # update character sequence
                    if j < Hp.maxlen - 1:
                        preds_prev[:, j + 1] = outs[:, j]
                    preds[:, j] = outs[:, j]
                
                # Write to file
                for source, target, pred in zip(sources, targets, preds): # sentence-wise
                    got = "".join(idx2char[idx] for idx in pred).split(u"␃")[0]
                    fout.write("- source: " + source +"\n")
                    fout.write("- expected: " + target + "\n")
                    fout.write("- got: " + got + "\n\n")
                    fout.flush()
                    
                    # For bleu score
                    ref = target.split()
                    hypothesis = got.split()
                    if len(ref) > 2:
                        list_of_refs.append([ref])
                        hypotheses.append(hypothesis)
            
            # Get bleu score
            score = corpus_bleu(list_of_refs, hypotheses)
            fout.write("Bleu Score = " + str(100*score)) 
開發者ID:Kyubyong,項目名稱:quasi-rnn,代碼行數:56,代碼來源:eval.py


注:本文中的sugartensor.Session方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。