久久ER99热精品一区二区-久久精品99国产精品日本-久久精品免费一区二区三区-久久综合九色综合欧美狠狠

博客專欄

EEPW首頁 > 博客 > Python視頻教程之Tensorflow中的tf.train.batch函數的使用分享

Python視頻教程之Tensorflow中的tf.train.batch函數的使用分享

發布人:扣丁學堂1 時間:2021-01-06 來源:工程師 發布文章

tensorflow中的讀取數據的隊列,不論是對正喜歡Python開發的人來說,還是對已經走入工作中的Python開發工程師來說都是比較難懂的。原因可能之大家對這方面的經驗不足吧。下面扣丁學堂Python培訓小編就和大家分享一下關于Tensorflow中的tf.train.batch函數的使用。

扣丁學堂Python培訓告訴你關于Tensorflow中的tf.train.batch函數的使用

tensorflow中的讀取數據的隊列,簡單的說,就是計算圖是從一個管道中讀取數據的,錄入管道是用的現成的方法,讀取也是。為了保證多線程的時候從一個管道讀取數據不會亂吧,所以這種時候讀取的時候需要線程管理的相關操作。今天給大家分享一個簡單的操作,就是給一個有序的數據,看看讀出來是不是有序的,結果發現是有序的,所以直接給代碼:

import tensorflow as tf
import numpy as np

def generate_data():
  num = 25
  label = np.asarray(range(0, num))
  images = np.random.random([num, 5, 5, 3])
  print('label size :{}, image size {}'.format(label.shape, images.shape))
  return label, images

def get_batch_data():
  label, images = generate_data()
  images = tf.cast(images, tf.float32)
  label = tf.cast(label, tf.int32)
  input_queue = tf.train.slice_input_producer([images, label], shuffle=False)
  image_batch, label_batch = tf.train.batch(input_queue, batch_size=10, num_threads=1, capacity=64)
  return image_batch, label_batch

image_batch, label_batch = get_batch_data()
with tf.Session() as sess:
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(sess, coord)
  i = 0
  try:
    while not coord.should_stop():
      image_batch_v, label_batch_v = sess.run([image_batch, label_batch])
      i += 1
      for j in range(10):
        print(image_batch_v.shape, label_batch_v[j])
  except tf.errors.OutOfRangeError:
    print("done")
  finally:
    coord.request_stop()
  coord.join(threads)


記得那個slice_input_producer方法,默認是要shuffle的哈。

Besides, I would like to comment this code.


1: there is a parameter ‘num_epochs' in slice_input_producer, which controls how many epochs the slice_input_producer method would work. when this method runs the specified epochs, it would report the OutOfRangeRrror. I think it would be useful for our control the training epochs.

2: the output of this method is one single image, we could operate this single image with tensorflow API, such as normalization, crops, and so on, then this single image is feed to batch method, a batch of images for training or testing wouldbe received.

tf.train.batch和tf.train.shuffle_batch的區別用法

tf.train.batch([example, label], batch_size=batch_size, capacity=capacity):[example, label]表示樣本和樣本標簽,這個可以是一個樣本和一個樣本標簽,batch_size是返回的一個batch樣本集的樣本個數。capacity是隊列中的容量。這主要是按順序組合成一個batch


tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue)。這里面的參數和上面的一樣的意思。不一樣的是這個參數min_after_dequeue,一定要保證這參數小于capacity參數的值,否則會出錯。這個代表隊列中的元素大于它的時候就輸出亂的順序的batch。也就是說這個函數的輸出結果是一個亂序的樣本排列的batch,不是按照順序排列的。


上面的函數返回值都是一個batch的樣本和樣本標簽,只是一個是按照順序,另外一個是隨機的。


以上內容是小編簡單整理的,小伙伴們先了解一下,如果想要了解更多內容的話可以登錄扣丁學堂官網查看詳細信息。


扣丁學堂Python在線學習是專業的Python培訓機構,不僅有專業的老師和與時俱進的課程體系,還有大量的Python在線教程供學員觀看學習。如果你想學好Python開發技術高薪就業的話,那就不要再猶豫了,快到報名扣丁學堂Python在線學習吧??鄱W堂python學習交流群:816572891。微信號:codingbb

*博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



關鍵詞:

相關推薦

技術專區

關閉