专栏首页人工智能LeadAITensorflow实践:用神经网络训练分类器

Tensorflow实践:用神经网络训练分类器

任务: 使用tensorflow训练一个神经网络作为分类器,分类的数据点如下:

螺旋形数据点

原理

数据点一共有三个类别,而且是螺旋形交织在一起,显然是线性不可分的,需要一个非线性的分类器。这里选择神经网络。

输入的数据点是二维的,因此每个点只有x,y坐标这个原始特征。这里设计的神经网络有两个隐藏层,每层有50个神经元,足够抓住数据点的高维特征(实际上每层10个都够用了)。最后输出层是一个逻辑回归,根据隐藏层计算出的50个特征来预测数据点的分类(红、黄、蓝)。

一般训练数据多的话,应该用随机梯度下降来训练神经网络,这里训练数据较少(300),就直接批量梯度下降了。

# 导入包、初始化
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf  %matplotlib inline plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray
'# 生成螺旋形的线形不可分数据点
np.random.seed(0) N = 100 # 每个类的数据个数
D = 2 # 输入维度
K = 3 # 类的个数
X = np.zeros((N*K,D)) num_train_examples = X.shape[0] y = np.zeros(N*K, dtype='uint8')
for j in xrange(K):   ix = range(N*j,N*(j+1))   r = np.linspace(0.0,1,N) # radius   t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta   X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]   y[ix] = j fig = plt.figure() plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim([-1,1]) plt.ylim([-1,1])

螺旋形数据点

打印输出输入X和label的shape

num_label = 3
labels = (np.arange(num_label) == y[:,None]).astype(np.float32) labels.shape
(300, 3)
X.shape
(300, 2)

用tensorflow构建神经网络

import math  N = 100 # 每个类的数据个数
D = 2 # 输入维度
num_label = 3 # 类的个数
num_data = N * num_label hidden_size_1 = 50
hidden_size_2 = 50
beta = 0.001 # L2 正则化系数
learning_rate = 0.1 # 学习速率
labels = (np.arange(num_label) == y[:,None]).astype(np.float32)  graph = tf.Graph()
with graph.as_default():     x = tf.constant(X.astype(np.float32))     tf_labels = tf.constant(labels)         # 隐藏层1     hidden_layer_weights_1 = tf.Variable(     tf.truncated_normal([D, hidden_size_1], stddev=math.sqrt(2.0/num_data)))     hidden_layer_bias_1 = tf.Variable(tf.zeros([hidden_size_1]))         # 隐藏层2     hidden_layer_weights_2 = tf.Variable(     tf.truncated_normal([hidden_size_1, hidden_size_2], stddev=math.sqrt(2.0/hidden_size_1)))     hidden_layer_bias_2 = tf.Variable(tf.zeros([hidden_size_2]))         # 输出层     out_weights = tf.Variable(     tf.truncated_normal([hidden_size_2, num_label], stddev=math.sqrt(2.0/hidden_size_2)))     out_bias = tf.Variable(tf.zeros([num_label]))          z1 = tf.matmul(x, hidden_layer_weights_1) + hidden_layer_bias_1     h1 = tf.nn.relu(z1)          z2 = tf.matmul(h1, hidden_layer_weights_2) + hidden_layer_bias_2     h2 = tf.nn.relu(z2)          logits = tf.matmul(h2, out_weights) + out_bias         # L2正则化     regularization = tf.nn.l2_loss(hidden_layer_weights_1) + tf.nn.l2_loss(hidden_layer_weights_2) + tf.nn.l2_loss(out_weights)     loss = tf.reduce_mean(         tf.nn.softmax_cross_entropy_with_logits(labels=tf_labels, logits=logits) + beta * regularization)           optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)          train_prediction = tf.nn.softmax(logits)      weights = [hidden_layer_weights_1, hidden_layer_bias_1, hidden_layer_weights_2, hidden_layer_bias_2, out_weights, out_bias]  

上一步相当于搭建了神经网络的骨架,现在需要训练。每1000步训练,打印交叉熵损失和正确率。

num_steps = 50000

def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0])def relu(x): return np.maximum(0,x)

with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() print('Initialized')

for step in range(num_steps): _, l, predictions = session.run([optimizer, loss, train_prediction]) if (step % 1000 == 0): print('Loss at step %d: %f' % (step, l)) print('Training accuracy: %.1f%%' % accuracy( predictions, labels)) w1, b1, w2, b2, w3, b3 = weights

# 显示分类器 h = 0.02 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = np.dot(relu(np.dot(relu(np.dot(np.c_[xx.ravel(), yy.ravel()], w1.eval()) + b1.eval()), w2.eval()) + b2.eval()), w3.eval()) + b3.eval() Z = np.argmax(Z, axis=1) Z = Z.reshape(xx.shape) fig = plt.figure() plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max())

Initialized

