• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# ==============================================================================
15"""Non-linear estimator tests."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import random
22
23from tensorflow.contrib.layers.python.layers import feature_column
24from tensorflow.contrib.learn.python.learn.datasets import base
25from tensorflow.contrib.learn.python.learn.estimators import dnn
26from tensorflow.contrib.learn.python.learn.estimators import run_config
27from tensorflow.python.framework import random_seed
28from tensorflow.python.platform import test
29
30
31class NonLinearTest(test.TestCase):
32  """Non-linear estimator tests."""
33
34  def setUp(self):
35    random.seed(42)
36    random_seed.set_random_seed(42)
37
38  def testIrisDNN(self):
39    iris = base.load_iris()
40    feature_columns = [feature_column.real_valued_column("", dimension=4)]
41    classifier = dnn.DNNClassifier(
42        feature_columns=feature_columns,
43        hidden_units=[10, 20, 10],
44        n_classes=3,
45        config=run_config.RunConfig(tf_random_seed=1))
46    classifier.fit(iris.data, iris.target, max_steps=200)
47    variable_names = classifier.get_variable_names()
48    self.assertEqual(
49        classifier.get_variable_value("dnn/hiddenlayer_0/weights").shape,
50        (4, 10))
51    self.assertEqual(
52        classifier.get_variable_value("dnn/hiddenlayer_1/weights").shape,
53        (10, 20))
54    self.assertEqual(
55        classifier.get_variable_value("dnn/hiddenlayer_2/weights").shape,
56        (20, 10))
57    self.assertEqual(
58        classifier.get_variable_value("dnn/logits/weights").shape, (10, 3))
59    self.assertIn("dnn/hiddenlayer_0/biases", variable_names)
60    self.assertIn("dnn/hiddenlayer_1/biases", variable_names)
61    self.assertIn("dnn/hiddenlayer_2/biases", variable_names)
62    self.assertIn("dnn/logits/biases", variable_names)
63
64  def testBostonDNN(self):
65    boston = base.load_boston()
66    feature_columns = [feature_column.real_valued_column("", dimension=13)]
67    regressor = dnn.DNNRegressor(
68        feature_columns=feature_columns,
69        hidden_units=[10, 20, 10],
70        config=run_config.RunConfig(tf_random_seed=1))
71    regressor.fit(boston.data,
72                  boston.target,
73                  steps=300,
74                  batch_size=boston.data.shape[0])
75    weights = ([regressor.get_variable_value("dnn/hiddenlayer_0/weights")] +
76               [regressor.get_variable_value("dnn/hiddenlayer_1/weights")] +
77               [regressor.get_variable_value("dnn/hiddenlayer_2/weights")] +
78               [regressor.get_variable_value("dnn/logits/weights")])
79    self.assertEqual(weights[0].shape, (13, 10))
80    self.assertEqual(weights[1].shape, (10, 20))
81    self.assertEqual(weights[2].shape, (20, 10))
82    self.assertEqual(weights[3].shape, (10, 1))
83
84    biases = ([regressor.get_variable_value("dnn/hiddenlayer_0/biases")] +
85              [regressor.get_variable_value("dnn/hiddenlayer_1/biases")] +
86              [regressor.get_variable_value("dnn/hiddenlayer_2/biases")] +
87              [regressor.get_variable_value("dnn/logits/biases")])
88    self.assertEqual(biases[0].shape, (10,))
89    self.assertEqual(biases[1].shape, (20,))
90    self.assertEqual(biases[2].shape, (10,))
91    self.assertEqual(biases[3].shape, (1,))
92
93  def testDNNDropout0(self):
94    # Dropout prob == 0.
95    iris = base.load_iris()
96    feature_columns = [feature_column.real_valued_column("", dimension=4)]
97    classifier = dnn.DNNClassifier(
98        feature_columns=feature_columns,
99        hidden_units=[10, 20, 10],
100        n_classes=3,
101        dropout=0.0,
102        config=run_config.RunConfig(tf_random_seed=1))
103    classifier.fit(iris.data, iris.target, max_steps=200)
104
105  def testDNNDropout0_1(self):
106    # Dropping only a little.
107    iris = base.load_iris()
108    feature_columns = [feature_column.real_valued_column("", dimension=4)]
109    classifier = dnn.DNNClassifier(
110        feature_columns=feature_columns,
111        hidden_units=[10, 20, 10],
112        n_classes=3,
113        dropout=0.1,
114        config=run_config.RunConfig(tf_random_seed=1))
115    classifier.fit(iris.data, iris.target, max_steps=200)
116
117  def testDNNDropout0_9(self):
118    # Dropping out most of it.
119    iris = base.load_iris()
120    feature_columns = [feature_column.real_valued_column("", dimension=4)]
121    classifier = dnn.DNNClassifier(
122        feature_columns=feature_columns,
123        hidden_units=[10, 20, 10],
124        n_classes=3,
125        dropout=0.9,
126        config=run_config.RunConfig(tf_random_seed=1))
127    classifier.fit(iris.data, iris.target, max_steps=200)
128
129
130if __name__ == "__main__":
131  test.main()
132