public void EvalManagedConstantNetworkTest() { string modelDefinition = @"precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ v1 = Constant(1) v2 = Constant(2, tag=""output"") ol = Plus(v1, v2, tag=""output"") FeatureNodes = (v1) ]"; using (var model = new ModelEvaluationExtendedF()) { model.CreateNetwork(modelDefinition); VariableSchema outputSchema = model.GetOutputSchema(); model.StartForwardEvaluation(outputSchema.Select(s => s.Name).ToList <string>()); var outputBuffer = outputSchema.CreateBuffers <float>(); var inputBuffer = new ValueBuffer <float> [0]; // We can call the evaluate method and get back the results... model.ForwardPass(inputBuffer, outputBuffer); float[][] expected = { new float[] { 2 }, new float[] { 3 } }; Assert.AreEqual(expected.Length, outputBuffer.Length); for (int idx = 0; idx < expected.Length; idx++) { CollectionAssert.AreEqual(expected[idx], outputBuffer[idx].Buffer); } } }
/// <summary> /// Evaluates an extended network (without a model and without input) and obtains a single layer output /// </summary> private static void EvaluateExtendedNetworkSingleLayerNoInput() { const string modelDefinition = @"precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ v1 = Constant(1) v2 = Constant(2, tag=""output"") ol = Plus(v1, v2, tag=""output"") FeatureNodes = (v1) ]"; try { // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the <CNTK>/x64/Debug|Release folder string workingDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Other\Simple2d\Config"); Environment.CurrentDirectory = initialDirectory; using (var model = new ModelEvaluationExtendedF()) { // Create the network // This network (AddOperatorConstantNoInput.cntk) is a simple network consisting of a single binary operator (Plus) // operating over a two constants, therefore no input is necessary. model.CreateNetwork(modelDefinition); VariableSchema outputSchema = model.GetOutputSchema(); var outputNodeNames = outputSchema.Select(s => s.Name).ToList <string>(); model.StartForwardEvaluation(outputNodeNames); var outputBuffer = outputSchema.CreateBuffers <float>(); var inputBuffer = new ValueBuffer <float> [0]; // We can call the evaluate method and get back the results... model.ForwardPass(inputBuffer, outputBuffer); // We expect two outputs: the v2 constant, and the ol Plus result var expected = new float[][] { new float[] { 2 }, new float[] { 3 } }; Console.WriteLine("Expected values: {0}", string.Join(" - ", expected.Select(b => string.Join(", ", b)).ToList <string>())); Console.WriteLine("Actual Values : {0}", string.Join(" - ", outputBuffer.Select(b => string.Join(", ", b.Buffer)).ToList <string>())); } } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } catch (Exception ex) { Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } }
/// <summary> /// Gets specified version of the schema for the variable describing its structure. /// </summary> public override VariableSchema GetSchema(SchemaVersion version) { if (HasChanges) { if (version == SchemaVersion.Committed) { return(changes.InitialSchema); } // Making a schema for proposed version of the variable string name = changes.Name == null ? this.name : changes.Name; ReadOnlyDimensionList roDims = new ReadOnlyDimensionList(changes.Dimensions == null ? dims : changes.Dimensions); VariableMetadata metadata = this.metadata.Clone(); CoordinateSystemCollection cs = changes.Cs == null ? csystems : changes.Cs; CoordinateSystemSchema[] csSchemas = new CoordinateSystemSchema[cs.Count]; for (int i = 0; i < csSchemas.Length; i++) { csSchemas[i] = cs[i].GetSchema(); } VariableSchema schema = new VariableSchema(name, TypeOfData, roDims, csSchemas, metadata); return(schema); } else { if (version == SchemaVersion.Proposed) { throw new Exception("Variable is commited and has no changes."); } CoordinateSystemSchema[] csSchemas = new CoordinateSystemSchema[csystems.Count]; for (int i = 0; i < csSchemas.Length; i++) { csSchemas[i] = csystems[i].GetSchema(); } VariableSchema schema = new VariableSchema( name, TypeOfData, new ReadOnlyDimensionList(dimensions), csSchemas, metadata.Clone()); return(schema); } }
public void EvalManagedSparseTimesTest() { string modelDefinition = @"deviceId = -1 precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ i1 = SparseInput(3) o1 = Times(Constant(2, rows=1, cols=3), i1, tag=""output"") FeatureNodes = (i1) ]"; using (var model = new ModelEvaluationExtendedF()) { model.CreateNetwork(modelDefinition); VariableSchema outputSchema = model.GetOutputSchema(); model.StartForwardEvaluation(outputSchema.Select(s => s.Name).ToList <string>()); var outputBuffer = new [] { new ValueBuffer <float>() { Buffer = new float[3], Size = 3 } }; var inputBuffer = new [] { new ValueBuffer <float>() { Buffer = new float[] { 1, 2, 3, 5, 6 }, Indices = new [] { 0, 2, 2, 1, 2 }, ColIndices = new [] { 0, 2, 2, 5 }, Size = 4 } }; // We can call the evaluate method and get back the results... model.ForwardPass(inputBuffer, outputBuffer); float[][] expected = { new float[] { 6, 0, 28 } }; Assert.AreEqual(expected.Length, outputBuffer.Length); for (int idx = 0; idx < expected.Length; idx++) { CollectionAssert.AreEqual(expected[idx], outputBuffer[idx].Buffer); } } }
/// <summary> /// Evaluates an extended network (without a model and without input) and obtains a single layer output /// </summary> private static void EvaluateExtendedNetworkSingleLayerNoInput() { const string modelDefinition = @"precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ v1 = Constant(1) v2 = Constant(2, tag=""output"") ol = Plus(v1, v2, tag=""output"") FeatureNodes = (v1) ]"; try { using (var model = new ModelEvaluationExtendedF()) { // Create the network model.CreateNetwork(modelDefinition); VariableSchema outputSchema = model.GetOutputSchema(); var outputNodeNames = outputSchema.Select(s => s.Name).ToList <string>(); model.StartForwardEvaluation(outputNodeNames); var outputBuffer = outputSchema.CreateBuffers <float>(); var inputBuffer = new ValueBuffer <float> [0]; // We can call the evaluate method and get back the results... model.ForwardPass(inputBuffer, outputBuffer); // We expect two outputs: the v2 constant, and the ol Plus result var expected = new float[][] { new float[] { 2 }, new float[] { 3 } }; Console.WriteLine("Expected values: {0}", string.Join(" - ", expected.Select(b => string.Join(", ", b)).ToList <string>())); Console.WriteLine("Actual Values : {0}", string.Join(" - ", outputBuffer.Select(b => string.Join(", ", b.Buffer)).ToList <string>())); } } catch (CNTKException ex) { Console.WriteLine("Error: {0}\nNative CallStack: {1}\n Inner Exception: {2}", ex.Message, ex.NativeCallStack, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } catch (Exception ex) { Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); } }
void OnEnable() { _schema = target as VariableSchema; _proxy = new DefinitionsProxy { Schema = _schema }; _definitions = serializedObject.FindProperty("_definitions._items"); _createPopup.Setup(_addDefinitionLabel, PopupCreate, PopupValidate); _list.Setup(_proxy) .MakeDrawable(DrawDefinition) .MakeRemovable(_removeDefinitionButton, RemoveDefinition) .MakeReorderable() .MakeHeaderButton(_addDefinitionButton, _createPopup, Color.white) .MakeCustomHeight(GetDefinitionHeight) .MakeEmptyLabel(_emptyLabel); }
public void EvalManagedScalarTimesDualOutputTest() { string modelDefinition = @"deviceId = -1 precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder=[ i1 = Input(1) i2 = Input(1) o1 = Times(Constant(3), i1, tag=""output"") o2 = Times(Constant(5), i1, tag=""output"") FeatureNodes = (i1) ]"; using (var model = new ModelEvaluationExtendedF()) { model.CreateNetwork(modelDefinition); VariableSchema outputSchema = model.GetOutputSchema(); VariableSchema inputSchema = model.GetInputSchema(); model.StartForwardEvaluation(outputSchema.Select(s => s.Name).ToList <string>()); var outputBuffer = outputSchema.CreateBuffers <float>(); var inputBuffer = inputSchema.CreateBuffers <float>(); inputBuffer[0].Buffer[0] = 2; // We can call the evaluate method and get back the results... model.ForwardPass(inputBuffer, outputBuffer); float[][] expected = { new float[] { 6 }, new float[] { 10 } }; Assert.AreEqual(expected.Length, outputBuffer.Length); for (int idx = 0; idx < expected.Length; idx++) { CollectionAssert.AreEqual(expected[idx], outputBuffer[idx].Buffer); } } }
public void EvalManagedVariableSchemaTest() { VariableSchema sc = new VariableSchema(); var buffers = sc.CreateBuffers <float>(); Assert.AreEqual(0, buffers.Length); sc.Add(new VariableLayout() { DataType = DataType.Float32, Name = "A", NumElements = 5, StorageType = StorageType.Dense }); buffers = sc.CreateBuffers <float>(); Assert.AreEqual(5, buffers[0].Buffer.Length); sc.Add(new VariableLayout() { DataType = DataType.Float32, Name = "B", NumElements = 10, StorageType = StorageType.Sparse }); buffers = sc.CreateBuffers <float>(); Assert.AreEqual(10, buffers[1].Buffer.Length); // Although sparse, the Indices and ColIndices are not allocated Assert.AreEqual(null, buffers[1].Indices); Assert.AreEqual(null, buffers[1].ColIndices); }
public void EvalManagedRNNTest() { string modelDefinition = @"deviceId = -1 precision = ""float"" traceLevel = 1 run=NDLNetworkBuilder NDLNetworkBuilder = [ LSTMComponent(inputDim, outputDim, cellDim, inputx, cellDimX2, cellDimX3, cellDimX4) = [ wx = Parameter(cellDimX4, 0, init = ""uniform"", initValueScale = 1); b = Parameter(cellDimX4, 1, init = ""fixedValue"", value = 0.0); Wh = Parameter(cellDimX4, 0, init = ""uniform"", initValueScale = 1); Wci = Parameter(cellDim, init = ""uniform"", initValueScale = 1); Wcf = Parameter(cellDim, init = ""uniform"", initValueScale = 1); Wco = Parameter(cellDim, init = ""uniform"", initValueScale = 1); dh = PastValue(outputDim, output, timeStep = 1); dc = PastValue(cellDim, ct, timeStep = 1); wxx = Times(wx, inputx); wxxpb = Plus(wxx, b); whh = Times(wh, dh); wxxpbpwhh = Plus(wxxpb, whh) G1 = RowSlice(0, cellDim, wxxpbpwhh) G2 = RowSlice(cellDim, cellDim, wxxpbpwhh) G3 = RowSlice(cellDimX2, cellDim, wxxpbpwhh); G4 = RowSlice(cellDimX3, cellDim, wxxpbpwhh); Wcidc = DiagTimes(Wci, dc); it = Sigmoid(Plus(G1, Wcidc)); bit = ElementTimes(it, Tanh(G2)); Wcfdc = DiagTimes(Wcf, dc); ft = Sigmoid(Plus(G3, Wcfdc)); bft = ElementTimes(ft, dc); ct = Plus(bft, bit); Wcoct = DiagTimes(Wco, ct); ot = Sigmoid(Plus(G4, Wcoct)); mt = ElementTimes(ot, Tanh(ct)); Wmr = Parameter(outputDim, cellDim, init = ""uniform"", initValueScale = 1); output = Times(Wmr, mt); ] i1 = Input(4) o1 = LSTMComponent(4, 4, 1, i1, 2, 3, 4) FeatureNodes = (i1) outputNodes = (o1) ]"; using (var model = new ModelEvaluationExtendedF()) { int featDim = 4; int labelDim = 4; model.CreateNetwork(modelDefinition); VariableSchema inputSchema = model.GetInputSchema(); VariableSchema outputSchema = model.GetOutputSchema(); model.StartForwardEvaluation(outputSchema.Select(s => s.Name).ToList <string>()); // Allocate the output values layer var outputBuffer = outputSchema.CreateBuffers <float>(); var inputBuffer = inputSchema.CreateBuffers <float>(); for (var i = 0; i < featDim; i++) { inputBuffer[0].Buffer[i] = (float)i; } int scaler = 100000; var result = new int[labelDim]; int[] expected = { 50, 10, 54, 55 }; // the first pass with reset model.ForwardPass(inputBuffer, outputBuffer); for (var i = 0; i < labelDim; i++) { result[i] = (int)(outputBuffer[0].Buffer[i] * scaler); } CollectionAssert.AreEqual(expected, result); // the second pass with reset model.ForwardPass(inputBuffer, outputBuffer); for (var i = 0; i < labelDim; i++) { result[i] = (int)(outputBuffer[0].Buffer[i] * scaler); } CollectionAssert.AreEqual(expected, result); // another pass with reset model.ForwardPass(inputBuffer, outputBuffer, true); for (var i = 0; i < labelDim; i++) { result[i] = (int)(outputBuffer[0].Buffer[i] * scaler); } CollectionAssert.AreEqual(expected, result); // pass w/o reset model.ForwardPass(inputBuffer, outputBuffer, false); for (var i = 0; i < labelDim; i++) { result[i] = (int)(outputBuffer[0].Buffer[i] * scaler); } expected = new int[] { 13, 2, 14, 14 }; CollectionAssert.AreEqual(expected, result); // another pass w/o reset model.ForwardPass(inputBuffer, outputBuffer, false); for (var i = 0; i < labelDim; i++) { result[i] = (int)(outputBuffer[0].Buffer[i] * scaler); } expected = new int[] { -4, 0, -4, -4 }; CollectionAssert.AreEqual(expected, result); } }
public static DataSet Clone(DataSet src, DataSetUri dstUri) { if (src == null) { throw new ArgumentNullException("src"); } // Maximum memory capacity in bytes int N = 200 * 1024 * 1024; // Estimated size of a single string in bytes int sizeofString = 100 * 1024; /*********************************************************************************** * Preparing output ***********************************************************************************/ DataSet dst = DataSet.Open(dstUri); if (dst.IsReadOnly) { throw new NotSupportedException("Output DataSet is read-only"); } dst.IsAutocommitEnabled = false; DataSetSchema srcSchema = src.GetSchema(); Dictionary <int, int> IDs = new Dictionary <int, int>(); // Creating empty variables and copying global metadata and scalar variables Console.Out.Write("\n\nCreating structure and copying global metadata and scalar variables... "); foreach (VariableSchema v in srcSchema.Variables) { if (v.ID == DataSet.GlobalMetadataVariableID) { // Copying global metadata var dstGlobalMetadata = dst.Metadata; foreach (var attr in v.Metadata) { dstGlobalMetadata[attr.Key] = attr.Value; } continue; } Variable t = dst.AddVariable(v.TypeOfData, v.Name, null, v.Dimensions.AsNamesArray()); IDs.Add(v.ID, t.ID); foreach (var attr in v.Metadata) { t.Metadata[attr.Key] = attr.Value; } if (t.Rank == 0) // scalar { t.PutData(src.Variables.GetByID(v.ID).GetData()); } } dst.Commit(); Console.Out.WriteLine("Done.\n"); /*********************************************************************************** * Adjusting dimensions deltas ***********************************************************************************/ Dimension[] srcDims = srcSchema.GetDimensions(); Dictionary <string, int> deltas = new Dictionary <string, int>(srcDims.Length); foreach (var d in srcDims) { deltas[d.Name] = d.Length; } Console.Out.WriteLine("Total memory capacity: " + (N / 1024.0 / 1024.0).ToString("F2") + " Mb"); int totalSize; do { totalSize = 0; foreach (var var in srcSchema.Variables) { if (var.Rank == 0) { continue; // scalar } int typeSize = SizeOf(var.TypeOfData, sizeofString); int count = 0; foreach (var vdim in var.Dimensions) { int dimDelta = deltas[vdim.Name]; if (count == 0) { count = dimDelta; } else { count *= dimDelta; } } totalSize += typeSize * count; } if (totalSize > N) { string maxDim = null; int max = int.MinValue; foreach (var dim in deltas) { if (dim.Value > max) { max = dim.Value; maxDim = dim.Key; } } if (maxDim == null || max <= 1) { throw new NotSupportedException("Cannot copy the DataSet: it is too large to be copied entirely by the utility for the provided memory capacity"); } deltas[maxDim] = max >> 1; } } while (totalSize > N); // Printing deltas Console.Out.WriteLine("Deltas for the dimensions adjusted (max iteration capacity: " + (totalSize / 1024.0 / 1024.0).ToString("F2") + " Mb):"); foreach (var delta in deltas) { Console.Out.WriteLine(" Dimension " + delta.Key + ": " + delta.Value); } /*********************************************************************************** * Copying data ***********************************************************************************/ Console.WriteLine(); UpdateProgress(0); Dictionary <int, int[]> origins = new Dictionary <int, int[]>(srcSchema.Variables.Length); Dictionary <int, int[]> shapes = new Dictionary <int, int[]>(srcSchema.Variables.Length); List <VariableSchema> copyVars = srcSchema.Variables.Where(vs => (vs.Rank > 0 && vs.ID != DataSet.GlobalMetadataVariableID)).ToList(); Dictionary <string, int> dimOrigin = new Dictionary <string, int>(srcDims.Length); foreach (var d in srcDims) { dimOrigin[d.Name] = 0; } Array.Sort(srcDims, (d1, d2) => d1.Length - d2.Length); int totalDims = srcDims.Length; do { // for each variable: for (int varIndex = copyVars.Count; --varIndex >= 0;) { VariableSchema var = copyVars[varIndex]; bool hasChanged = false; // Getting its origin int[] origin; if (!origins.TryGetValue(var.ID, out origin)) { origin = new int[var.Rank]; origins[var.ID] = origin; hasChanged = true; } // Getting its shape int[] shape; if (!shapes.TryGetValue(var.ID, out shape)) { shape = new int[var.Rank]; for (int i = 0; i < var.Dimensions.Count; i++) { shape[i] = deltas[var.Dimensions[i].Name]; } shapes.Add(var.ID, shape); } // Updating origin for the variable: if (!hasChanged) { for (int i = 0; i < shape.Length; i++) { int o = dimOrigin[var.Dimensions[i].Name]; if (origin[i] != o) { hasChanged = true; origin[i] = o; } } } if (!hasChanged) // this block is already copied { continue; } bool doCopy = false; bool shapeUpdated = false; for (int i = 0; i < shape.Length; i++) { int s = origin[i] + shape[i]; int len = var.Dimensions[i].Length; if (s > len) { if (!shapeUpdated) { shapeUpdated = true; shape = (int[])shape.Clone(); } shape[i] = len - origin[i]; } if (shape[i] > 0) { doCopy = true; } } if (doCopy) { Array data = src.Variables.GetByID(var.ID).GetData(origin, shape); // Compute real size here for strings dst.Variables.GetByID(IDs[var.ID]).PutData(origin, data); } else // variable is copied { copyVars.RemoveAt(varIndex); } } dst.Commit(); // Updating dimensions origin bool isOver = true; for (int i = 0; i < totalDims; i++) { Dimension dim = srcDims[i]; int origin = dimOrigin[dim.Name] + deltas[dim.Name]; if (origin < dim.Length) { dimOrigin[dim.Name] = origin; isOver = false; // Progress indicator if (i == totalDims - 1) { double perc = (double)origin / dim.Length * 100.0; UpdateProgress(perc); } break; } dimOrigin[dim.Name] = 0; } if (isOver) { break; } } while (copyVars.Count > 0); UpdateProgress(100.0); Console.Out.WriteLine(); return(dst); }
public ConstrainedStore(VariableSchema schema) { Schema = schema; SetupSchema(); }
public void EvalManagedVariableSchemaTest() { VariableSchema sc = new VariableSchema(); var buffers = sc.CreateBuffers<float>(); Assert.AreEqual(0, buffers.Length); sc.Add(new VariableLayout(){DataType=DataType.Float32, Name="A", NumElements=5, StorageType = StorageType.Dense}); buffers = sc.CreateBuffers<float>(); Assert.AreEqual(5, buffers[0].Buffer.Length); sc.Add(new VariableLayout() { DataType = DataType.Float32, Name = "B", NumElements = 10, StorageType = StorageType.Sparse}); buffers = sc.CreateBuffers<float>(); Assert.AreEqual(10, buffers[1].Buffer.Length); // Although sparse, the Indices and ColIndices are not allocated Assert.AreEqual(null, buffers[1].Indices); Assert.AreEqual(null, buffers[1].ColIndices); }