TensorFlow1.12.0
TensorFlow 的數(shù)據(jù)讀取機(jī)制
一、TensorFlow基礎(chǔ)
1.使用多線程并行化
tf.data模塊運(yùn)行時,使用多線程進(jìn)行數(shù)據(jù)通道處理鲁纠,從而實現(xiàn)并行,這種操作幾乎是透明的搁凸。我們只需要添加一個num_parallel_calls參數(shù)到每一個dataset.map()call中,
num_threads = 4
dataset = dataset.map(parse_function, num_parallel_calls=num_threads)
如果使用值tf.data.experimental.AUTOTUNE,則根據(jù)可用的CPU動態(tài)設(shè)置并行調(diào)用的數(shù)量。
num_parallel_calls 參數(shù)的最優(yōu)值取決于你的硬件铡恕,訓(xùn)練數(shù)據(jù)的特點(diǎn)(比如:它的 size、shape)回挽,map 函數(shù)的計算量 和 CPU 上同時進(jìn)行的其它處理没咙。一個簡單的原則是:將 num_parallel_calls 設(shè)置為 CPU 的核心數(shù)猩谊。例如千劈,如果 CPU 有四個核,將 num_parallel_calls 設(shè)置為 4 將會很高效牌捷。另一方面墙牌,設(shè)置 num_parallel_calls 大于 CPU 的核心數(shù)涡驮,能夠?qū)е碌托У恼{(diào)度,導(dǎo)致輸入管道速度下降喜滨。
2.圖片格式
image = tf.image.decode_jpeg(image_file) # 如果是png格式的圖片捉捅,使用tf.image.decode_png()
3.tensorflow中tf.one_hot()函數(shù)的作用
是將一個值化為一個概率分布的向量,一般用于分類問題虽风。
具體用法以及作用見以下代碼:
import numpy as np
import tensorflow as tf
SIZE=6
CLASS=8
label1=tf.constant([0,1,2,3,4,5,6,7])
sess1=tf.Session()
print('label1:',sess1.run(label1))
b = tf.one_hot(label1,CLASS,1,0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(b)
print('after one_hot',sess.run(b))
輸出:
label1: [0 1 2 3 4 5 6 7]
after one_hot:
[[1 0 0 0 0 0 0 0]
[0 1 0 0 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 0 1 0 0 0]
[0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 0 1]]
二棒口、TensorFlow報錯
1. ImportError: cannot import name 'Iterator'
嘗試:
import tensorflow as tf
Iterator = tf.data.Iterator
2. tensorflow/core/framework/allocator.cc:122] Allocation of 1247616000 exceeds 10% of system memory.
batch_size過大,調(diào)小設(shè)定的 batch_size 即可
Batch_size參數(shù)的作用:決定了下降的方向
總結(jié):對于新手而言辜膝,在GPU內(nèi)存足夠的情況下无牵,結(jié)合樣本大小,可以嘗試batch_size為8厂抖,16茎毁,32,64等.
參考文獻(xiàn):
1.TensorFlow學(xué)習(xí)(一)——常用方法
2.http://www.reibang.com/nb/34356339
3.第一個TensorFlow程序(hello world)詳解