TensorFlow,tensorboard学习

边学边总结。

背景:

在基于WiFi指纹数据库的定位算法中,为了减小计算量,需要将指纹数据库进行分区。数据库形式如下所示:

                                                   (Rss_1,Rss_2,.....Rss_n,(x_j,y_j),part_j)

其中Rss_i为第i个AP的信号强度,(x_j,y_j)是位置坐标,part_j是该位置所属分区号。

本文基于室内空间特征将指纹库分为了四个区,然后基于TensorFlow框架,搭建神经网络进行训练,并用tensorboard将数据可视化。

输入为:

                                   (Rss_1,Rss_2,.....Rss_n)

输出为:

                                                   part_i

加载数据:

#coding: utf8                 
from numpy import genfromtxt, zeros

def load_data(filename):
    
    with open(filename, 'rb') as dataset:    
        X = genfromtxt(dataset,delimiter=',',usecols=range(28))
    with open(filename, 'rb') as dataset: 
        y = genfromtxt(dataset,delimiter=',',usecols=(30))
    
    result=[ver(j) for j in y]
    result = [x.T[0] for x in result]
   
    a = X.min()
    b = X.max()
    X -= a              # 对 X 进行归一化处理
    X /= b
  
    my_data = list(zip(X,result))      
    return(my_data)

def load_data_wrapper():
    
    training_data = load_data(r'C:\Users\hyl\Desktop\Second_158\Classification_Model\30columns\train_6320_30.csv')
    test_data = load_data(r'C:\Users\hyl\Desktop\Second_158\Classification_Model\30columns\test_1550_301.csv')  
    return (training_data,test_data)

def ver(j):
    j = int(j)
    e = zeros((4,1))
    e[j]=1.0
    return e

搭建网络:

# -*- coding: utf-8 -*- 
import datas_loader as dl
import tensorflow as tf
from random import shuffle

traind,testd = dl.load_data_wrapper()  # 载入数据集
# trd = [x[0] for x in traind]           # 训练数据   6320*28
# trd_label = [x[1] for x in traind]     # 训练数据的标签   6320*4
ted = [x[0] for x in testd]            # 测试数据  1550*28
ted_label = [x[1] for x in testd]      # 测试数据的标签  1550*4

batch_size = 40                 # 每个批次的大小
n_batch = len(traind)//batch_size  # 总批次
# 可视化函数
def variable_summaries(var):          
    with tf.name_scope("summaries"):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean',mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var-mean)))
        tf.summary.scalar('stddev',stddev) # 标准差
        tf.summary.scalar('max',tf.reduce_max(var)) # 最大值
        tf.summary.histogram('histogram',var)  # 直方图

# 定义两个 placeholder
with tf.name_scope("input"):
    x = tf.placeholder(tf.float32,[None,28])
    y = tf.placeholder(tf.float32,[None,4])
# 创建网络
with tf.name_scope('layer'):
    with tf.name_scope('wigths'):
        w1 = tf.Variable(tf.zeros([28,4]))
        variable_summaries(w1)
    with tf.name_scope('biases'):
        b1 = tf.Variable(tf.zeros([4]))
        variable_summaries(b1)
    with tf.name_scope('softmax'):
        z = tf.nn.softmax(tf.matmul(x,w1)+b1)
# 二次代价函数
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=z))
    tf.summary.scalar('loss',loss)
# 训练
train_step = tf.train.GradientDescentOptimizer(0.005).minimize(loss)  
init = tf.global_variables_initializer()  # 初始化
# 结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(z,1))
# 求准确率
with tf.name_scope('accuracy'):
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    tf.summary.scalar('accuracy',accuracy)
# 合并所有的summary
merged = tf.summary.merge_all()
# 定义图
with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('logs/',sess.graph)
    for epoch in range(15):
        shuffle(traind)                        # 将训练数据顺序打乱
        trd = [x[0] for x in traind]           # 训练数据   6320*28
        trd_label = [x[1] for x in traind]     # 训练数据的标签   6320*4
        for batch in range(n_batch-1):       
            trd_i = trd[batch_size*batch:batch_size*(batch+1)]
            trd_label_i = trd_label[batch_size*batch:batch_size*(batch+1)]
            # merged的返回值存在summary中
            summary,_ = sess.run([merged,train_step],feed_dict={x:trd,y:trd_label}) 
         
        writer.add_summary(summary,epoch)  # 将summary写入到文件中  
        acc = sess.run(accuracy,feed_dict={x:ted,y:ted_label})  # 测试精度
        print('Iter'+str(epoch)+',Accuracy: '+str(acc))
print ('end!')
           
训练结果与可视化:


可以看到准确度达到了95.7%。

用tensorboard查看详细数据:




数据流:


用TensorFlow搭建网络方便多了。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值