public virtual void TestElementwiseDenseToSparse(double[] dense1, int sparseIndex, double v) { Assume.AssumeTrue(sparseIndex >= 0); Assume.AssumeTrue(sparseIndex <= 100); ConcatVector v1 = new ConcatVector(1); v1.SetDenseComponent(0, dense1); ConcatVector v2 = new ConcatVector(1); v2.SetSparseComponent(0, (int)sparseIndex, v); v2.ElementwiseProductInPlace(v1); for (int i = 0; i < dense1.Length; i++) { double expected = 0.0f; if (i == sparseIndex) { expected = dense1[i] * v; } NUnit.Framework.Assert.AreEqual(v2.GetValueAt(0, i), 5.0e-4, expected); } }
public override ConcatVector[][][] Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus) { int l = sourceOfRandomness.NextInt(10) + 1; int m = sourceOfRandomness.NextInt(10) + 1; int n = sourceOfRandomness.NextInt(10) + 1; ConcatVector[][][] factor3 = new ConcatVector[l][][]; for (int i = 0; i < factor3.Length; i++) { for (int j = 0; j < factor3[0].Length; j++) { for (int k = 0; k < factor3[0][0].Length; k++) { int numComponents = sourceOfRandomness.NextInt(7); ConcatVector v = new ConcatVector(numComponents); for (int x = 0; x < numComponents; x++) { if (sourceOfRandomness.NextBoolean()) { v.SetSparseComponent(x, sourceOfRandomness.NextInt(32), sourceOfRandomness.NextDouble()); } else { double[] val = new double[sourceOfRandomness.NextInt(12)]; for (int y = 0; y < val.Length; y++) { val[y] = sourceOfRandomness.NextDouble(); } v.SetDenseComponent(x, val); } } factor3[i][j][k] = v; } } } return(factor3); }
/// <summary> /// This adds a sparse feature to a vector, setting the appropriate component of the given vector to the passed in /// value. /// </summary> /// <param name="vector">the vector</param> /// <param name="featureName">the feature whose value to set</param> /// <param name="index">the index of the one-hot vector to set, as a string, which we will translate into a mapping</param> /// <param name="value">the value we want to set this one-hot index to</param> public virtual void SetSparseFeature(ConcatVector vector, string featureName, string index, double value) { vector.SetSparseComponent(EnsureFeature(featureName), EnsureSparseFeature(featureName, index), value); }