TensorFlow高层次机器学习API (tf.contrib.learn)
TensorFlow高层次机器学习API (tf.contrib.learn)
1.tf.contrib.learn.datasets.base.load_csv_with_header 加载csv格式数据
2.tf.contrib.learn.DNNClassifier 建立DNN模型(classifier)
3.classifer.fit 训练模型
4.classifier.evaluate 评价模型
5.classifier.predict 预测新样本
完整代码:

1 from __future__ import absolute_import
2 from __future__ import division
3 from __future__ import print_function
4
5 import tensorflow as tf
6 import numpy as np
7
8 # Data sets
9 IRIS_TRAINING = "iris_training.csv"
10 IRIS_TEST = "iris_test.csv"
11
12 # Load datasets.
13 training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
14 filename=IRIS_TRAINING,
15 target_dtype=np.int,
16 features_dtype=np.float32)
17 test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
18 filename=IRIS_TEST,
19 target_dtype=np.int,
20 features_dtype=np.float32)
21
22 # Specify that all features have real-value data
23 feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
24
25 # Build 3 layer DNN with 10, 20, 10 units respectively.
26 classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
27 hidden_units=[10, 20, 10],
28 n_classes=3,
29 model_dir="/tmp/iris_model")
30
31 # Fit model.
32 classifier.fit(x=training_set.data,
33 y=training_set.target,
34 steps=2000)
35
36 # Evaluate accuracy.
37 accuracy_score = classifier.evaluate(x=test_set.data,
38 y=test_set.target)["accuracy"]
39 print('Accuracy: {0:f}'.format(accuracy_score))
40
41 # Classify two new flower samples.
42 new_samples = np.array(
43 [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
44 y = list(classifier.predict(new_samples, as_iterable=True))
45 print('Predictions: {}'.format(str(y)))

结果:
Accuracy:0.966667
TensorFlow高层次机器学习API (tf.contrib.learn)的更多相关文章
- TensorFlow高级API(tf.contrib.learn)及可视化工具TensorBoard的使用
一.TensorFlow高层次机器学习API (tf.contrib.learn) 1.tf.contrib.learn.datasets.base.load_csv_with_header 加载cs ...
- tf.contrib.learn.preprocessing.VocabularyProcessor()
tf.contrib.learn.preprocessing.VocabularyProcessor (max_document_length, min_frequency=0, vocabulary ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
- tf.contrib.layers.fully_connected参数笔记
tf.contrib.layers.fully_connected 添加完全连接的图层. tf.contrib.layers.fully_connected( inputs, num_ou ...
- TensorFlow——tf.contrib.layers库中的相关API
在TensorFlow中封装好了一个高级库,tf.contrib.layers库封装了很多的函数,使用这个高级库来开发将会提高效率,卷积函数使用tf.contrib.layers.conv2d,池化函 ...
- tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()
在分析训练代码的时候,遇到了,tf.contrib.crf.crf_log_likelihood,这个函数,于是想简单理解下: 函数的目的:使用crf 来计算损失,里面用到的优化方法是:最大似然估计 ...
- tensorflow教程:tf.contrib.rnn.DropoutWrapper
tf.contrib.rnn.DropoutWrapper Defined in tensorflow/python/ops/rnn_cell_impl.py. def __init__(self, ...
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- 关于tensorflow里面的tf.contrib.rnn.BasicLSTMCell 中num_units参数问题
这里的num_units参数并不是指这一层油多少个相互独立的时序lstm,而是lstm单元内部的几个门的参数,这几个门其实内部是一个神经网络,答案来自知乎: class TRNNConfig(obje ...
随机推荐
- ftk学习记(首篇)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 非常早之前就知道ftk了,当时主要是由于买了李先静的书,所以知道了这么一个项目.由于对这样的g ...
- UVA - 11722 Joining with Friend 几何概率
Joining with Friend You are going from Dhaka to Chittagong by train and you ...
- 堆排序(Swift版本)
一:什么是堆? 堆可视为 "以数组方式存储的一棵完全二叉树" 堆又分为最大堆和最小堆, 最大堆就是对于整个二叉树中的每一个节点都满足:节点的键值比其左右子节点的键值都要大,对应的 ...
- Hello The World! —— 致我们无悔的IT之旅
感谢IT,让我有了这么可爱活泼的伙伴. 有了KsCla,Coming,lhx_QAQ,tututu,AB_ever,Fat-zhang,wka,lhm这些伙伴神犇的陪伴,我的OI历程不至于那么枯燥无味 ...
- [MySQL] 查询一段时间记录
24小时内记录(即86400秒) $sql="SELECT video_id,count(id)as n FROM `rec_down` WHERE UNIX_TIMESTAMP(NOW() ...
- C#6.0新增功能
C# 6.0 版本包含许多可提高开发人员工作效率的功能. 此版本中的功能包括: 只读自动属性: 可以创建只能在构造函数中设置的只读自动属性. 自动属性初始值设定项: 可以编写初始化表达式来设置自动属性 ...
- 洛谷P3357 最长k可重线段集问题(费用流)
题目描述 给定平面 x-O-yx−O−y 上 nn 个开线段组成的集合 II ,和一个正整数 kk .试设计一个算法,从开线段集合 II 中选取出开线段集合 S\subseteq IS⊆I ,使得在 ...
- 子线程创建AlertDialog错误
Can't create handler inside thread that has not called Looper.prepare()
- 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...
- Cookie和Session有什么区别
1. 由于HTTP协议是无状态的协议,所以服务端需要记录用户的状态时,就需要用某种机制来识别具体的用户,这个机制就是Session. 典型的场景比如购物车,当你点击下单按钮时,由于HTTP协议无状 ...