Loss at step 0: 1.132545
Training accuracy: 43.7%
Loss at step 1000: 0.257016
Training accuracy: 94.0%
Loss at step 2000: 0.165511
Training accuracy: 98.0%
Loss at step 3000: 0.149266
Training accuracy: 99.0%
Loss at step 4000: 0.142311
Training accuracy: 99.3%
Loss at step 5000: 0.137762
Training accuracy: 99.3%
Loss at step 6000: 0.134356
Training accuracy: 99.3%
Loss at step 7000: 0.131588
Training accuracy: 99.3%
Loss at step 8000: 0.129299
Training accuracy: 99.3%
Loss at step 9000: 0.127340
Training accuracy: 99.3%
Loss at step 10000: 0.125686
Training accuracy: 99.3%
Loss at step 11000: 0.124293
Training accuracy: 99.3%
Loss at step 12000: 0.123130
Training accuracy: 99.3%
Loss at step 13000: 0.122149
Training accuracy: 99.3%
Loss at step 14000: 0.121309
Training accuracy: 99.3%
Loss at step 15000: 0.120542
Training accuracy: 99.3%
Loss at step 16000: 0.119895
Training accuracy: 99.3%
Loss at step 17000: 0.119335
Training accuracy: 99.3%
Loss at step 18000: 0.118836
Training accuracy: 99.3%
Loss at step 19000: 0.118376
Training accuracy: 99.3%
Loss at step 20000: 0.117974
Training accuracy: 99.3%
Loss at step 21000: 0.117601
Training accuracy: 99.3%
Loss at step 22000: 0.117253
Training accuracy: 99.3%
Loss at step 23000: 0.116887
Training accuracy: 99.3%
Loss at step 24000: 0.116561
Training accuracy: 99.3%
Loss at step 25000: 0.116265
Training accuracy: 99.3%
Loss at step 26000: 0.115995
Training accuracy: 99.3%
Loss at step 27000: 0.115750
Training accuracy: 99.3%
Loss at step 28000: 0.115521
Training accuracy: 99.3%
Loss at step 29000: 0.115310
Training accuracy: 99.3%
Loss at step 30000: 0.115111
Training accuracy: 99.3%
Loss at step 31000: 0.114922
Training accuracy: 99.3%
Loss at step 32000: 0.114743
Training accuracy: 99.3%
Loss at step 33000: 0.114567
Training accuracy: 99.3%
Loss at step 34000: 0.114401
Training accuracy: 99.3%
Loss at step 35000: 0.114242
Training accuracy: 99.3%
Loss at step 36000: 0.114086
Training accuracy: 99.3%
Loss at step 37000: 0.113933
Training accuracy: 99.3%
Loss at step 38000: 0.113785
Training accuracy: 99.3%
Loss at step 39000: 0.113644
Training accuracy: 99.3%
Loss at step 40000: 0.113504
Training accuracy: 99.3%
Loss at step 41000: 0.113366
Training accuracy: 99.3%
Loss at step 42000: 0.113229
Training accuracy: 99.3%
Loss at step 43000: 0.113096
Training accuracy: 99.3%
Loss at step 44000: 0.112966
Training accuracy: 99.3%
Loss at step 45000: 0.112838
Training accuracy: 99.3%
Loss at step 46000: 0.112711
Training accuracy: 99.3%
Loss at step 47000: 0.112590
Training accuracy: 99.3%
Loss at step 48000: 0.112472
Training accuracy: 99.3%
Loss at step 49000: 0.112358
Training accuracy: 99.3%

分类器.png

本文分享自微信公众号 - 人工智能LeadAI(atleadai)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-02-11

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 流式数据 | 天天在做大数据,你的时间都花在哪了

    大数据做了这许多年,有没有问过自己,大数据中,工作量最大和技术难度最高的,分别是什么呢? 01 大数据时代 我每天都在思考,思考很重要,是一个消化和不断深入的过...

    用户1332428
  • 数据预处理 | 机器学习之特征工程

    作者:苏小保(jacksu) 华为工程师 擅长分布式系统、大数据、机器学习。github地址:https://github.com/jacksu 通过特征提取,...

    用户1332428
  • TensorFlow从0到1 | 第十章:NN基本功:反向传播的推导

    上一篇 9 “驱魔”之反传大法 引出了反向传播算法,强调了其在神经网络中的决定性地位,并在最后窥探了算法的全貌。本篇将详细的讨论算法各方面的细节。尽管我们都能...

    用户1332428
  • 计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)

    java程序员在实际的开发中会遇到很多的单位换算问题。今天我给大家带来的是关于计算机硬盘大小的换算。多数情况下,一般要求b,kb,mb,gb,tb,pb之间的大...

    业余草
  • Azure Datalake Store Gen2:微软放了大杀器还是做了大傻逼?

    2018年6月,微软正式宣布了Azure Datalake Store Gen2,第二代数据湖文件系统。熟悉微软数据湖的知道,这个东西是Raghu搞出来的,用于...

    用户1564362
  • 教程 | 萌物生成器:如何使用四种GAN制造猫图

    选自WordPress 作者:Alexia Jolicoeur-Martineau 机器之心编译 参与:李泽南 生成对抗生成网络(GAN)被认为是近年来机器学习...

    机器之心
  • Python快速入门

    最近在很多地方都可以看到Python的身影,尤其在人工智能等科学领域,其丰富的科学计算等方面类库无比强大。很多身边的哥们也提到Python非常的简洁方便,比如用...

    用户1216676
  • OpenFlow网络中的路由服务

    这里,所谓OpenFlow网络指的是相互连接的一组OpenFlow交换机的集合,并且这些交换机全部置于一个OpenFlow Controller或一个OpenF...

    SDNLAB
  • vue.js聊天IM系统|聊天室|群聊

    基于vue+vuex+vue-router+webpack2.0+es6+wcPop+iconfont等技术开发的仿微信界面聊天室,之前使用h5开发过一版h5聊...

    andy2018
  • layui小问题

    解决办法:把<button></button>标签改为<input type="button">或者在<button>中添加属性 type="button"

    生南星

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动