'''
Created on 2017年5月21日 @author: weizhen
'''
#Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练Tensorflow模型进行了一些封装
#使其更便于使用。
#使用TFLearn实现分类问题
#为了方便数据处理,本程序使用了sklearn工具包,
#更多信息可以参考http://scikit-learn.org
from sklearn import model_selection
from sklearn import datasets
from sklearn import metrics
import tensorflow as tf
import numpy as np
from tensorflow.contrib.learn.python.learn.estimators.estimator import SKCompat
#导入TFLearn
learn = tf.contrib.learn #自定义模型,对于给定的输入数据(features)以及其对应的正确答案(target)
#返回在这些输入上的预测值、损失值以及训练步骤
def my_model(features,target):
#将预测的目标转换为one-hot编码的形式,因为共有三个类别,所以向量长度为3.经过转化后,第一个类别表示为(1,0,0)
#第二个为(0,1,0),第三个为(0,0,1)
target = tf.one_hot(target,3,1,0) #定义模型以及其在给定数据上的损失函数
logits = tf.contrib.layers.fully_connected(features,3,tf.nn.softmax)
loss = tf.losses.softmax_cross_entropy(target, logits) #创建模型的优化器,并得到优化步骤
train_op=tf.contrib.layers.optimize_loss(loss, #损失函数
tf.contrib.framework.get_global_step(), #获取训练步数并在训练时更新
optimizer='Adam', #定义优化器
learning_rate=0.01) #定义学习率
#返回在给定数据上的预测结果、损失值以及优化步骤
return tf.arg_max(logits, 1),loss,train_op #加载iris数据集,并划分为训练集合和测试集合
iris = datasets.load_iris()
x_train,x_test,y_train,y_test=model_selection.train_test_split(iris.data,
iris.target,
test_size=0.2,
random_state=0)
#将数据转化为float32格式
x_train,x_test = map(np.float32,[x_train,x_test])
#封装和训练模型,输出准确率
classifier=SKCompat(learn.Estimator(model_fn=my_model,model_dir="Models/model_1"))
#使用封装好的模型和训练数据执行100轮迭代
classifier.fit(x_train,y_train,steps=800) #使用训练好的模型进行结果预测
y_predicted=[i for i in classifier.predict(x_test)]
#计算模型的准确度
score=metrics.accuracy_score(y_test,y_predicted)
print("Accuracy: %.2f"%(score*100))

结果如下所示

2017-05-21 15:49:11.386435: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.386846: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.387271: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.387604: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.388450: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.388882: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.389180: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-21 15:49:11.389766: W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Accuracy: 100.00

85、使用TFLearn实现iris数据集的分类的更多相关文章

  1. 实验一 使用sklearn的决策树实现iris鸢尾花数据集的分类

    使用sklearn的决策树实现iris鸢尾花数据集的分类 要求: 建立分类模型,至少包含4个剪枝参数:max_depth.min_samples_leaf .min_samples_split.max ...

  2. 机器学习笔记2 – sklearn之iris数据集

    前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...

  3. 用Python实现支持向量机并处理Iris数据集

    SVM全称是Support Vector Machine,即支持向量机,是一种监督式学习算法.它主要应用于分类问题,通过改进代码也可以用作回归.所谓支持向量就是距离分隔面最近的向量.支持向量机就是要确 ...

  4. Iris数据集实战

    本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概 ...

  5. 从Iris数据集开始---机器学习入门

    代码多来自<Introduction to Machine Learning with Python>. 该文集主要是自己的一个阅读笔记以及一些小思考,小总结. 前言 在开始进行模型训练之 ...

  6. 做一个logitic分类之鸢尾花数据集的分类

    做一个logitic分类之鸢尾花数据集的分类 Iris 鸢尾花数据集是一个经典数据集,在统计学习和机器学习领域都经常被用作示例.数据集内包含 3 类共 150 条记录,每类各 50 个数据,每条记录都 ...

  7. iris数据集(.csv .txt)免费下载

    我看CSDN下载的iris数据集都需要币,我愿意免费共享,希望下载后的朋友们给我留个言 分享iris数据集(供学习使用): 链接: https://pan.baidu.com/s/1Knsp7zn-C ...

  8. Windows下mnist数据集caffemodel分类模型训练及测试

    1. MNIST数据集介绍 MNIST是一个手写数字数据库,样本收集的是美国中学生手写样本,比较符合实际情况,大体上样本是这样的: MNIST数据库有以下特性: 包含了60000个训练样本集和1000 ...

  9. R语言实现分层抽样(Stratified Sampling)以iris数据集为例

    R语言实现分层抽样(Stratified Sampling)以iris数据集为例 1.观察数据集 head(iris) Sampling)以iris数据集为例">  选取数据集中前6个 ...

随机推荐

  1. git add 添加错文件的撤销方法

    git add 添加 多余文件 这样的错误是由于,有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操作 ...

  2. C# Self Injector into non managed process

    Hey all, I'm gonna explain you how make a self injecting program in C#.I hope you guys thinks its us ...

  3. 三线SWD模式Jlink

    三线SWD模式Jlink   在公司实习,部门经理让我做一个USB-CAN的适配器. 在网上找资料,找到一个开源的USB-CAN的适配器的资料. 采用的是CP2102芯片实现USB转串口.STM32作 ...

  4. Egyptian Collegiate Programming Contest 2017 (ACM ECPC 2017) - original tests edition

    题目链接:https://codeforces.com/gym/101856 D. Dream Team 题意:n个点,让你连边成为一棵树,边权为顶点的GCD(u,v).求所有边权和的最大值. 思路: ...

  5. java多线程学习笔记(八)

    本节开始线程间通信: 使用wait/notify实现线程间通信 生产者/消费者模式的实现 方法join的使用 ThreadLocal类的使用 可以通过使用 sleep() 结合 while(true) ...

  6. UVA1442_Cave

    Cave 大致题意: 一个洞穴,已经i位置地面高度和顶的高度,要求在这个洞穴里面储蓄尽可能多的燃料,而且任何位置燃料不能碰到顶点 思路: 先从左往右扫描一下得出每一个点燃料能达到的最大高度,然后右边一 ...

  7. 深入理解__proto__ 、constructor和prototype的关系

    深入理解__proto__ .constructor和prototype的关系 2013-11-12 09:56 1390人阅读 评论(3) 收藏 举报  分类: 前端之Javascript(59)  ...

  8. 偏向锁,偏向线程id ,自旋锁

    理解锁的基础知识 如果想要透彻的理解Java锁的来龙去脉,需要先了解以下基础知识. 基础知识之一:锁的类型 锁从宏观上分类,分为悲观锁与乐观锁. 乐观锁 乐观锁是一种乐观思想,即认为读多写少,遇到并发 ...

  9. Docker 清理容器 log 日志

    原文 Docker 清理容器 log 日志 docker logs <容器ID> 是常用命令,来查看容器运行日志,但时间长了之后,就会发现越来越慢,log 太多了,这时就需要清理一下. 先 ...

  10. BUUCTF 梅花香自苦寒来

    梅花香自苦寒来 打开图片可以看到,在jpg后面有大量的数据,将它保存出来,可以看出是十六进制,将它转为ascii,写脚本 with open('hex.txt','r') as h: h=h.read ...