1// Copyright 2017 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 16#include "tensorflow/contrib/boosted_trees/lib/utils/batch_features.h" 17#include "tensorflow/core/framework/tensor_testutil.h" 18#include "tensorflow/core/lib/core/errors.h" 19#include "tensorflow/core/lib/core/status.h" 20#include "tensorflow/core/lib/core/status_test_util.h" 21#include "tensorflow/core/platform/test.h" 22 23namespace tensorflow { 24namespace boosted_trees { 25namespace utils { 26namespace { 27 28using errors::InvalidArgument; 29using test::AsTensor; 30 31class BatchFeaturesTest : public ::testing::Test {}; 32 33TEST_F(BatchFeaturesTest, InvalidNumFeatures) { 34 BatchFeatures batch_features(8); 35 EXPECT_DEATH(({ batch_features.Initialize({}, {}, {}, {}, {}, {}, {}); }) 36 .IgnoreError(), 37 "Must have at least one feature column."); 38} 39 40TEST_F(BatchFeaturesTest, DenseFloatFeatures_WrongShape) { 41 BatchFeatures batch_features(8); 42 auto dense_vec = AsTensor<float>({3.0f, 7.0f}); 43 auto expected_error = 44 InvalidArgument("Dense float feature must be a matrix."); 45 EXPECT_EQ(expected_error, 46 batch_features.Initialize({dense_vec}, {}, {}, {}, {}, {}, {})); 47} 48 49TEST_F(BatchFeaturesTest, DenseFloatFeatures_WrongBatchDimension) { 50 BatchFeatures batch_features(8); 51 auto dense_vec = AsTensor<float>({3.0f, 7.0f}, {2, 1}); 52 auto expected_error = 53 InvalidArgument("Dense float vector must have batch_size rows: 8 vs. 2"); 54 EXPECT_EQ(expected_error, 55 batch_features.Initialize({dense_vec}, {}, {}, {}, {}, {}, {})); 56} 57 58TEST_F(BatchFeaturesTest, DenseFloatFeatures_Multivalent) { 59 BatchFeatures batch_features(1); 60 auto dense_vec = AsTensor<float>({3.0f, 7.0f}, {1, 2}); 61 auto expected_error = InvalidArgument( 62 "Dense float features may not be multi-valent: dim_size(1) = 2"); 63 EXPECT_EQ(expected_error, 64 batch_features.Initialize({dense_vec}, {}, {}, {}, {}, {}, {})); 65} 66 67TEST_F(BatchFeaturesTest, SparseFloatFeatures_WrongShapeIndices) { 68 BatchFeatures batch_features(2); 69 auto sparse_float_feature_indices = AsTensor<int64>({0, 0, 1, 0}); 70 auto sparse_float_feature_values = AsTensor<float>({3.0f, 7.0f}); 71 auto sparse_float_feature_shape = AsTensor<int64>({2, 1}); 72 auto expected_error = 73 InvalidArgument("Sparse float feature indices must be a matrix."); 74 EXPECT_EQ(expected_error, batch_features.Initialize( 75 {}, {sparse_float_feature_indices}, 76 {sparse_float_feature_values}, 77 {sparse_float_feature_shape}, {}, {}, {})); 78} 79 80TEST_F(BatchFeaturesTest, SparseFloatFeatures_WrongShapeValues) { 81 BatchFeatures batch_features(2); 82 auto sparse_float_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 83 auto sparse_float_feature_values = AsTensor<float>({3.0f, 7.0f}, {1, 2}); 84 auto sparse_float_feature_shape = AsTensor<int64>({2, 1}); 85 auto expected_error = 86 InvalidArgument("Sparse float feature values must be a vector."); 87 EXPECT_EQ(expected_error, batch_features.Initialize( 88 {}, {sparse_float_feature_indices}, 89 {sparse_float_feature_values}, 90 {sparse_float_feature_shape}, {}, {}, {})); 91} 92 93TEST_F(BatchFeaturesTest, SparseFloatFeatures_WrongShapeShape) { 94 BatchFeatures batch_features(2); 95 auto sparse_float_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 96 auto sparse_float_feature_values = AsTensor<float>({3.0f, 7.0f}); 97 auto sparse_float_feature_shape = AsTensor<int64>({2, 1}, {1, 2}); 98 auto expected_error = 99 InvalidArgument("Sparse float feature shape must be a vector."); 100 EXPECT_EQ(expected_error, batch_features.Initialize( 101 {}, {sparse_float_feature_indices}, 102 {sparse_float_feature_values}, 103 {sparse_float_feature_shape}, {}, {}, {})); 104} 105 106TEST_F(BatchFeaturesTest, SparseFloatFeatures_WrongSizeShape) { 107 BatchFeatures batch_features(2); 108 auto sparse_float_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 109 auto sparse_float_feature_values = AsTensor<float>({3.0f, 7.0f}); 110 auto sparse_float_feature_shape = AsTensor<int64>({2, 1, 9}); 111 auto expected_error = 112 InvalidArgument("Sparse float feature column must be two-dimensional."); 113 EXPECT_EQ(expected_error, batch_features.Initialize( 114 {}, {sparse_float_feature_indices}, 115 {sparse_float_feature_values}, 116 {sparse_float_feature_shape}, {}, {}, {})); 117} 118 119TEST_F(BatchFeaturesTest, SparseFloatFeatures_IncompatibleShape) { 120 BatchFeatures batch_features(2); 121 auto sparse_float_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 122 auto sparse_float_feature_values = AsTensor<float>({3.0f, 7.0f}); 123 auto sparse_float_feature_shape = AsTensor<int64>({8, 1}); 124 auto expected_error = InvalidArgument( 125 "Sparse float feature shape incompatible with batch size."); 126 EXPECT_EQ(expected_error, batch_features.Initialize( 127 {}, {sparse_float_feature_indices}, 128 {sparse_float_feature_values}, 129 {sparse_float_feature_shape}, {}, {}, {})); 130} 131 132TEST_F(BatchFeaturesTest, SparseIntFeatures_WrongShapeIndices) { 133 BatchFeatures batch_features(2); 134 auto sparse_int_feature_indices = AsTensor<int64>({0, 0, 1, 0}); 135 auto sparse_int_feature_values = AsTensor<int64>({3, 7}); 136 auto sparse_int_feature_shape = AsTensor<int64>({2, 1}); 137 auto expected_error = 138 InvalidArgument("Sparse int feature indices must be a matrix."); 139 EXPECT_EQ(expected_error, 140 batch_features.Initialize( 141 {}, {}, {}, {}, {sparse_int_feature_indices}, 142 {sparse_int_feature_values}, {sparse_int_feature_shape})); 143} 144 145TEST_F(BatchFeaturesTest, SparseIntFeatures_WrongShapeValues) { 146 BatchFeatures batch_features(2); 147 auto sparse_int_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 148 auto sparse_int_feature_values = AsTensor<int64>({3, 7}, {1, 2}); 149 auto sparse_int_feature_shape = AsTensor<int64>({2, 1}); 150 auto expected_error = 151 InvalidArgument("Sparse int feature values must be a vector."); 152 EXPECT_EQ(expected_error, 153 batch_features.Initialize( 154 {}, {}, {}, {}, {sparse_int_feature_indices}, 155 {sparse_int_feature_values}, {sparse_int_feature_shape})); 156} 157 158TEST_F(BatchFeaturesTest, SparseIntFeatures_WrongShapeShape) { 159 BatchFeatures batch_features(2); 160 auto sparse_int_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 161 auto sparse_int_feature_values = AsTensor<int64>({3, 7}); 162 auto sparse_int_feature_shape = AsTensor<int64>({2, 1}, {1, 2}); 163 auto expected_error = 164 InvalidArgument("Sparse int feature shape must be a vector."); 165 EXPECT_EQ(expected_error, 166 batch_features.Initialize( 167 {}, {}, {}, {}, {sparse_int_feature_indices}, 168 {sparse_int_feature_values}, {sparse_int_feature_shape})); 169} 170 171TEST_F(BatchFeaturesTest, SparseIntFeatures_WrongSizeShape) { 172 BatchFeatures batch_features(2); 173 auto sparse_int_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 174 auto sparse_int_feature_values = AsTensor<int64>({3, 7}); 175 auto sparse_int_feature_shape = AsTensor<int64>({2, 1, 9}); 176 auto expected_error = 177 InvalidArgument("Sparse int feature column must be two-dimensional."); 178 EXPECT_EQ(expected_error, 179 batch_features.Initialize( 180 {}, {}, {}, {}, {sparse_int_feature_indices}, 181 {sparse_int_feature_values}, {sparse_int_feature_shape})); 182} 183 184TEST_F(BatchFeaturesTest, SparseIntFeatures_IncompatibleShape) { 185 BatchFeatures batch_features(2); 186 auto sparse_int_feature_indices = AsTensor<int64>({0, 0, 1, 0}, {2, 2}); 187 auto sparse_int_feature_values = AsTensor<int64>({3, 7}); 188 auto sparse_int_feature_shape = AsTensor<int64>({8, 1}); 189 auto expected_error = 190 InvalidArgument("Sparse int feature shape incompatible with batch size."); 191 EXPECT_EQ(expected_error, 192 batch_features.Initialize( 193 {}, {}, {}, {}, {sparse_int_feature_indices}, 194 {sparse_int_feature_values}, {sparse_int_feature_shape})); 195} 196 197} // namespace 198} // namespace utils 199} // namespace boosted_trees 200} // namespace tensorflow 201