Tf.contrib is the tensorflow inter high lever API for handle kinds of machine learning algrothms like regression SVM randomforest as so on , this time I will give a code review from the official guideline, this realy works for us to run DNN if you want, so up to now  I could run the CNN and DNN , Ihope this will help some of you who wwonder to have a deep dig on ML.

the whole process is divied into three steps:

  • fit with data and get the model
    evaluate with the test data
    predict with new example
#import the data and tensorflow

import tensorflow as tf 
import numpy as np 

# please download the data and put in your local folder iris_train  and iris_test
# load the data( you also could use the load_iris())

trainset=tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TRAIN, ##读取数据,注意设定类型 target_dtype 不能错

feature_columns=[tf.contrib.learn.layers.real_valued_columns('';dimension=4)]## 设定feature 的属性值

cassifier=tf.contrib.learn.DNNClassifier(feature_columns=feature_columns, hidden_units=[10 20 10],n_classes=3,model_dir='./iris_model')## 主要的就是这个,tf的DNN分类器,当然是全连接的, 第一个参数就是读取属性,第二是指定隐藏层数,第三个输出变量,第四个输出模型地址,,steps=200) # 用数据逼近模型

classifier.evaluate(,,steps=200)# 用数据去检测模型


# predict the new example  新的列子来测试模型预测输出

new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = classifier.predict(new_samples)
print('Predictions: {}'.format(str(y)))

[1 2]