| 23 | namespace utils { |
| 24 | |
| 25 | Status BatchFeatures::Initialize( |
| 26 | std::vector<Tensor> dense_float_features_list, |
| 27 | std::vector<Tensor> sparse_float_feature_indices_list, |
| 28 | std::vector<Tensor> sparse_float_feature_values_list, |
| 29 | std::vector<Tensor> sparse_float_feature_shapes_list, |
| 30 | std::vector<Tensor> sparse_int_feature_indices_list, |
| 31 | std::vector<Tensor> sparse_int_feature_values_list, |
| 32 | std::vector<Tensor> sparse_int_feature_shapes_list) { |
| 33 | // Validate number of feature columns. |
| 34 | auto num_dense_float_features = dense_float_features_list.size(); |
| 35 | auto num_sparse_float_features = sparse_float_feature_indices_list.size(); |
| 36 | auto num_sparse_int_features = sparse_int_feature_indices_list.size(); |
| 37 | QCHECK(num_dense_float_features + num_sparse_float_features + |
| 38 | num_sparse_int_features > |
| 39 | 0) |
| 40 | << "Must have at least one feature column."; |
| 41 | |
| 42 | // Read dense float features. |
| 43 | dense_float_feature_columns_.reserve(num_dense_float_features); |
| 44 | for (uint32 dense_feat_idx = 0; dense_feat_idx < num_dense_float_features; |
| 45 | ++dense_feat_idx) { |
| 46 | auto dense_float_feature = dense_float_features_list[dense_feat_idx]; |
| 47 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 48 | TensorShapeUtils::IsMatrix(dense_float_feature.shape()), |
| 49 | errors::InvalidArgument("Dense float feature must be a matrix.")); |
| 50 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 51 | dense_float_feature.dim_size(0) == batch_size_, |
| 52 | errors::InvalidArgument( |
| 53 | "Dense float vector must have batch_size rows: ", batch_size_, |
| 54 | " vs. ", dense_float_feature.dim_size(0))); |
| 55 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 56 | dense_float_feature.dim_size(1) == 1, |
| 57 | errors::InvalidArgument( |
| 58 | "Dense float features may not be multivalent: dim_size(1) = ", |
| 59 | dense_float_feature.dim_size(1))); |
| 60 | dense_float_feature_columns_.emplace_back(dense_float_feature); |
| 61 | } |
| 62 | |
| 63 | // Read sparse float features. |
| 64 | sparse_float_feature_columns_.reserve(num_sparse_float_features); |
| 65 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 66 | sparse_float_feature_values_list.size() == num_sparse_float_features && |
| 67 | sparse_float_feature_shapes_list.size() == num_sparse_float_features, |
| 68 | errors::InvalidArgument("Inconsistent number of sparse float features.")); |
| 69 | for (uint32 sparse_feat_idx = 0; sparse_feat_idx < num_sparse_float_features; |
| 70 | ++sparse_feat_idx) { |
| 71 | auto sparse_float_feature_indices = |
| 72 | sparse_float_feature_indices_list[sparse_feat_idx]; |
| 73 | auto sparse_float_feature_values = |
| 74 | sparse_float_feature_values_list[sparse_feat_idx]; |
| 75 | auto sparse_float_feature_shape = |
| 76 | sparse_float_feature_shapes_list[sparse_feat_idx]; |
| 77 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 78 | TensorShapeUtils::IsMatrix(sparse_float_feature_indices.shape()), |
| 79 | errors::InvalidArgument( |
| 80 | "Sparse float feature indices must be a matrix.")); |
| 81 | TF_CHECK_AND_RETURN_IF_ERROR( |
| 82 | TensorShapeUtils::IsVector(sparse_float_feature_values.shape()), |