数据转换成tfrecord类型并完成读取
前提:
tensorflow --1.13.1 numpy --1.16.2 python --3.6.5本例轉(zhuǎn)換 泰坦尼克號數(shù)據(jù)集
鏈接 密碼:n8wz
數(shù)據(jù)預(yù)覽:
字段說明:
PassengerId ,乘客的id號,這個我覺得對生存率沒影響。因為一個人的id號不會影響我是否生存下來吧。這列可以忽略
Survived ,生存的標(biāo)號,上面圖的數(shù)值1表示這個人很幸運(yùn),生存了下來。數(shù)值0,則表示遺憾。
Pclass ,船艙等級,就是我們坐船有等級之分,像高鐵,飛機(jī)都有。這個屬性會對生產(chǎn)率有影響。因為一般有錢人,權(quán)貴才會住頭等艙的。保留。
Name ,名字,這個不影響生存率。我覺得可以不用這列數(shù)據(jù)。可以忽略
Sex , 性別,這個因為全球都說lady first,女士優(yōu)先,所有這列保留。
Age , 年齡,因為優(yōu)先保護(hù)老幼,這個保留。
SibSp ,兄弟姐妹,就是有些人和兄弟姐妹一起上船的。這個會有影響,因為有可能因為救他們而導(dǎo)致自己沒有上救生船船。保留這列
Parch , 父母和小孩。就是有些人會帶著父母小孩上船的。這個也可能因為要救父母小孩耽誤上救生船。保留
Ticket , 票的編號。這個沒有影響吧。
Fare , 費(fèi)用。這個和Pclass有相同的道理,有錢人和權(quán)貴比較有勢力和影響力。這列保留
Cabin ,艙號。住的艙號沒有影響。忽略。
Embarked ,上船的地方。這列可能有影響。我認(rèn)為登陸地點不同,可能顯示人的地位之類的不一樣。我們先保留這列。
字段類型:
1.csv數(shù)據(jù)轉(zhuǎn)換成tfrecord
這里取了7個進(jìn)行后續(xù)分析,所以只保存其中7個參數(shù)
def transform_to_tfrecord():data=pd.read_csv('./data/Titanic-dataset/train.csv')tfrecord_file='train.tfrecords'def int_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]))writer=tf.python_io.TFRecordWriter(tfrecord_file)for i in range(len(data)):features=tf.train.Features(feature={"Age":float_feature(data['Age'][i]),"Survived":int_feature(data['Survived'][i]),"Pclass":int_feature(data['Pclass'][i]),"Parch":int_feature(data['Parch'][i]),"SibSp":int_feature(data['SibSp'][i]),"Sex":int_feature(1 if data['Sex'][i]=='male' else 0),"Fare":float_feature(data['Fare'][i]),})example=tf.train.Example(features=features)writer.write(example.SerializeToString())writer.close()2.tfrecord數(shù)據(jù)讀取
def read_and_decode(train_files,num_threads=2,num_epochs=100,batch_size=10,min_after_dequeue=10):reader=tf.TFRecordReader()filename_queue=tf.train.string_input_producer(train_files,num_epochs=num_epochs)_,serialized_example = reader.read(filename_queue)featuresdict=tf.parse_single_example(serialized_example,features={'Survived':tf.FixedLenFeature([],tf.int64),'Pclass':tf.FixedLenFeature([],tf.int64),'Parch':tf.FixedLenFeature([],tf.int64),'SibSp':tf.FixedLenFeature([],tf.int64),'Sex':tf.FixedLenFeature([],tf.int64),'Age':tf.FixedLenFeature([],tf.float32),'Fare':tf.FixedLenFeature([],tf.float32),})labels=featuresdict.pop('Survived')features=[tf.cast(value,tf.float32) for value in featuresdict.values()]features,labels=tf.train.shuffle_batch([features,labels],batch_size=batch_size,num_threads=num_threads,capacity=min_after_dequeue + 3 * batch_size,min_after_dequeue=min_after_dequeue)return features,labelsdef train_with_queuerunner():x,y=read_and_decode(['./data/Titanic-dataset/train.tfrecords'])with tf.Session() as sess:tf.group(tf.global_variables_initializer(),tf.local_variables_initializer()).run()coord=tf.train.Coordinator()threads=tf.train.start_queue_runners(sess=sess,coord=coord)try:step=0while not coord.should_stop():features,lables=sess.run([x,y])if step % 100==0:print('step %d:'%step,lables)step += 1except tf.errors.OutOfRangeError:print('Done training -- epoch limit reached')finally:coord.request_stop()coord.join(threads)???????從TFRecord文件中讀出數(shù)據(jù),使用TFRecordReader。TFRecordReader是一個算子,因此TensorFlow能夠記住tfrecords文件讀取的位置,并且始終能返回下一條記錄。
???????tf.train.string_input_producer方法用于定義TFRecord文件作為模型結(jié)構(gòu)的輸入部分。該函數(shù)輸入文件名列表在Session運(yùn)行時產(chǎn)生文件路徑字符串循環(huán)隊列。
???????根據(jù)產(chǎn)生的文件名,TFRecordReader.read方法打開文件,再由tf.parse_single_example方法解析成一條可用的數(shù)據(jù)。tf.train.shuffle_batch可以設(shè)置內(nèi)存讀取樣本的上限與上限訓(xùn)練batch批次的大小等參數(shù),用于定義產(chǎn)生隨機(jī)生成的batch訓(xùn)練數(shù)據(jù)包。
???????在Session的運(yùn)行中,tf.train.shuffle_batch函數(shù)生成batch數(shù)據(jù)包的過程是作為線程獨立運(yùn)行的。數(shù)據(jù)輸入線程的掛起和運(yùn)行時機(jī)由batch數(shù)據(jù)的生成函數(shù)控制。本例中的tf.train.shuffle_batch函數(shù)指定內(nèi)存保存樣本數(shù)量的上限capacity和下限min_after_dequeue。當(dāng)內(nèi)存的樣本數(shù)量大于上限capacity時,數(shù)據(jù)輸入線程掛起。反之,當(dāng)樣本數(shù)量小于min_after_dequeue時,訓(xùn)練程序掛起。函數(shù)start_queue_runners開啟對應(yīng)運(yùn)行回話Session的所有線程隊列并返回線程句柄。Coordinator類對象負(fù)責(zé)實現(xiàn)數(shù)據(jù)輸入線程的同步。當(dāng)string_input_producer函數(shù)產(chǎn)生無限循環(huán)隊列時,應(yīng)取消數(shù)據(jù)輸入與訓(xùn)練程序的線程同步。
總結(jié)
以上是生活随笔為你收集整理的数据转换成tfrecord类型并完成读取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GCP/临床试验基础知识集锦
- 下一篇: 跳跳的书包