/** * Adds a value to a vector. * * @param originalForm The original form of the value as a string. * @param data The vector to which the value should be added. */ @Override public void addToVector(byte[] originalForm, double weight, Vector data) { int probes = getProbes(); String name = getName(); for (int i = 0; i < probes; i++) { int n = hashForProbe(originalForm, data.size(), name, i); if (isTraceEnabled()) { trace((String) null, n); } data.set(n, data.get(n) + getWeight(originalForm,weight)); } }
@Override public void setProbes(int probes) { super.setProbes(probes); initCaches(); }
public static ArgumentEncoder newNumericEncoder(int position, String name) { ArgumentEncoder r = new ArgumentEncoder(position, name); r.encoder = new ContinuousValueEncoder(name); return r; }
@Test public void testAddToVector() { WordValueEncoder wv = new StaticWordValueEncoder("word"); ContinuousValueEncoder cv = new ContinuousValueEncoder("cont"); InteractionValueEncoder enc = new InteractionValueEncoder("interactions", wv, cv); Vector v1 = new DenseVector(200); enc.addInteractionToVector("a","1.0",1.0, v1); int k = enc.getProbes(); // should set k distinct locations to 1 assertEquals((float) k, v1.norm(1), 0); assertEquals(1.0, v1.maxValue(), 0); // adding same interaction again should increment weights enc.addInteractionToVector("a","1.0",1.0,v1); assertEquals((float) k*2, v1.norm(1), 0); assertEquals(2.0, v1.maxValue(), 0); Vector v2 = new DenseVector(20000); enc.addInteractionToVector("a","1.0",1.0,v2); wv.addToVector("a", v2); cv.addToVector("1.0", v2); k = enc.getProbes(); //this assumes no hash collision assertEquals((float) (k + wv.getProbes()+cv.getProbes()), v2.norm(1), 1.0e-3); }
/** * Converts a value into a form that would help a human understand the internals of how the value * is being interpreted. For text-like things, this is likely to be a list of the terms found with * associated weights (if any). * * @param originalForm The original form of the value as a string. * @return A string that a human can read. */ @Override public String asString(String originalForm) { return getName() + ':' + originalForm; }
@Override protected int hashForProbe(byte[] originalForm, int dataSize, String name, int probe) { Preconditions.checkArgument(dataSize == this.dataSize, "dataSize argument [" + dataSize + "] does not match expected dataSize [" + this.dataSize + ']'); int originalHashcode = Arrays.hashCode(originalForm); if (caches[probe].containsKey(originalHashcode)) { return caches[probe].get(originalHashcode); } int hash = super.hashForProbe(originalForm, dataSize, name, probe); caches[probe].put(originalHashcode, hash); return hash; } }
/** * Converts a value into a form that would help a human understand the internals of how the value * is being interpreted. For text-like things, this is likely to be a list of the terms found with * associated weights (if any). * * @param originalForm The original form of the value as a string. * @return A string that a human can read. */ @Override public String asString(String originalForm) { return getName() + ':' + originalForm; }
@Override protected int hashForProbe(byte[] originalForm, int dataSize, String name, int probe) { Preconditions.checkArgument(dataSize == this.dataSize, "dataSize argument [" + dataSize + "] does not match expected dataSize [" + this.dataSize + ']'); int originalHashcode = originalForm.hashCode(); if (caches[probe].containsKey(originalHashcode)) { return caches[probe].get(originalHashcode); } int hash = super.hashForProbe(originalForm, dataSize, name, probe); caches[probe].put(originalHashcode, hash); return hash; } }
/** * Adds a value to a vector. * * @param originalForm The original form of the value as a string. * @param data The vector to which the value should be added. */ @Override public void addToVector(byte[] originalForm, double weight, Vector data) { int probes = getProbes(); String name = getName(); for (int i = 0; i < probes; i++) { int n = hashForProbe(originalForm, data.size(), name, i); if (isTraceEnabled()) { trace((String) null, n); } data.set(n, data.get(n) + getWeight(originalForm,weight)); } }
/** * Converts a value into a form that would help a human understand the internals of how the value * is being interpreted. For text-like things, this is likely to be a list of the terms found with * associated weights (if any). * * @param originalForm The original form of the value as a string. * @return A string that a human can read. */ @Override public String asString(String originalForm) { return getName() + ':' + originalForm; }
@Override public void setProbes(int probes) { super.setProbes(probes); initCaches(); }
@Test public void testAsString() { FeatureVectorEncoder enc = new ContinuousValueEncoder("foo"); assertEquals("foo:123", enc.asString("123")); }
/** * Adds a value to a vector. * * @param originalForm The original form of the value as a string. * @param data The vector to which the value should be added. */ @Override public void addToVector(byte[] originalForm, double weight, Vector data) { int probes = getProbes(); String name = getName(); for (int i = 0; i < probes; i++) { int n = hashForProbe(originalForm, data.size(), name, i); if (isTraceEnabled()) { trace((String) null, n); } data.set(n, data.get(n) + getWeight(originalForm,weight)); } }
@Override public void setProbes(int probes) { super.setProbes(probes); initCaches(); }
@Test public void testAddToVectorUsesProductOfWeights() { WordValueEncoder wv = new StaticWordValueEncoder("word"); ContinuousValueEncoder cv = new ContinuousValueEncoder("cont"); InteractionValueEncoder enc = new InteractionValueEncoder("interactions", wv, cv); Vector v1 = new DenseVector(200); enc.addInteractionToVector("a","0.9",0.5, v1); int k = enc.getProbes(); // should set k distinct locations to 0.9*0.5 assertEquals((float) k*0.5*0.9, v1.norm(1), 0); assertEquals(0.5*0.9, v1.maxValue(), 0); }
@Test public void testAddToVector() { FeatureVectorEncoder enc = new ContinuousValueEncoder("foo"); Vector v1 = new DenseVector(20); enc.addToVector("-123", v1);