TensorFlow 深度学习
任何深度学习网络都由四个重要部分组成:数据集、定义模型(网络结构)、训练/学习和预测/评估。
读取数据
3 种方式读取数据:
1.通过feed_dict传递数据;
2.从文件中读取数据;
3.使用预加载的数据;
1.通过feed_dict传递数据
运行每个步骤时会使用run()或者eval()函数调用中的feed_dict()参数来提供数据。是在占位符的帮助下完成的。允许传递numpy数组数据
y = tf.placeholder(tf.float32)
x = tf.placeholder(tf.float32)
with tf.Session as sess:
X_Array = some numpy Array
y_Array = some numpy Array
loss = ...
sess.run(loss,feed_dict = {x:X_Array, y:Y_Array})
x和y是占位符,在feed_dict的帮助下传递包含X值和Y值的数组。
2.从文件中读取
数据集非常大时,此方法确保不是所有数据都立即占用内存:
step1:
使用字符串张量 [“file0”,“file1”] 或者 [("file%d"i)for in in range(2)] 的方式创建文件命名列表,或者使用
files=tf.train.match_filenames_once('*.JPG')
step2:
文件名队列:创建一个队列来保存文件名,此时需要使用 tf.train.string_input_producer 函数:
filename_queue = tf.train.string_input_producer(files)
#where files is the list of filenames created above
step3:
Reader用于从文件名队列中读取文件。根据输入文件格式选择相应的阅读器。
read方法是标识文件和记录(调试时有用)以及标量字符串值的关键字。例当文件格式为.csv时:
reader = tf.TextLineReader()
key, value = reader.read(filenname_queue)
step4:
Decoder:
使用一个或多个编码器和转换操作来将值字符串解码为构成训练样本的张量:
record_defaults = [[1],[1],[1]]
col1,col2,col3 = ef.decode_csv(value)
3.预加载的数据
当数据集很小时可以使用,在内存中完全加载。将数据存储在常量或者变量中,使用变量时,将可训练标志设为False,以便训练时数据不会变。
#preloaded data as constant
training_data = ...
training_labels = ...
with tf.Session as sess:
x_data = tf.Constant (training_data)
y_data = tf.Constant(training_labels)
#preloaded data as Variables
training_data = ...
training_labels = ...
with tf.Session as sess:
data_x = tf.placeholder(dtype = training_data.dtype,shape = training_data.shape)
data_y = tf.placeholder(dtype = training_label.dtype,shape = training_label.shape)
x_data = tf.Variable(data_x,trainable = False,collections[])
y_data = tf.Variable(data_y,trainable = False,collections[])
定义模型
建立描述网络结构的计算图。它涉及指定信息从一组神经元到另一组神经元的超参数、变量和占位符序列以及损失/错误函数。
训练/学习
在DNN中的学习通常基于梯度下降法,其目的是要找到训练变量(权重、偏置),将损失、错误函数最小化。这是通过初始化变量并使用run()实现的:
with tf.Session as sess:
…
sess.run()
…
评估模型
predict()函数,使用验证数据和测试数据评估网络。