85、使用TFLearn实现iris数据集的分类
2017-10-13 11:13
597 查看
''' 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数据集的分类
- 使用Scikit-learn实现基于内存的协同过滤算法(使用movieLens数据集)
- iris数据集 决策树实现分类并画出决策树
- 数据挖掘-K-近邻分类器-Iris数据集分析-使用K-近邻分类器进行分类预测(四)
- TF之AE:AE实现TF自带数据集AE的encoder之后decoder之前的非监督学习分类—Jason niu
- TF:TF分类问题之MNIST手写50000数据集实现87.4%准确率识别:SGD法+softmax法+cross_entropy法—Jason niu
- Python 3实现k-邻近算法以及 iris 数据集分类应用
- 使用 scikit-learn 实现多类别及多标签分类算法
- 使用不同的SVM对iris数据集进行分类并绘出结果
- tensorflow 学习专栏(六):使用卷积神经网络(CNN)在mnist数据集上实现分类
- 使用sklearn中的Iris植物分类数据集进行特征检验与预测分析
- Java实现LSTM和GRU做分类(以IRIS数据集为例)
- 神经网络与深度学习 1.6 使用Python实现基于梯度下降算法的神经网络和MNIST数据集的手写数字分类程序
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- TF之RNN:基于顺序的RNN分类案例对手写数字图片mnist数据集实现高精度预测—Jason niu
- c#神经网络,实现对Iris数据集进行分类
- 数据挖掘-oneR算法-Iris数据集分析-使用oneR算法进行分类预测(五)
- python 实现 knn分类算法 (Iris 数据集)
- 使用hibernate实现树形结构无限级分类
- 使用jQuery插件filtrify实现的超酷动态标签分类摩托车新款展示