我們用上一節(jié)讀取tfrecord文件得到的數(shù)據(jù)來建立一個(gè)簡(jiǎn)單的二分類神經(jīng)網(wǎng)絡(luò)染服。進(jìn)而介紹模型的保存和導(dǎo)入方法
# 簡(jiǎn)單的二分類神經(jīng)網(wǎng)絡(luò):?jiǎn)坞[藏層
# 每個(gè)樣本有5個(gè)features
# 隱藏層有10個(gè)神經(jīng)元,一個(gè)輸出
# -*- coding: UTF-8 -*-
#!/usr/bin/python3
# Env: python3.6
import tensorflow as tf
import numpy as np
import os
# path
data_filename = 'data/data_train.txt'
size = (10000, 5)
tfrecord_path = 'data/test_data.tfrecord'
# tfrecord_path2 = 'data/test_data2.tfrecord'
# generate data 10000*5, label: 0 or 1
# generate tfrecord named test_data.tfrecord.
def generate_data(data_filename = data_filename, size=size):
if not os.path.exists(data_filename):
np.random.seed(9)
x_data = np.random.randint(0, 10, size = size)
y1_data = np.ones((size[0]//2, 1), int)
y2_data = np.zeros((size[0]//2, 1), int)
y_data = np.append(y1_data, y2_data)
np.random.shuffle(y_data)
# stitching together x and y in one file
xy_data = str('')
for xy_row in range(len(x_data)):
x_str = str('')
for xy_col in range(len(x_data[0])):
if not xy_col == (len(x_data[0])-1):
x_str =x_str+str(x_data[xy_row, xy_col])+' '
else:
x_str = x_str + str(x_data[xy_row, xy_col])
y_str = str(y_data[xy_row])
xy_data = xy_data+(x_str+'/'+y_str + '\n')
#print(xy_data[1])
# write to txt
write_txt = open(data_filename, 'w')
write_txt.write(xy_data)
write_txt.close()
return
# obtain data from txt
# every line of data is just as follow: 1 2 3 4 5/1. train data: 1 2 3 4 5, label: 1
def txt_to_tfrecord(txt_filename = data_filename, tfrecord_path = tfrecord_path):
# 第一步:生成TFRecord Writer
writer = tf.python_io.TFRecordWriter(tfrecord_path)
# 第二步:讀取TXT數(shù)據(jù),并分割出樣本數(shù)據(jù)和標(biāo)簽
file = open(txt_filename)
for data_line in file.readlines(): # 每一行
data_line = data_line.strip('\n') # 去掉換行符
sample = []
spls = data_line.split('/', 1)[0]# 樣本
for m in spls.split(' '):
sample.append(int(m))
label = data_line.split('/', 1)[1]# 標(biāo)簽
label = int(label)
print('sample:', sample, 'labels:', label)
# 第三步: 建立feature字典,tf.train.Feature()對(duì)單一數(shù)據(jù)編碼成feature
feature = {'sample': tf.train.Feature(int64_list=tf.train.Int64List(value=sample)),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[label]))}
# 第四步:可以理解為將內(nèi)層多個(gè)feature的字典數(shù)據(jù)再編碼腻扇,集成為features
features = tf.train.Features(feature = feature)
# 第五步:將features數(shù)據(jù)封裝成特定的協(xié)議格式
example = tf.train.Example(features=features)
# 第六步:將example數(shù)據(jù)序列化為字符串
Serialized = example.SerializeToString()
# 第七步:將序列化的字符串?dāng)?shù)據(jù)寫入?yún)f(xié)議緩沖區(qū)
writer.write(Serialized)
# 記得關(guān)閉writer和open file的操作
writer.close()
file.close()
return
#txt_to_tfrecord(data_filename, tfrecord_path2)
# read tfrecord
def _parse_function(example_proto):
dics = { # 這里沒用default_value情竹,隨后的都是None
'sample': tf.FixedLenFeature([5], tf.int64), # 如果不是標(biāo)量届榄,一定要在這里說明數(shù)組的長(zhǎng)度
'label': tf.FixedLenFeature([], tf.int64)}
# 把序列化樣本和解析字典送入函數(shù)里得到解析的樣本
parsed_example = tf.parse_single_example(example_proto, dics)
parsed_example['sample'] = tf.cast(parsed_example['sample'], tf.float32)
parsed_example['label'] = tf.cast(parsed_example['label'], tf.float32)
# 返回所有feature
return parsed_example
def read_dataset(tfrecord_path = tfrecord_path):
# 聲明閱讀器
dataset = tf.data.TFRecordDataset(tfrecord_path)
# 建立解析函數(shù)
new_dataset = dataset.map(_parse_function)
# 打亂樣本順序
shuffle_dataset = new_dataset.shuffle(buffer_size=20000)
# batch輸出
batch_dataset = shuffle_dataset.batch(2)
# 建立迭代器
iterator = batch_dataset.make_one_shot_iterator()
# 獲得下一個(gè)樣本
next_element = iterator.get_next()
x_samples = next_element['sample']
y_labels = next_element['label']
return x_samples, y_labels
def weight_bias_variable(weight_shape, bias_shape):
weight = tf.get_variable('weight', weight_shape, initializer=tf.random_normal_initializer(mean=0, stddev=1))
bias = tf.get_variable('bias', bias_shape, initializer=tf.random_normal_initializer(mean=0, stddev=1))
return weight, bias
# neural network:
# input layer: 5 features with on sample
# one hidden layer: 10 neuron
# output: y_out
################ fetch data ####################
with tf.variable_scope('input_data'):
x_samples, y_labels = read_dataset()
with tf.variable_scope('hidden_layer1', reuse=tf.AUTO_REUSE):
w1, b1 = weight_bias_variable(weight_shape=[5, 10], bias_shape=[10])
y_hidden = tf.nn.relu(tf.matmul(x_samples, w1) + b1)
tf.summary.histogram('w1', w1)
tf.summary.histogram('b1', b1)
with tf.variable_scope('output_layer', reuse=tf.AUTO_REUSE):
w2, b2 = weight_bias_variable(weight_shape=[10, 1], bias_shape=[1])
y_out = tf.matmul(y_hidden, w2) + b2
y_out = tf.reshape(y_out, [-1])
tf.summary.histogram('w2', w2)
tf.summary.histogram('b2', b2)
with tf.variable_scope('loss_function'):
# ################ Loss Function
# 這里的sigmoid是對(duì)y_out的激活函數(shù)
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_out, labels=y_labels, name=None)
loss_mean = tf.reduce_mean(loss, 0)
tf.summary.scalar('loss_mean', loss_mean)
################## BackPropagation
# 創(chuàng)建基于梯度下降算法的Optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
# 添加操作節(jié)點(diǎn)莉兰,用于最小化loss挑围,并更新var_list
# 該函數(shù)是簡(jiǎn)單的合并了compute_gradients()與apply_gradients()函數(shù)
# 返回為一個(gè)優(yōu)化更新后的var_list
train = optimizer.minimize(loss_mean)
save_path = 'data/save/b2.txt'
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# 建立tensorbord
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('data/tfboard', sess.graph)
saver = tf.train.Saver()
for i in range(2000):
sess.run(train)
summary = sess.run(merged)
writer.add_summary(summary, i)
if i % 1000 == 0:
print(' ############### step = %d ############ ' %i)
print('b2: ', sess.run(b2))
# 用官網(wǎng)介紹的checkpoint方式保存模型
# 創(chuàng)建saver對(duì)象,默認(rèn)max_to_keep=5糖荒,保存最近5次的模型杉辙。
saver.save(sess, 'data/tmp/model', global_step=1000) # 保存第1000步的模型
# 將變量保存到文件(這里也可以創(chuàng)建字典,將所有變量寫成tfrecord文件
# 用sess.run就是將tensor數(shù)據(jù)轉(zhuǎn)為python數(shù)據(jù)捶朵,然后進(jìn)行保存
b2_save = sess.run(b2)
print('TXT b2 save:', b2_save)
np.savetxt(save_path, b2_save)
writer.close()
coord.request_stop()
coord.join(threads)
##### checkpoint 恢復(fù)模型
# 為了區(qū)分蜘矢,我們?cè)俳⒁粋€(gè)session
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# 恢復(fù)模型,這是一個(gè) protocol buffer保存了完整的Tensorflow圖综看,即所有變量品腹、操作和集合等。擁有一個(gè).meta
last_ckpt = saver.last_checkpoints # 得到保存模型的路徑
saver_restore = tf.train.import_meta_graph(os.path.join(last_ckpt[0] + '.meta'))
# 用 checkpoint 恢復(fù)模型參數(shù)
saver_restore.restore(sess, last_ckpt[0]) # method 1
print('methond1:ckpt: ', sess.run(b2)) # 要知道參數(shù)名
saver.restore(sess, last_ckpt[0]) # method 2
print('methond2:ckpt: ', sess.run(b2)) # 要知道參數(shù)名
# 讀取TXT文檔恢復(fù)參數(shù) # method 3
b2_restore = np.loadtxt(save_path)
b2_restore = tf.cast(b2_restore, tf.float32) # numpy默認(rèn)float64而不是float32红碑,而TF中默認(rèn)時(shí)float32舞吭,才能用TF.RESHAPE()
b2_restore = tf.reshape(b2_restore, [-1]) # b2是標(biāo)量,shape為[]析珊。要求tensor時(shí)必須給標(biāo)量擴(kuò)維度
print('TXT b2_restore:', sess.run(b2.assign(b2_restore)))
# 或者 寫成:
# print('TXT b2_restore:', sess.run(tf.assign(b2, b2_restore)))
print(sess.run(b2))
tf.summary()可以利用Tensorboard將網(wǎng)絡(luò)可視化羡鸥,在終端輸入summary的路徑:
$ tensorboard --logdir=/Users/username/PycharmProjects/firsttensorflow/readtf/data/tfboard
終端輸出為:TensorBoard 1.9.0 at http://pc-171-10-100-190.cm.vtr.net:6006 (Press CTRL+C to quit)
我們需要在網(wǎng)頁(yè)中輸入:http://localhost:6006
tensorboard默認(rèn)是在scalars(標(biāo)量)界面:
這里顯示的是損失函數(shù)在2000次訓(xùn)練過程中的變化趨勢(shì)。(因?yàn)槭请S機(jī)生成的數(shù)據(jù)忠寻,這里網(wǎng)絡(luò)性能并沒有考慮) 切換到GRAPHS界面惧浴,可以看到網(wǎng)絡(luò)結(jié)構(gòu):
可以雙擊每個(gè)scope查看里面的tensor flow和operation。
傳送:
- 神經(jīng)網(wǎng)絡(luò)激活函數(shù)
- tensorflow中常用的神將網(wǎng)絡(luò)函數(shù)
- 梯度下降算法
- checkpoint方法保存加載模型的Blog