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"""Example of DNNClassifier for Iris plant dataset, hdf5 format."""
15
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import numpy as np
21from sklearn import datasets
22from sklearn import metrics
23from sklearn import model_selection
24import tensorflow as tf
25import h5py  # pylint: disable=g-bad-import-order
26
27
28X_FEATURE = 'x'  # Name of the input feature.
29
30
31def main(unused_argv):
32  # Load dataset.
33  iris = datasets.load_iris()
34  x_train, x_test, y_train, y_test = model_selection.train_test_split(
35      iris.data, iris.target, test_size=0.2, random_state=42)
36
37  # Note that we are saving and load iris data as h5 format as a simple
38  # demonstration here.
39  h5f = h5py.File('/tmp/test_hdf5.h5', 'w')
40  h5f.create_dataset('X_train', data=x_train)
41  h5f.create_dataset('X_test', data=x_test)
42  h5f.create_dataset('y_train', data=y_train)
43  h5f.create_dataset('y_test', data=y_test)
44  h5f.close()
45
46  h5f = h5py.File('/tmp/test_hdf5.h5', 'r')
47  x_train = np.array(h5f['X_train'])
48  x_test = np.array(h5f['X_test'])
49  y_train = np.array(h5f['y_train'])
50  y_test = np.array(h5f['y_test'])
51
52  # Build 3 layer DNN with 10, 20, 10 units respectively.
53  feature_columns = [
54      tf.feature_column.numeric_column(
55          X_FEATURE, shape=np.array(x_train).shape[1:])]
56  classifier = tf.estimator.DNNClassifier(
57      feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
58
59  # Train.
60  train_input_fn = tf.estimator.inputs.numpy_input_fn(
61      x={X_FEATURE: x_train}, y=y_train, num_epochs=None, shuffle=True)
62  classifier.train(input_fn=train_input_fn, steps=200)
63
64  # Predict.
65  test_input_fn = tf.estimator.inputs.numpy_input_fn(
66      x={X_FEATURE: x_test}, y=y_test, num_epochs=1, shuffle=False)
67  predictions = classifier.predict(input_fn=test_input_fn)
68  y_predicted = np.array(list(p['class_ids'] for p in predictions))
69  y_predicted = y_predicted.reshape(np.array(y_test).shape)
70
71  # Score with sklearn.
72  score = metrics.accuracy_score(y_test, y_predicted)
73  print('Accuracy (sklearn): {0:f}'.format(score))
74
75  # Score with tensorflow.
76  scores = classifier.evaluate(input_fn=test_input_fn)
77  print('Accuracy (tensorflow): {0:f}'.format(scores['accuracy']))
78
79
80if __name__ == '__main__':
81  tf.app.run()
82