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"""Tests for tf upgrader.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20import shutil 21import tempfile 22import numpy as np 23import tensorflow as tf 24from tensorflow.python.framework import test_util 25from tensorflow.python.platform import test as test_lib 26 27 28class TestUpgrade(test_util.TensorFlowTestCase): 29 """Test various APIs that have been changed in 1.0. 30 31 This test will not run in current TensorFlow, but did run in 0.11. 32 This file is intended to be converted by a genrule() that uses the converter 33 so that a 1.0 compatible version of this file is generated. That is run as 34 a unit test if the converter is successful. 35 """ 36 37 @test_util.run_v1_only("b/120545219") 38 def testArgRenames(self): 39 with self.cached_session(): 40 41 a = [[1., 2., 3.], [4., 5., 6.]] 42 b = [[True, False, False], [False, True, True]] 43 dim0 = [1] 44 dim1 = [1] 45 46 self.assertAllEqual( 47 tf.reduce_any( 48 b, reduction_indices=dim0).eval(), [True, True]) 49 self.assertAllEqual( 50 tf.reduce_all( 51 b, reduction_indices=[0]).eval(), [False, False, False]) 52 self.assertAllEqual( 53 tf.reduce_all( 54 b, reduction_indices=dim1).eval(), [False, False]) 55 self.assertAllEqual( 56 tf.reduce_sum( 57 a, reduction_indices=[1]).eval(), [6., 15.]) 58 self.assertAllEqual( 59 tf.reduce_sum( 60 a, reduction_indices=[0, 1]).eval(), 21.0) 61 self.assertAllEqual(tf.reduce_sum(a, [0, 1]).eval(), 21.0) 62 self.assertAllEqual( 63 tf.reduce_prod( 64 a, reduction_indices=[1]).eval(), [6., 120.]) 65 self.assertAllEqual( 66 tf.reduce_prod( 67 a, reduction_indices=[0, 1]).eval(), 720.0) 68 self.assertAllEqual(tf.reduce_prod(a, [0, 1]).eval(), 720.0) 69 self.assertAllEqual( 70 tf.reduce_mean( 71 a, reduction_indices=[1]).eval(), [2., 5.]) 72 self.assertAllEqual( 73 tf.reduce_mean( 74 a, reduction_indices=[0, 1]).eval(), 3.5) 75 self.assertAllEqual(tf.reduce_mean(a, [0, 1]).eval(), 3.5) 76 self.assertAllEqual( 77 tf.reduce_min( 78 a, reduction_indices=[1]).eval(), [1., 4.]) 79 self.assertAllEqual( 80 tf.reduce_min( 81 a, reduction_indices=[0, 1]).eval(), 1.0) 82 self.assertAllEqual(tf.reduce_min(a, [0, 1]).eval(), 1.0) 83 self.assertAllEqual( 84 tf.reduce_max( 85 a, reduction_indices=[1]).eval(), [3., 6.]) 86 self.assertAllEqual( 87 tf.reduce_max( 88 a, reduction_indices=[0, 1]).eval(), 6.0) 89 self.assertAllEqual(tf.reduce_max(a, [0, 1]).eval(), 6.0) 90 self.assertAllClose(tf.reduce_logsumexp(a, reduction_indices=[1]).eval(), 91 [3.40760589, 6.40760612]) 92 self.assertAllClose( 93 tf.reduce_logsumexp(a, reduction_indices=[0, 1]).eval(), 94 6.45619344711) 95 self.assertAllClose( 96 tf.reduce_logsumexp(a, [0, 1]).eval(), 6.45619344711) 97 self.assertAllEqual( 98 tf.expand_dims([[1, 2], [3, 4]], axis=1).eval(), 99 [[[1, 2]], [[3, 4]]]) 100 101 @test_util.run_v1_only("b/120545219") 102 def testArgMinMax(self): 103 with self.cached_session(): 104 self.assertAllEqual( 105 tf.argmin([[1, 2, 3], [4, 1, 0]], dimension=1).eval(), 106 [0, 2]) 107 self.assertAllEqual( 108 tf.argmin([[1, 2, 3], [4, 1, 0]], dimension=0).eval(), 109 [0, 1, 1]) 110 self.assertAllEqual( 111 tf.argmax([[1, 2, 3], [4, 1, 0]], dimension=1).eval(), 112 [2, 0]) 113 self.assertAllEqual( 114 tf.argmax([[1, 2, 3], [4, 1, 0]], dimension=0).eval(), 115 [1, 0, 0]) 116 117 @test_util.run_v1_only("b/120545219") 118 def testExpandAndSqueeze(self): 119 with self.cached_session(): 120 121 # TODO(aselle): sparse_split, sparse_reduce_sum, 122 # sparse_reduce_sum_sparse, reduce_join 123 a = [[1, 2, 3]] 124 self.assertAllEqual(tf.expand_dims(tf.squeeze(a, [0]), 0).eval(), 125 a) 126 self.assertAllEqual(tf.squeeze(tf.expand_dims(a, 1), [1]).eval(), 127 a) 128 self.assertAllEqual( 129 tf.expand_dims(tf.squeeze([[1, 2, 3]], axis=[0]), dim=0).eval(), a) 130 self.assertAllEqual( 131 tf.squeeze(tf.expand_dims([[1, 2, 3]], dim=1), axis=[1]).eval(), a) 132 133 self.assertAllEqual( 134 tf.squeeze(tf.expand_dims([[1, 2, 3]], dim=1), axis=[1]).eval(), a) 135 136 @test_util.run_v1_only("b/120545219") 137 def testArithmeticRenames(self): 138 with self.cached_session() as s: 139 stuff = tf.split(1, 2, [[1, 2, 3, 4], [4, 5, 6, 7]]) 140 vals = s.run(stuff) 141 self.assertAllEqual(vals, 142 [[[1, 2], [4, 5]], [[3, 4], [6, 7]]]) 143 self.assertAllEqual( 144 tf.neg(tf.mul(tf.add(1, 2), tf.sub(5, 3))).eval(), 145 -6) 146 self.assertAllEqual( 147 s.run(tf.listdiff([1, 2, 3], [3, 3, 4]))[0], [1, 2]) 148 self.assertAllEqual( 149 tf.list_diff([1, 2, 3], [3, 3, 4])[0].eval(), [1, 2]) 150 a = [[1., 2., 3.], [4., 5., 6.]] 151 foo = np.where(np.less(a, 2), np.negative(a), a) 152 self.assertAllEqual( 153 tf.select(tf.less(a, 2), tf.neg(a), a).eval(), 154 foo) 155 self.assertAllEqual( 156 tf.complex_abs(tf.constant(3 + 4.j)).eval(), 157 5) 158 # # TODO(aselle): (tf.batch_*) 159 # ] 160 161 @test_util.run_v1_only("b/120545219") 162 def testBatchAndSvd(self): 163 with self.cached_session(): 164 mat = [[1., 2.], [2., 3.]] 165 batched_mat = tf.expand_dims(mat, [0]) 166 result = tf.matmul(mat, mat).eval() 167 result_batched = tf.batch_matmul(batched_mat, batched_mat).eval() 168 self.assertAllEqual(result_batched, np.expand_dims(result, 0)) 169 self.assertAllEqual( 170 tf.svd(mat, False, True).eval(), 171 tf.svd(mat, compute_uv=False, full_matrices=True).eval()) 172 173 @test_util.run_v1_only("b/120545219") 174 def testCrossEntropy(self): 175 # TODO(aselle): Test sparse_softmax_... 176 with self.cached_session(): 177 labels = [.8, .5, .2, .1] 178 logits = [.9, .1, .3, .1] 179 self.assertAllEqual( 180 tf.nn.softmax_cross_entropy_with_logits( 181 logits, labels).eval(), 182 tf.nn.softmax_cross_entropy_with_logits( 183 labels=labels, logits=logits).eval()) 184 self.assertAllEqual( 185 tf.nn.sigmoid_cross_entropy_with_logits( 186 logits, labels).eval(), 187 tf.nn.sigmoid_cross_entropy_with_logits( 188 labels=labels, logits=logits).eval()) 189 190 @test_util.run_v1_only("b/120545219") 191 def testVariables(self): 192 with self.cached_session() as s: 193 194 # make some variables 195 _ = [tf.Variable([1, 2, 3], dtype=tf.float32), 196 tf.Variable([1, 2, 3], dtype=tf.int32)] 197 s.run(tf.global_variables_initializer()) 198 _ = [v.name for v in tf.all_variables()] 199 _ = [v.name for v in tf.local_variables()] 200 201 @test_util.run_v1_only("b/120545219") 202 def testSummaries(self): 203 with self.cached_session() as s: 204 var = tf.Variable([1, 2, 3], dtype=tf.float32) 205 s.run(tf.global_variables_initializer()) 206 x, y = np.meshgrid(np.linspace(-10, 10, 256), np.linspace(-10, 10, 256)) 207 image = np.sin(x**2 + y**2) / np.sqrt(x**2 + y**2) * .5 + .5 208 image = image[None, :, :, None] 209 210 # make a dummy sound 211 freq = 440 # A = 440Hz 212 sampling_frequency = 11000 213 audio = np.sin(2 * np.pi * np.linspace(0, 1, sampling_frequency) * freq) 214 audio = audio[None, :, None] 215 test_dir = tempfile.mkdtemp() 216 # test summaries 217 writer = tf.train.SummaryWriter(test_dir) 218 summaries = [ 219 tf.scalar_summary("scalar_var", var[0]), 220 tf.scalar_summary("scalar_reduce_var", tf.reduce_sum(var)), 221 tf.histogram_summary("var_histogram", var), 222 tf.image_summary("sin_image", image), 223 tf.audio_summary("sin_wave", audio, sampling_frequency), 224 ] 225 run_summaries = s.run(summaries) 226 writer.add_summary(s.run(tf.merge_summary(inputs=run_summaries))) 227 # This is redundant, but we want to be able to rewrite the command 228 writer.add_summary(s.run(tf.merge_all_summaries())) 229 writer.close() 230 shutil.rmtree(test_dir) 231 232 233if __name__ == "__main__": 234 test_lib.main() 235