最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

tensorflow中next_batch的具體使用

 更新時間:2018年02月02日 09:03:03   作者:小妖精Fsky  
本篇文章主要介紹了tensorflow中next_batch的具體使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了tensorflow中next_batch的具體使用,分享給大家,具體如下:

此處給出了幾種不同的next_batch方法,該文章只是做出代碼片段的解釋,以備以后查看:

 def next_batch(self, batch_size, fake_data=False):
  """Return the next `batch_size` examples from this data set."""
  if fake_data:
   fake_image = [1] * 784
   if self.one_hot:
    fake_label = [1] + [0] * 9
   else:
    fake_label = 0
   return [fake_image for _ in xrange(batch_size)], [
     fake_label for _ in xrange(batch_size)
   ]
  start = self._index_in_epoch
  self._index_in_epoch += batch_size
  if self._index_in_epoch > self._num_examples: # epoch中的句子下標是否大于所有語料的個數(shù),如果為True,開始新一輪的遍歷
   # Finished epoch
   self._epochs_completed += 1
   # Shuffle the data
   perm = numpy.arange(self._num_examples) # arange函數(shù)用于創(chuàng)建等差數(shù)組
   numpy.random.shuffle(perm) # 打亂
   self._images = self._images[perm]
   self._labels = self._labels[perm]
   # Start next epoch
   start = 0
   self._index_in_epoch = batch_size
   assert batch_size <= self._num_examples
  end = self._index_in_epoch
  return self._images[start:end], self._labels[start:end]

該段代碼摘自mnist.py文件,從代碼第12行start = self._index_in_epoch開始解釋,_index_in_epoch-1是上一次batch個圖片中最后一張圖片的下邊,這次epoch第一張圖片的下標是從 _index_in_epoch開始,最后一張圖片的下標是_index_in_epoch+batch, 如果 _index_in_epoch 大于語料中圖片的個數(shù),表示這個epoch是不合適的,就算是完成了語料的一遍的遍歷,所以應該對圖片洗牌然后開始新一輪的語料組成batch開始

def ptb_iterator(raw_data, batch_size, num_steps):
 """Iterate on the raw PTB data.

 This generates batch_size pointers into the raw PTB data, and allows
 minibatch iteration along these pointers.

 Args:
  raw_data: one of the raw data outputs from ptb_raw_data.
  batch_size: int, the batch size.
  num_steps: int, the number of unrolls.

 Yields:
  Pairs of the batched data, each a matrix of shape [batch_size, num_steps].
  The second element of the tuple is the same data time-shifted to the
  right by one.

 Raises:
  ValueError: if batch_size or num_steps are too high.
 """
 raw_data = np.array(raw_data, dtype=np.int32)

 data_len = len(raw_data)
 batch_len = data_len // batch_size #有多少個batch
 data = np.zeros([batch_size, batch_len], dtype=np.int32) # batch_len 有多少個單詞
 for i in range(batch_size): # batch_size 有多少個batch
  data[i] = raw_data[batch_len * i:batch_len * (i + 1)]

 epoch_size = (batch_len - 1) // num_steps # batch_len 是指一個batch中有多少個句子
 #epoch_size = ((len(data) // model.batch_size) - 1) // model.num_steps # // 表示整數(shù)除法
 if epoch_size == 0:
  raise ValueError("epoch_size == 0, decrease batch_size or num_steps")

 for i in range(epoch_size):
  x = data[:, i*num_steps:(i+1)*num_steps]
  y = data[:, i*num_steps+1:(i+1)*num_steps+1]
  yield (x, y)

第三種方式:

  def next(self, batch_size):
    """ Return a batch of data. When dataset end is reached, start over.
    """
    if self.batch_id == len(self.data):
      self.batch_id = 0
    batch_data = (self.data[self.batch_id:min(self.batch_id +
                         batch_size, len(self.data))])
    batch_labels = (self.labels[self.batch_id:min(self.batch_id +
                         batch_size, len(self.data))])
    batch_seqlen = (self.seqlen[self.batch_id:min(self.batch_id +
                         batch_size, len(self.data))])
    self.batch_id = min(self.batch_id + batch_size, len(self.data))
    return batch_data, batch_labels, batch_seqlen

第四種方式:

def batch_iter(sourceData, batch_size, num_epochs, shuffle=True):
  data = np.array(sourceData) # 將sourceData轉(zhuǎn)換為array存儲
  data_size = len(sourceData)
  num_batches_per_epoch = int(len(sourceData) / batch_size) + 1
  for epoch in range(num_epochs):
    # Shuffle the data at each epoch
    if shuffle:
      shuffle_indices = np.random.permutation(np.arange(data_size))
      shuffled_data = sourceData[shuffle_indices]
    else:
      shuffled_data = sourceData

    for batch_num in range(num_batches_per_epoch):
      start_index = batch_num * batch_size
      end_index = min((batch_num + 1) * batch_size, data_size)

      yield shuffled_data[start_index:end_index]

