Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
feature-vector-test.C
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
35 
36 #include <map>
37 
38 #include "feature-vector.H"
39 
41 using std::map;
42 using std::cout;
43 using std::endl;
44 
45 int
46 main(int argc, char **argv) {
47  map<int, double> features1;
48  features1[0] = 2.0;
49  features1[1] = 4.0;
50  FeatureVector<int,double> feature_vector1(features1);
51 
52  map<int, double> features2;
53  features2[0] = 4.0;
54  features2[1] = 8.0;
55  features2[2] = 45.0;
56  FeatureVector<int,double> feature_vector2(features2);
57 
58  cout << "Feature vector 1:" << feature_vector1 << endl
59  << "Feature vector 2:" << feature_vector2 << endl
60  << "Dot product: " << feature_vector1.Dot(feature_vector2) << endl;
61 
62  cout << "Weight in feature vector 1 for feature 2: "
63  << feature_vector1.GetWeight(2) << endl;
64  cout << "Weight in feature vector 2 for feature 2: "
65  << feature_vector2.GetWeight(2) << endl;
66 
67  cout << "Feature vector 1 size: " << feature_vector1.size() << endl
68  << "Setting feature 0 to 0..." << endl;
69  feature_vector1.SetWeight(0, 0.0);
70  cout << "New feature vector 1 size: " << feature_vector1.size() << endl;
71 
72  cout << "Printing out feature vector 1 using its iterators:" << endl;
73  for (FeatureVector<int,double>::const_iterator it = feature_vector1.begin();
74  it != feature_vector1.end();
75  ++it) {
76  cout << "\t" << it->first << "=" << it->second << "\n";
77  }
78  cout << "Done." << endl;
79 
80  cout << "Printing out feature vector 2 using its iterators:" << endl;
81  for (FeatureVector<int,double>::const_iterator it = feature_vector2.begin();
82  it != feature_vector2.end();
83  ++it) {
84  cout << "\t" << it->first << "=" << it->second << "\n";
85  }
86  cout << "Done." << endl;
87 
88  cout << "Scaling feature vector 2 by 2.0:" << endl;
89  feature_vector2.Scale(2.0);
90  cout << "\t" << feature_vector2 << endl;
91 
92  cout << "Adding feature vector 2 scaled by 2.0 to feature vector 1:" << endl;
93  feature_vector1.AddScaledVector(feature_vector2, 2.0);
94  cout << "\t" << feature_vector1 << endl;
95 
96  cout << "Have a nice day!" << endl;
97 }
FeatureVector< K, V > & AddScaledVector(const FeatureVector< K, V > &other, V scalar)
Modifies this vector so that it equals this vector plus the scaled specified vector.
size_t size() const
Returns the number of non-zero feature components of this feature vector.
V GetWeight(const K &uid) const
Returns the weight of the feature with the specified uid, where crucially features not "present" in t...
FeatureVector< K, V > & Scale(V scalar)
Multiplies this vector, in situ, by the specified scalar.
const_iterator end() const
Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...
const_iterator begin() const
Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...
V SetWeight(const K &uid, V new_weight)
Sets the weight of the specified feature to the specified value.
Defines the reranker::FeatureVector class, which, as it happens, is used to store feature vectors...
V Dot(const FeatureVector< K, V > &other) const
Computes the dot product of this feature vector with the specified FeatureVector. ...
int main(int argc, char **argv)
A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.