跟 Google 学 machineLearning [2] -- 关于 classifier.fit 的 warning
tensorfllow 的进化有点快。学习的很多例子已经很快的过时了,这里记录一些久的例子里被淘汰的方法,供后面参考。
我系统现在安装的是 tensorflow 1.4.1。
主要是使用了下面的代码后,出现 warning:
from tensorflow.contrib import learn myclassifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3) myclassifier.fit(x_train_array, y_train_array)
warning:
calling fit whith x is deprecated and will be removed after ...
解决方法,按照 warning 里的提示,搜了一下,发现,引入 SKCompat,并通过它来调用 classifier,即可使用原来的 fit 函数:
from tensorflow.contrib.learn.python import SKCompat
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
classifier = SKCompat( learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3) )
但是,使用 SKCompat 并没有真正的让 classifier 变成原来那个,只是改变了数据输入方式而已。从 pydoc 看到 SKCompat 共重写了三个函数:
1. fit,可以像原来一样,使用两个 array list 来进行数据填充。
2. predict,并不是原来的 predict,而是新 tensorflow.contrib.learn.Estimator 中的 predict,同样是使用 array 来喂数据。它的返回值也不是一个 array,反正我还没看懂到底它是个啥。
3. score,事实上就是新的 ensorflow.contrib.learn.Evaluable 中的 evaluate,同上,使用 array 来喂数据。
所以,即使使用过 SKCompat 之后,也还是没法用原来 predict 取得 y_test_prediction, 然后与 y_test 做比较。但是,你可以调用 score 得到一个 dic,其中 ["accuracy"]就是准确度评分。
accuracy_score = classifier.score(x_test, y_test)["accuracy"]
使用 predict ,要用下面的方法打印出可以看懂的结果(最新的手册上说 predict 的返回值是个 intertor,要用下面的方式取结果;我实验的结果是,我这里的返回值是个 dict, key 为 'classes'的就是我们要的内容了,具体的见最后的代码,这是我今天实验的最终代码;所以,tensor 又进化了):
y=classifier.predict(x_test)
predictions = list(p["predictions"] for p in itertools.islice(y, 6))
print("Predictions: {}".format(str(predictions)))
上面的 6 是 x_test 元素的个数。
===================================================
分割线
===================================================
新的 classifer 中,输入全部用的是 input_func 。这是上面报错的根本原因。
为什么要用 input_func 呢?官方给出的说法大概是,array 只适合小数据量时候使用。。。毕竟 array 的大小是有限的。这看起来完全没什么毛病。
官方给出的最新的方法(2017-12-25)是:
import numpy as np training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING, target_dtype=np.int, features_dtype=np.float32) train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(training_set.data)},
y=np.array(training_set.target),
num_epochs=None,
shuffle=True) classifier.train(input_fn=train_input_fn, steps=)
载入一个 datasets 之后,直接调用 estimator.inputs 中的 numpy.input_fn 来生成需要的 input_fn,后面给 classifier 喂数据,就喂这个 train_input_fn 就可以了。需要注意的是,这里传入的是函数 input_fn=train_input_fn, 而不是函数的返回值 input_fn=train_input_fn()。闭包?
或者,你想使用一个可以传递参数的 input_func,官方给出了三种方法(茴香豆的茴字也有三种写法,mmp):
A)写个 wrapper
def my_input_fn(data_set):
... def my_input_fn_training_set():
return my_input_fn(training_set) classifier.train(input_fn=my_input_fn_training_set, steps=)
B)使用 functools.partial
classifier.train(
input_fn=functools.partial(my_input_fn, data_set=training_set),
steps=)
C) 使用 lamda
classifier.train(input_fn=lambda: my_input_fn(training_set), steps=2000)
反正,在我看来,是越来越麻烦了,但是,现在它毕竟是一个有用的工具,还是要用的。
============
from sklearn import metrics
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.contrib import learn
import numpy as np
from tensorflow.contrib.learn.python import SKCompat
import itertools iris = learn.datasets.load_dataset('iris') print iris.data
print iris.target x_train, x_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42) feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)] classifier = SKCompat( learn.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3) ) classifier.fit(x_train, y_train, steps=200)
accuracy_score = classifier.score(x_test, y_test)["accuracy"]
print('Accuracy:{0:f}'.format(accuracy_score)) predictions=classifier.predict(x_test)['classes']
print("Predictions: {}".format(str(predictions)))
跟 Google 学 machineLearning [2] -- 关于 classifier.fit 的 warning的更多相关文章
- 跟 Google 学 machineLearning [1] -- hello sklearn
时至今日,我才发现 machineLearning 的应用门槛已经被降到了这么低,简直唾手可得.我实在找不到任何理由不对它进入深入了解.如标题,感谢 Google 为这项技术发展作出的贡献.当然,可能 ...
- Google机器学习课程基于TensorFlow : https://developers.google.cn/machine-learning/crash-course
Google机器学习课程基于TensorFlow : https://developers.google.cn/machine-learning/crash-course https ...
- 学习笔记之Machine Learning Crash Course | Google Developers
Machine Learning Crash Course | Google Developers https://developers.google.com/machine-learning/c ...
- Google机器学习笔记(七)TF.Learn 手写文字识别
转载请注明作者:梦里风林 Google Machine Learning Recipes 7 官方中文博客 - 视频地址 Github工程地址 https://github.com/ahangchen ...
- 机器学习入门 - Google的机器学习速成课程
1 - MLCC 通过机器学习,可以有效地解读数据的潜在含义,甚至可以改变思考问题的方式,使用统计信息而非逻辑推理来处理问题. Google的机器学习速成课程(MLCC,machine-learnin ...
- 【机器学习】Google机器学习工程的43条最佳实践
https://blog.csdn.net/ChenVast/article/details/81449509 本文档旨在帮助那些掌握机器学习基础知识的人从Google机器学习的最佳实践中获益.它提供 ...
- 使用Google Colab训练神经网络(二)
Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果.它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行.Colaborat ...
- 【阿里聚安全·安全周刊】Google“手枪”替换 | 伊朗中央银行禁止加密货币
本周七个关键词:Google"手枪"替换丨IOS 漏洞影响工业交换机丨伊朗中央银行禁止加密货币丨黑客针对医疗保健丨付费DDoS攻击丨数据获利的8种方式丨MySQL 8.0 正式版 ...
- google学习
https://developers.google.com/machine-learning/crash-course/ https://developers.google.com/machine-l ...
随机推荐
- 递归搜寻NSString中重复的文本
递归搜寻NSString中重复的文本 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 StringRange 项目 // // ...
- 波吉亚家族第一季/全集The Borgias 1迅雷下载
波吉亚家族 第一季 The Borgias Season 1 (2011)本季看点:<波吉亚家族>是一个非常复杂的故事,是现代人描绘这个臭名昭著的王朝家族过往历史的一副有趣又坦率的肖像画. ...
- 《ASP.NET Web API 2框架揭秘》
<ASP.NET Web API 2框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121235368 上架时间:2014-7-5 出版日期:2014 年7 ...
- Orchard之模版开发
生成新模版之后(参看:Orchard之生成新模板),紧接着就是模版开发了. 一:开发必备之 Shape Tracing 到了这一步,非常依赖一个工具,当然,它也是 Orchard 项目本身的一个 Mo ...
- linux rename命令批量修改文件名
修改文件名可以用mv命令来实现 mv filename1 filename2 1 但如果批量修改还是使用rename命令更为方便 现在我们有a b c d 四个文件 增加后缀 rename 's/$/ ...
- docker logs-查看docker容器日志
只限制最后100条的日志,并持续更新日志显示 docker logs -f --tail= CONTAINER_ID docker logs -f --tail CONTAINER_ID http ...
- 网关局域网通信协议V2.0
http://docs.opencloud.aqara.cn/development/gateway-LAN-communication/ https://github.com/aqara/openc ...
- The Art of Deception
前言 一些黑客毁坏别人的文件甚至整个硬盘,他们被称为电脑狂人(crackers)或计算机破坏者(vandals).另一些新手省去学习技术的麻烦,直接下载黑客工具侵入别人的计算机,这些人被称为脚本小子( ...
- GROUP BY中ROLLUP/CUBE/GROUPING/GROUPING SETS使用示例
oracle group by中rollup和cube的区别: Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句.CUBE ROLLUP 是用于统计数据的. 实验 ...
- scrapy框架系列 (4) Scrapy Shell
Scrapy Shell Scrapy终端是一个交互终端,我们可以在未启动spider的情况下尝试及调试代码,也可以用来测试XPath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取的数据 ...