embedding_feature_extractor.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "syntaxnet/embedding_feature_extractor.h"
  13. #include <vector>
  14. #include "syntaxnet/feature_extractor.h"
  15. #include "syntaxnet/parser_features.h"
  16. #include "syntaxnet/task_context.h"
  17. #include "syntaxnet/utils.h"
  18. namespace syntaxnet {
  19. void GenericEmbeddingFeatureExtractor::Setup(TaskContext *context) {
  20. // Don't use version to determine how to get feature FML.
  21. const string features = context->Get(
  22. tensorflow::strings::StrCat(ArgPrefix(), "_", "features"), "");
  23. const string embedding_names =
  24. context->Get(GetParamName("embedding_names"), "");
  25. const string embedding_dims =
  26. context->Get(GetParamName("embedding_dims"), "");
  27. LOG(INFO) << "Features: " << features;
  28. LOG(INFO) << "Embedding names: " << embedding_names;
  29. LOG(INFO) << "Embedding dims: " << embedding_dims;
  30. embedding_fml_ = utils::Split(features, ';');
  31. add_strings_ = context->Get(GetParamName("add_varlen_strings"), false);
  32. embedding_names_ = utils::Split(embedding_names, ';');
  33. for (const string &dim : utils::Split(embedding_dims, ';')) {
  34. embedding_dims_.push_back(utils::ParseUsing<int>(dim, utils::ParseInt32));
  35. }
  36. }
  37. void GenericEmbeddingFeatureExtractor::Init(TaskContext *context) {
  38. }
  39. std::vector<std::vector<SparseFeatures>>
  40. GenericEmbeddingFeatureExtractor::ConvertExample(
  41. const std::vector<FeatureVector> &feature_vectors) const {
  42. // Extract the features.
  43. std::vector<std::vector<SparseFeatures>> sparse_features(
  44. feature_vectors.size());
  45. for (size_t i = 0; i < feature_vectors.size(); ++i) {
  46. // Convert the nlp_parser::FeatureVector to dist belief format.
  47. sparse_features[i] = std::vector<SparseFeatures>(
  48. generic_feature_extractor(i).feature_types());
  49. for (int j = 0; j < feature_vectors[i].size(); ++j) {
  50. const FeatureType &feature_type = *feature_vectors[i].type(j);
  51. const FeatureValue value = feature_vectors[i].value(j);
  52. const bool is_continuous = feature_type.name().find("continuous") == 0;
  53. const int64 id = is_continuous ? FloatFeatureValue(value).id : value;
  54. const int base = feature_type.base();
  55. if (id >= 0) {
  56. sparse_features[i][base].add_id(id);
  57. if (is_continuous) {
  58. sparse_features[i][base].add_weight(FloatFeatureValue(value).weight);
  59. }
  60. if (add_strings_) {
  61. sparse_features[i][base].add_description(tensorflow::strings::StrCat(
  62. feature_type.name(), "=", feature_type.GetFeatureValueName(id)));
  63. }
  64. }
  65. }
  66. }
  67. return sparse_features;
  68. }
  69. } // namespace syntaxnet