tensorflow搭建多层感知器网络(MLP)

最近在学tensorflow,在调试的过程中无意实现了一个小的网络搭建,感觉代码还比较清晰,就记录一下,希望能帮助初学的同学。
使用的是python 3.0, tf 1.0

下面的代码搭建一个多层感知器网络,示例学的是简单的函数如 f=2*x+3yf=x^2+3*y,然后还包括loss可视化,最终会保存在文件夹visual_dir
要想查看可视化的结果

tensorboard --logdir=your_path/visual_dir 

注意如果再windows 下用tensorboard,需要先定位到盘符比如你保存的目录在e盘

e:
tensorboard --logdir=your_path/visual_dir 

另外网络有一些没有用的变量,希望大家进行辨别。

# -*- coding:utf-8 -* 
import os ; import sys 
os.chdir( os.path.split( os.path.realpath( sys.argv[0] ) )[0] ) 

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from tensorflow import flags
import numpy as np
import numpy
from numpy import random as nr

true=True
false=False
FLAGS=flags.FLAGS
FLAGS.visual_trian=true
FLAGS.visual_savedir="visual_dir/"

input_szie=2 
output_szie=1

# 每一层节点个个数
node_num=[200,200,200,200,output_szie]
layer_num=5

active_types=[0,0,1,1,1]
active_type=1

active_func=["sigmoid","liner"]

def sigmoid(x):
    out=tf.sigmoid(x)
    return out
    
def connected(x,w,b,scope):
    out=tf.nn.xw_plus_b(x,w,b,name=scope)
    if active_func[active_type]=="sigmoid":
        out=sigmoid(out)
    return out

def get_w_b(indim,outdim,name):
    args = [0., 1e-2, [indim,outdim]]
    w_init = np.random.normal(*args)
    args = [0., 1e-2, [outdim]]
    b_init = np.random.normal(*args)
    w_init=tf.constant_initializer(w_init)
    b_init=tf.constant_initializer(b_init)
    
    w=tf.get_variable(name+"_w",
              shape=[indim,outdim],
              dtype=tf.float32,
              initializer=w_init)
              
    b=tf.get_variable(name+"_b",
              shape=[outdim],
              dtype=tf.float32,
              initializer=b_init)
              
    return [w,b]

def compute_loss(truth,net_out):
    diff = truth - net_out
    loss = tf.nn.l2_loss(diff)
    return loss

def get_input_target():
    # 目前我这里的函数是 f=x^2+3*y x,y要 归一化到 0-1 才行
    # 否则很难训的
    max_batches = 50000
    batch_num = 64

    batch_cnt = 0
    seen = 0
    hd_data=[None]*2
    while batch_cnt < max_batches:
        hd_data[0]=[]
        hd_data[1]=[]
        for i in range(batch_num):
            x, y = nr.rand(2) #* 1000
            f = x * x + 3 * y
            #x += nr.randn()
            #y += nr.randn()
            hd_data[0] += [ [x,y] ]
            hd_data[1] += [ [f] ]

        fimg_batch = numpy.array(hd_data[0])
        labels_batch = numpy.array(hd_data[1])

        datum = dict({"input_data": fimg_batch, "truth_target": labels_batch})

        batch_cnt += 1
        seen += batch_num
        yield [seen, datum]
    
input_data=tf.placeholder(tf.float32, [None,input_szie], 'input_data')
truth_target=tf.placeholder(tf.float32, [None,output_szie], 'truth_target')

# 开始构建网络
x=input_data
pre_dim=input_szie

for i in range(layer_num):
    active_type=active_types[i]
    now_dim=node_num[i]
    w,b=get_w_b(pre_dim,now_dim,str(i+1))
    x=connected(x,w,b,"conn-"+str(i+1))
    pre_dim=now_dim
net_out=x
loss_op=compute_loss(truth_target,net_out)

optimizer=tf.train.AdamOptimizer(0.001)
#optimizer=tf.train.GradientDescentOptimizer(0.001)
gradient =optimizer.compute_gradients(loss_op)
train_op = optimizer.apply_gradients(gradient)

sess=tf.Session()
init_op = tf.global_variables_initializer()
init_local_op = tf.local_variables_initializer()
sess.run(init_op)
sess.run(init_local_op)


loss_mva = None
placeholders=dict()
placeholders["input_data"] = input_data
placeholders["truth_target"] = truth_target

''' 可视化有关
'''
if FLAGS.visual_trian:
    summary_writer = tf.summary.FileWriter( \
        FLAGS.visual_savedir, tf.get_default_graph())
    tf.summary.scalar('sse_l2', loss_op)
    merged = tf.summary.merge_all()

batches = get_input_target()
for i, (seen,datum) in  enumerate(batches):
    # 各种 holder的填入
    feed_dict = {
        placeholders[key]: datum[key] for key in datum }

    fetches = [train_op, loss_op ]
    # 加上可视信息
    if FLAGS.visual_trian == True:
        merged_id=len(fetches)
        fetches+=[merged]

    fetched = sess.run(fetches, feed_dict)

    if FLAGS.visual_trian == True:
        summary_writer.add_summary(fetched[merged_id], i)
        
    loss = fetched[1]
    
    grad = sess.run(gradient, feed_dict)
    pre_grad=numpy.max([numpy.max(x) for x in grad])
    print("max gradient: ", pre_grad)

    if loss_mva is None: loss_mva = loss
    loss_mva = .9 * loss_mva + .1 * loss

    form = 'step {} - seen {} - loss {} - loss_mva {}'
    print(form.format(i, seen, loss, loss_mva))
        

你可能感兴趣的:(深度学习算法/有关工具)