迭代器的用法,具體學習Python迭代器的用法

另外需要注意的是,前三種方式只是所有語料遍歷一次,而最后一種方法是,所有語料遍歷了num_epochs次

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python3enumrate和range對比及示例詳解

    Python3enumrate和range對比及示例詳解

    這篇文章主要介紹了Python3enumrate和range對比及示例詳解,在Python中,enumrate和range都常用于for循環(huán)中,enumrate函數(shù)用于同時循環(huán)列表和元素,而range()函數(shù)可以生成數(shù)值范圍變化的列表,而能夠用于for循環(huán)即都是可迭代的,需要的朋友可以參考下
    2019-07-07
  • python之no module named xxxx以及虛擬環(huán)境配置過程

    python之no module named xxxx以及虛擬環(huán)境配置過程

    在Python開發(fā)過程中,經(jīng)常會遇到環(huán)境配置和包管理的問題,主要原因包括未安裝所需包或使用虛擬環(huán)境導致的,通過pip install命令安裝缺失的包是解決問題的一種方式,此外,使用虛擬環(huán)境,例如PyCharm支持的Virtualenv,可以為每個項目創(chuàng)建獨立的運行環(huán)境
    2024-10-10
  • Python通過4種方式實現(xiàn)進程數(shù)據(jù)通信

    Python通過4種方式實現(xiàn)進程數(shù)據(jù)通信

    這篇文章主要介紹了Python通過4種方式實現(xiàn)進程數(shù)據(jù)通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • python2.7使用plotly繪制本地散點圖和折線圖

    python2.7使用plotly繪制本地散點圖和折線圖

    這篇文章主要為大家詳細介紹了python2.7使用plotly繪制本地散點圖和折線圖實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 跟老齊學Python之不要紅頭文件(2)

    跟老齊學Python之不要紅頭文件(2)

    在前面學習了基本的打開和建立文件之后,就可以對文件進行多種多樣的操作了。請看官要注意,文件,不是什么特別的東西,就是一個對象,如同對待此前學習過的字符串、列表等一樣。
    2014-09-09
  • python 調(diào)用js的四種方式

    python 調(diào)用js的四種方式

    這篇文章主要介紹了python 調(diào)用js的四種方式,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-04-04
  • python腳本監(jiān)控logstash進程并郵件告警實例

    python腳本監(jiān)控logstash進程并郵件告警實例

    這篇文章主要介紹了python腳本監(jiān)控logstash進程并郵件告警實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python使用reportlab實現(xiàn)圖片轉(zhuǎn)換成pdf的方法

    python使用reportlab實現(xiàn)圖片轉(zhuǎn)換成pdf的方法

    這篇文章主要介紹了python使用reportlab實現(xiàn)圖片轉(zhuǎn)換成pdf的方法,涉及Python使用reportlab模塊操作圖片轉(zhuǎn)換的相關技巧,需要的朋友可以參考下
    2015-05-05
  • Python7個爬蟲小案例詳解(附源碼)上篇

    Python7個爬蟲小案例詳解(附源碼)上篇

    這篇文章主要介紹了Python7個爬蟲小案例詳解(附源碼)上篇,本文章內(nèi)容詳細,通過案例可以更好的理解爬蟲的相關知識,七個例子分為了三部分,本次為上篇,共有二道題,需要的朋友可以參考下
    2023-01-01
  • Python使用oslo.vmware管理ESXI虛擬機的示例參考

    Python使用oslo.vmware管理ESXI虛擬機的示例參考

    oslo.vmware是OpenStack通用框架中的一部分,主要用于實現(xiàn)對虛擬機的管理任務,借助oslo.vmware模塊我們可以管理Vmware ESXI集群環(huán)境。
    2021-06-06

最新評論

香河县| 芦溪县| 化德县| 美姑县| 佛坪县| 沅江市| 北京市| 兴国县| 仲巴县| 常德市| 中山市| 绥宁县| 通山县| 苍山县| 静安区| 合作市| 东港市| 武山县| 营口市| 桃源县| 渭南市| 聂拉木县| 汉源县| 密云县| 小金县| 奇台县| 定安县| 丹阳市| 巢湖市| 桦川县| 当雄县| 重庆市| 崇州市| 永定县| 金山区| 新巴尔虎右旗| 陵水| 上高县| 东阿县| 庐江县| 永靖县|