• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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"""Tests for SoftmaxCrossEntropyWithLogits op."""
16
17import itertools
18import sys
19
20import numpy as np
21
22from tensorflow.python.client import session
23from tensorflow.python.framework import constant_op
24from tensorflow.python.framework import dtypes
25from tensorflow.python.framework import ops
26from tensorflow.python.framework import test_util
27from tensorflow.python.kernel_tests.nn_ops import xent_op_test_base
28from tensorflow.python.ops import array_ops
29from tensorflow.python.ops import gen_nn_ops
30from tensorflow.python.ops import nn_ops
31from tensorflow.python.platform import test
32
33
34class XentOpTest(xent_op_test_base.XentOpTestBase):
35
36  @test_util.run_deprecated_v1
37  def testRankTooLarge(self):
38    for dtype in np.float16, np.float32:
39      np_features = np.array([[[1., 1., 1., 1.]], [[1., 2., 3.,
40                                                    4.]]]).astype(dtype)
41      np_labels = np.array([[[0., 0., 0., 1.]], [[0., .5, .5,
42                                                  0.]]]).astype(dtype)
43      self.assertRaisesRegex(ValueError, "rank 2, but is rank 3",
44                             gen_nn_ops.softmax_cross_entropy_with_logits,
45                             np_features, np_labels)
46
47  def testFeaturesBroadcast(self):
48    np_f = np.array([[1., 2., 3., 4.],
49                     [1., 2., 3., 4.]]).astype(np.float32)
50    np_l = np.array([[0., 0., 0., 1.],
51                     [0., .5, .5, 0.]]).astype(np.float32)
52    np_loss, np_gradient = self._npXent(labels=np_l, logits=np_f)
53    tf_f = constant_op.constant(
54        np.array([[1., 2., 3., 4.]]).astype(np.float32))
55    tf_l = constant_op.constant(
56        np.array([[0., 0., 0., 1.], [0., .5, .5, 0.]]).astype(np.float32))
57    tf_loss, tf_gradient = gen_nn_ops.softmax_cross_entropy_with_logits(
58        tf_f, tf_l)
59    self.assertAllCloseAccordingToType(np_loss, tf_loss)
60    self.assertAllCloseAccordingToType(np_gradient, tf_gradient)
61
62    tf_f = constant_op.constant(np.array([[1.]]).astype(np.float32))
63    tf_l = constant_op.constant(np.array([[1.], [1.]]).astype(np.float32))
64    tf_loss, tf_gradient = gen_nn_ops.softmax_cross_entropy_with_logits(
65        tf_f, tf_l)
66    self.assertAllClose([0, 0], tf_loss)
67    self.assertAllCloseAccordingToType([[0], [0]], tf_gradient)
68
69  @test_util.run_deprecated_v1
70  def testNotMatrix(self):
71    with self.cached_session():
72      with self.assertRaises(ValueError):
73        gen_nn_ops.softmax_cross_entropy_with_logits([0., 1., 2., 3.],
74                                                     [0., 1., 0., 1.])
75
76
77class XentBenchmark(test.Benchmark):
78
79  def benchmarkZeroDimension(self):
80    for (m, n, p, use_gpu) in itertools.product(
81        [128],
82        [10, 100, 1000, 10000, 100000],
83        [0.001, 0.01, 0.5, 0.99, 1.0],
84        [False]):
85      k = int(p * n)
86      if k == 0:
87        continue
88      name = "zero_dimension_m_%d_n_%d_k_%g_use_gpu_%s" % (m, n, k, use_gpu)
89      device = "/%s:0" % ("gpu" if use_gpu else "cpu")
90      with ops.Graph().as_default():
91        with ops.device(device):
92          labels = array_ops.zeros([0, 2, 4], dtype=dtypes.float32)
93          logits = array_ops.zeros([0, 2, 4], dtype=dtypes.float32)
94          op = nn_ops.softmax_cross_entropy_with_logits(
95              labels=labels, logits=logits)
96        with session.Session() as sess:
97          r = self.run_op_benchmark(sess, op, min_iters=100, name=name)
98          gb_processed_input = m * n / 1.0e9
99          throughput = gb_processed_input / r["wall_time"]
100          print("Benchmark: %s \t wall_time: %0.03g s \t "
101                "Throughput: %0.03g GB/s" % (name, r["wall_time"], throughput))
102          sys.stdout.flush()
103
104  def benchmarkSingleClass(self):
105    for (m, n, p, use_gpu) in itertools.product(
106        [128],
107        [10, 100, 1000, 10000, 100000],
108        [0.001, 0.01, 0.5, 0.99, 1.0],
109        [False]):
110      k = int(p * n)
111      if k == 0:
112        continue
113      name = "single_class_m_%d_n_%d_k_%g_use_gpu_%s" % (m, n, k, use_gpu)
114      device = "/%s:0" % ("gpu" if use_gpu else "cpu")
115      with ops.Graph().as_default():
116        with ops.device(device):
117          labels = constant_op.constant([[1.], [-1.], [0.]],
118                                        dtype=dtypes.float32)
119          logits = constant_op.constant([[-1.], [0.], [1.]],
120                                        dtype=dtypes.float32)
121          op = nn_ops.softmax_cross_entropy_with_logits(
122              labels=labels, logits=logits)
123        with session.Session() as sess:
124          r = self.run_op_benchmark(sess, op, min_iters=100, name=name)
125          gb_processed_input = m * n / 1.0e9
126          throughput = gb_processed_input / r["wall_time"]
127          print("Benchmark: %s \t wall_time: %0.03g s \t "
128                "Throughput: %0.03g GB/s" % (name, r["wall_time"], throughput))
129          sys.stdout.flush()
130
131
132if __name__ == "__main__":
133  test.main()
134