由於資料的**複雜性以及每乙個樣例中的資訊較為豐富,從而需要一種統一的格式來儲存資料,然而在tensorflow中提供了tfreord的格式來統一輸入資料的格式。
tfrecord檔案中的資料是通過tf.train.example protoclo buffer的格式儲存;
tf.train.example定義為:
message example ;
message features ;
message feature
};
tf.train.example 中包含乙個從屬性名稱到取值的字典。屬性名稱為乙個字串,屬性的取值可以為字串(byteslist),實數列表(floatlist)或者整數列表(int64list)
tfrecord樣例程式:
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 _bytes_feature(value): #生成字串型的屬性
return tf.train.feature(bytes_list=tf.train.byteslist(value=[value]))
mnist = input_data.read_data_sets("d/path/to/mnist/data", dtype=tf.uint8, one_hot=true)
images = mnist.train.images
labels = mnist.train.labels #訓練資料所對應的正確答案,可作為乙個屬性儲存在tfrecord中
pixels = images.shape[1]
num_examples = mnist.train.num_examples #訓練資料的影象解析度,可以作為example中的乙個屬性
filename = "d/path/to/output.tfrecords" #輸出tfrecord檔案的位址
writer = tf.python_io.tfrecordwriter(filename) #通過writer來寫tfrecord檔案
for index in range(num_examples):
image_raw = images[index].tostring()#將影象矩陣轉化為乙個字串
example = tf.train.example(features=tf.train.feature(feature=))
writer.write(example.serializetostring()) #將乙個example寫入tfrecord檔案
writer.close()
tensorflow中tfrecords使用介紹
這篇文章主要講一下如何用tensorflow中的標準資料讀取方式簡單的實現對自己資料的讀取操作 主要分為以下兩個步驟 1 將自己的資料集轉化為 xx.tfrecords的形式 2 在自己的程式中讀取並使用.tfrecords進行操作 資料集轉換 為了便於講解,我們簡單製作了乙個資料,如下圖所示 程式...
Tensorflow中dynamic rnn的用法
1 api介面dynamic rnn cell,inputs,sequence length none,initial state none,dtype none,parallel iterations none,swap memory false,time major false,scope no...
TensorFlow中遮蔽warning的方法
tensorflow的日誌級別分為以下三種 tf cpp min log level 1 預設設定,為顯示所有資訊 tf cpp min log level 2 只顯示error和warining資訊 tf cpp min log level 3 只顯示error資訊 所以,當tensorflow出...