/// <summary> /// Describes how the transformer handles one column pair. /// </summary> /// <param name="name">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param> /// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="name"/> will be used as source.</param> /// <param name="outputKind">Kind of output: bag, indicator vector etc.</param> /// <param name="numberOfBits">Number of bits to hash into. Must be between 1 and 31, inclusive.</param> /// <param name="seed">Hashing seed.</param> /// <param name="useOrderedHashing">Whether the position of each term should be included in the hash.</param> /// <param name="maximumNumberOfInverts">During hashing we constuct mappings between original values and the produced hash values. /// Text representation of original values are stored in the slot names of the metadata for the new column.Hashing, as such, can map many initial values to one. /// <paramref name="maximumNumberOfInverts"/> specifies the upper bound of the number of distinct input values mapping to a hash that should be retained. /// <value>0</value> does not retain any input values. <value>-1</value> retains all input values mapping to each hash.</param> public ColumnOptions(string name, string inputColumnName = null, OneHotEncodingEstimator.OutputKind outputKind = Defaults.OutputKind, int numberOfBits = Defaults.NumberOfBits, uint seed = Defaults.Seed, bool useOrderedHashing = Defaults.UseOrderedHashing, int maximumNumberOfInverts = Defaults.MaximumNumberOfInverts) { HashingOptions = new HashingEstimator.ColumnOptionsInternal(name, inputColumnName ?? name, numberOfBits, seed, useOrderedHashing, maximumNumberOfInverts); OutputKind = outputKind; }
/// <summary> /// Ensures the provided <paramref name="samplingKeyColumn"/> is valid for <see cref="RangeFilter"/>, hashing it if necessary, or creates a new column <paramref name="samplingKeyColumn"/> is null. /// </summary> internal static void EnsureGroupPreservationColumn(IHostEnvironment env, ref IDataView data, ref string samplingKeyColumn, int?seed = null) { Contracts.CheckValue(env, nameof(env)); // We need to handle two cases: if samplingKeyColumn is provided, we use hashJoin to // build a single hash of it. If it is not, we generate a random number. if (samplingKeyColumn == null) { samplingKeyColumn = data.Schema.GetTempColumnName("SamplingKeyColumn"); data = new GenerateNumberTransform(env, data, samplingKeyColumn, (uint?)(seed ?? ((ISeededEnvironment)env).Seed)); } else { if (!data.Schema.TryGetColumnIndex(samplingKeyColumn, out int stratCol)) { throw env.ExceptSchemaMismatch(nameof(samplingKeyColumn), "SamplingKeyColumn", samplingKeyColumn); } var type = data.Schema[stratCol].Type; if (!RangeFilter.IsValidRangeFilterColumnType(env, type)) { // Hash the samplingKeyColumn. // REVIEW: this could currently crash, since Hash only accepts a limited set // of column types. It used to be HashJoin, but we should probably extend Hash // instead of having two hash transformations. var origStratCol = samplingKeyColumn; samplingKeyColumn = data.Schema.GetTempColumnName(samplingKeyColumn); HashingEstimator.ColumnOptionsInternal columnOptions; if (seed.HasValue) { columnOptions = new HashingEstimator.ColumnOptionsInternal(samplingKeyColumn, origStratCol, 30, (uint)seed.Value); } else if (((ISeededEnvironment)env).Seed.HasValue) { columnOptions = new HashingEstimator.ColumnOptionsInternal(samplingKeyColumn, origStratCol, 30, (uint)((ISeededEnvironment)env).Seed.Value); } else { columnOptions = new HashingEstimator.ColumnOptionsInternal(samplingKeyColumn, origStratCol, 30); } data = new HashingEstimator(env, columnOptions).Fit(data).Transform(data); } else { if (!data.Schema[samplingKeyColumn].IsNormalized() && (type == NumberDataViewType.Single || type == NumberDataViewType.Double)) { var origStratCol = samplingKeyColumn; samplingKeyColumn = data.Schema.GetTempColumnName(samplingKeyColumn); data = new NormalizingEstimator(env, new NormalizingEstimator.MinMaxColumnOptions(samplingKeyColumn, origStratCol, ensureZeroUntouched: true)).Fit(data).Transform(data); } } } }
private void InitMap <T>(T val, DataViewType type, int numberOfBits = 20, ValueGetter <T> getter = null) { if (getter == null) { getter = (ref T dst) => dst = val; } _inRow = RowImpl.Create(type, getter); // One million features is a nice, typical number. var info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: numberOfBits); var xf = new HashingTransformer(_env, new[] { info }); var mapper = ((ITransformer)xf).GetRowToRowMapper(_inRow.Schema); var column = mapper.OutputSchema["Bar"]; var outRow = mapper.GetRow(_inRow, column); if (type is VectorDataViewType) { _vecGetter = outRow.GetGetter <VBuffer <uint> >(column); } else { _getter = outRow.GetGetter <uint>(column); } }
private void HashTestCore <T>(T val, PrimitiveDataViewType type, uint expected, uint expectedOrdered, uint expectedOrdered3) { const int bits = 10; var builder = new DataViewSchema.Annotations.Builder(); builder.AddPrimitiveValue("Foo", type, val); var inRow = AnnotationUtils.AnnotationsAsRow(builder.ToAnnotations()); //helper ValueGetter <TType> hashGetter <TType>(HashingEstimator.ColumnOptionsInternal colInfo) { var xf = new HashingTransformer(Env, new[] { colInfo }); var mapper = ((ITransformer)xf).GetRowToRowMapper(inRow.Schema); var col = mapper.OutputSchema["Bar"]; var outRow = mapper.GetRow(inRow, col); return(outRow.GetGetter <TType>(col)); }; // First do an unordered hash. var info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits); var getter = hashGetter <uint>(info); uint result = 0; getter(ref result); Assert.Equal(expected, result); // Next do an ordered hash. info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits, useOrderedHashing: true); getter = hashGetter <uint>(info); getter(ref result); Assert.Equal(expectedOrdered, result); // Next build up a vector to make sure that hashing is consistent between scalar values // at least in the first position, and in the unordered case, the last position. const int vecLen = 5; var denseVec = new VBuffer <T>(vecLen, Utils.CreateArray(vecLen, val)); builder = new DataViewSchema.Annotations.Builder(); builder.Add("Foo", new VectorDataViewType(type, vecLen), (ref VBuffer <T> dst) => denseVec.CopyTo(ref dst)); inRow = AnnotationUtils.AnnotationsAsRow(builder.ToAnnotations()); info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits, useOrderedHashing: false); var vecGetter = hashGetter <VBuffer <uint> >(info); VBuffer <uint> vecResult = default; vecGetter(ref vecResult); Assert.Equal(vecLen, vecResult.Length); // They all should equal this in this case. Assert.All(vecResult.DenseValues(), v => Assert.Equal(expected, v)); // Now do ordered with the dense vector. info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits, useOrderedHashing: true); vecGetter = hashGetter <VBuffer <uint> >(info); vecGetter(ref vecResult); Assert.Equal(vecLen, vecResult.Length); Assert.Equal(expectedOrdered, vecResult.GetItemOrDefault(0)); Assert.Equal(expectedOrdered3, vecResult.GetItemOrDefault(3)); Assert.All(vecResult.DenseValues(), v => Assert.True((v == 0) == (expectedOrdered == 0))); // Let's now do a sparse vector. var sparseVec = new VBuffer <T>(10, 3, Utils.CreateArray(3, val), new[] { 0, 3, 7 }); builder = new DataViewSchema.Annotations.Builder(); builder.Add("Foo", new VectorDataViewType(type, vecLen), (ref VBuffer <T> dst) => sparseVec.CopyTo(ref dst)); inRow = AnnotationUtils.AnnotationsAsRow(builder.ToAnnotations()); info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits, useOrderedHashing: false); vecGetter = hashGetter <VBuffer <uint> >(info); vecGetter(ref vecResult); Assert.Equal(10, vecResult.Length); Assert.Equal(expected, vecResult.GetItemOrDefault(0)); Assert.Equal(expected, vecResult.GetItemOrDefault(3)); Assert.Equal(expected, vecResult.GetItemOrDefault(7)); info = new HashingEstimator.ColumnOptionsInternal("Bar", "Foo", numberOfBits: bits, useOrderedHashing: true); vecGetter = hashGetter <VBuffer <uint> >(info); vecGetter(ref vecResult); Assert.Equal(10, vecResult.Length); Assert.Equal(expectedOrdered, vecResult.GetItemOrDefault(0)); Assert.Equal(expectedOrdered3, vecResult.GetItemOrDefault(3)); }