一.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

 

二.在tf.contrib.learn中创建input函数(输入预处理函数)

格式:

def my_input_fn():

  # Preprocess your data here...

  # ...then return 1) a mapping of feature columns to Tensors with
  # the corresponding feature data, and 2) a Tensor containing labels
  return feature_cols, labels

完整代码:

 1 #  Copyright 2016 The TensorFlow Authors. All Rights Reserved.
 2 #
 3 #  Licensed under the Apache License, Version 2.0 (the "License");
 4 #  you may not use this file except in compliance with the License.
 5 #  You may obtain a copy of the License at
 6 #
 7 #   http://www.apache.org/licenses/LICENSE-2.0
 8 #
 9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14 """DNNRegressor with custom input_fn for Housing dataset."""
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 
20 import itertools
21 
22 import pandas as pd
23 import tensorflow as tf
24 
25 tf.logging.set_verbosity(tf.logging.INFO)
26 
27 COLUMNS = ["crim", "zn", "indus", "nox", "rm", "age",
28            "dis", "tax", "ptratio", "medv"]
29 FEATURES = ["crim", "zn", "indus", "nox", "rm",
30             "age", "dis", "tax", "ptratio"]
31 LABEL = "medv"
32 
33 
34 def input_fn(data_set):
35   feature_cols = {k: tf.constant(data_set[k].values) for k in FEATURES}
36   labels = tf.constant(data_set[LABEL].values)
37   return feature_cols, labels
38 
39 
40 def main(unused_argv):
41   # Load datasets
42   training_set = pd.read_csv("boston_train.csv", skipinitialspace=True,
43                              skiprows=1, names=COLUMNS)
44   test_set = pd.read_csv("boston_test.csv", skipinitialspace=True,
45                          skiprows=1, names=COLUMNS)
46 
47   # Set of 6 examples for which to predict median house values
48   prediction_set = pd.read_csv("boston_predict.csv", skipinitialspace=True,
49                                skiprows=1, names=COLUMNS)
50 
51   # Feature cols
52   feature_cols = [tf.contrib.layers.real_valued_column(k)
53                   for k in FEATURES]
54 
55   # Build 2 layer fully connected DNN with 10, 10 units respectively.
56   regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
57                                             hidden_units=[10, 10],
58                                             model_dir="/tmp/boston_model")
59 
60   # Fit
61   regressor.fit(input_fn=lambda: input_fn(training_set), steps=5000)
62 
63   # Score accuracy
64   ev = regressor.evaluate(input_fn=lambda: input_fn(test_set), steps=1)
65   loss_score = ev["loss"]
66   print("Loss: {0:f}".format(loss_score))
67 
68   # Print out predictions
69   y = regressor.predict(input_fn=lambda: input_fn(prediction_set))
70   # .predict() returns an iterator; convert to a list and print predictions
71   predictions = list(itertools.islice(y, 6))
72   print("Predictions: {}".format(str(predictions)))
73 
74 if __name__ == "__main__":
75   tf.app.run()
inputfunc_contrib_learn.py

相关文章:

  • 2021-11-22
  • 2022-12-23
  • 2021-06-23
  • 2021-10-04
  • 2021-10-14
  • 2022-12-23
  • 2021-12-04
  • 2021-05-14
猜你喜欢
  • 2021-08-03
  • 2021-10-14
  • 2021-07-01
  • 2021-03-31
  • 2021-05-16
  • 2022-01-12
  • 2022-12-23
相关资源
相似解决方案