/** * This constructs a fresh vector that is sized correctly to accommodate all the known sparse values for vectors * that are possibly sparse. * * @param presize a flag for whether or not to create all the dense double arrays for our sparse features * * @return a new, internally correctly sized ConcatVector that will work correctly as weights for features from * this namespace; */ public ConcatVector newWeightsVector(boolean presize) { ConcatVector vector = new ConcatVector(featureToIndex.size()); if (presize) { for (ObjectCursor<String> s : sparseFeatureIndex.keys()) { int size = sparseFeatureIndex.get(s.value).size(); vector.setDenseComponent(ensureFeature(s.value), new double[size]); } } setAlwaysOneFeature(vector, 1); return vector; }
/** * @return a Builder for proto serialization */ public ConcatVectorNamespaceProto.ConcatVectorNamespace.Builder getProtoBuilder() { ConcatVectorNamespaceProto.ConcatVectorNamespace.Builder m = ConcatVectorNamespaceProto.ConcatVectorNamespace.newBuilder(); // Add the outer layer features for (ObjectCursor<String> feature : featureToIndex.keys()) { ConcatVectorNamespaceProto.ConcatVectorNamespace.FeatureToIndexComponent.Builder component = ConcatVectorNamespaceProto.ConcatVectorNamespace.FeatureToIndexComponent.newBuilder(); component.setKey(feature.value); component.setData(featureToIndex.getOrDefault(feature.value, -1)); m.addFeatureToIndex(component); } for (ObjectCursor<String> feature : sparseFeatureIndex.keys()) { ConcatVectorNamespaceProto.ConcatVectorNamespace.SparseFeatureIndex.Builder sparseFeature = ConcatVectorNamespaceProto.ConcatVectorNamespace.SparseFeatureIndex.newBuilder(); sparseFeature.setKey(feature.value); for (ObjectCursor<String> sparseFeatureName : sparseFeatureIndex.get(feature.value).keys()) { ConcatVectorNamespaceProto.ConcatVectorNamespace.FeatureToIndexComponent.Builder component = ConcatVectorNamespaceProto.ConcatVectorNamespace.FeatureToIndexComponent.newBuilder(); component.setKey(sparseFeatureName.value); component.setData(sparseFeatureIndex.get(feature.value).getOrDefault(sparseFeatureName.value, -1)); sparseFeature.addFeatureToIndex(component); } m.addSparseFeatureIndex(sparseFeature); } return m; }