bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

使用TFRecord怎么存取多個數據-創新互聯

這篇文章給大家介紹使用TFRecord怎么存取多個數據,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創新互聯公司專注于企業成都全網營銷推廣、網站重做改版、內江網站定制設計、自適應品牌網站建設、H5響應式網站商城網站制作、集團公司官網建設、成都外貿網站建設、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為內江等各大城市提供網站開發制作服務。

TensorFlow提供了一種統一的格式來存儲數據,就是TFRecord,它可以統一不同的原始數據格式,并且更加有效地管理不同的屬性。

TFRecord格式

TFRecord文件中的數據都是用tf.train.Example Protocol Buffer的格式來存儲的,tf.train.Example可以被定義為:

message Example{
  Features features = 1
}

message Features{
  map<string, Feature> feature = 1
}

message Feature{
  oneof kind{
    BytesList bytes_list = 1
    FloatList float_list = 1
    Int64List int64_list = 1
  }
}

可以看出Example是一個嵌套的數據結構,其中屬性名稱可以為一個字符串,其取值可以是字符串BytesList、實數列表FloatList或整數列表Int64List。

將數據轉化為TFRecord格式

以下代碼是將MNIST輸入數據轉化為TFRecord格式:

# -*- coding: utf-8 -*-

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np


# 生成整數型的屬性
def _int64_feature(value):
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

# 生成浮點型的屬性
def _float_feature(value):
  return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))  
#若想保存為數組,則要改成value=value即可


# 生成字符串型的屬性
def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


mnist = input_data.read_data_sets("/tensorflow_google", dtype=tf.uint8, one_hot=True)
images = mnist.train.images
# 訓練數據所對應的正確答案,可以作為一個屬性保存在TFRecord中
labels = mnist.train.labels
# 訓練數據的圖像分辨率,這可以作為Example中的一個屬性
pixels = images.shape[1]
num_examples = mnist.train.num_examples

# 輸出TFRecord文件的地址
filename = "/tensorflow_google/mnist_output.tfrecords"
# 創建一個writer來寫TFRecord文件
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
  # 將圖像矩陣轉換成一個字符串
  image_raw = images[index].tostring()
  # 將一個樣例轉化為Example Protocol Buffer, 并將所有的信息寫入這個數據結構
  example = tf.train.Example(features=tf.train.Features(feature={
    'pixels': _int64_feature(pixels),
    'label': _int64_feature(np.argmax(labels[index])),
    'image_raw': _bytes_feature(image_raw)}))

  # 將一個Example寫入TFRecord文件
  writer.write(example.SerializeToString())
writer.close()

本程序將MNIST數據集中所有的訓練數據存儲到了一個TFRecord文件中,若數據量較大,也可以存入多個文件。

從TFRecord文件中讀取數據

以下代碼可以從上面代碼中的TFRecord中讀取單個或多個訓練數據:

# -*- coding: utf-8 -*-
import tensorflow as tf

# 創建一個reader來讀取TFRecord文件中的樣例
reader = tf.TFRecordReader()
# 創建一個隊列來維護輸入文件列表
filename_queue = tf.train.string_input_producer(["/Users/gaoyue/文檔/Program/tensorflow_google/chapter7"
                         "/mnist_output.tfrecords"])

# 從文件中讀出一個樣例,也可以使用read_up_to函數一次性讀取多個樣例
# _, serialized_example = reader.read(filename_queue)
_, serialized_example = reader.read_up_to(filename_queue, 6) #讀取6個樣例
# 解析讀入的一個樣例,如果需要解析多個樣例,可以用parse_example函數
# features = tf.parse_single_example(serialized_example, features={
# 解析多個樣例
features = tf.parse_example(serialized_example, features={
  # TensorFlow提供兩種不同的屬性解析方法
  # 第一種是tf.FixedLenFeature,得到的解析結果為Tensor
  # 第二種是tf.VarLenFeature,得到的解析結果為SparseTensor,用于處理稀疏數據
  # 解析數據的格式需要與寫入數據的格式一致
  'image_raw': tf.FixedLenFeature([], tf.string),
  'pixels': tf.FixedLenFeature([], tf.int64),
  'label': tf.FixedLenFeature([], tf.int64),
})

# tf.decode_raw可以將字符串解析成圖像對應的像素數組
images = tf.decode_raw(features['image_raw'], tf.uint8)
labels = tf.cast(features['label'], tf.int32)
pixels = tf.cast(features['pixels'], tf.int32)

sess = tf.Session()
# 啟動多線程處理輸入數據
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

# 每次運行可以讀取TFRecord中的一個樣例,當所有樣例都讀完之后,會重頭讀取
# for i in range(10):
#   image, label, pixel = sess.run([images, labels, pixels])
#   # print(image, label, pixel)
#   print(label, pixel)

# 讀取TFRecord中的前6個樣例,若加入循環,則會每次從上次輸出的地方繼續順序讀6個樣例
image, label, pixel = sess.run([images, labels, pixels])
print(label, pixel)

sess.close()

>> [7 3 4 6 1 8] [784 784 784 784 784 784]

輸出結果顯示,從TFRecord文件中順序讀出前6個樣例。

關于使用TFRecord怎么存取多個數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文標題:使用TFRecord怎么存取多個數據-創新互聯
標題來源:http://vcdvsql.cn/article40/ccspho.html

成都網站建設公司_創新互聯,為您提供關鍵詞優化品牌網站設計全網營銷推廣定制開發網站策劃ChatGPT

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

h5響應式網站建設