private string Sample(int targetLength, string prime = null) { Variable inputVariable = Model.Arguments[0]; Variable outputVariable = Model.Output; var inputs = new Arguments(); var outputs = new Arguments(); // uses a random recommended char if no prime is provided if (string.IsNullOrEmpty(prime)) { prime = Vocab.GetRandomSamplePrefix.ToString(); } var sequence = new List <float>(Vocab.CharCount * targetLength); char[] result = new char[targetLength]; // load the prefix into the sample int i = 0, charIndex = 0; for (; i < prime.Length; i++) { charIndex = Vocab.Encode(prime[i]); UpdateAndEvaluateModel(); result[i] = prime[i]; } // fill the remainder of the sample with suggested chars for (; i < targetLength; i++) { var outputData = outputs[outputVariable].GetDenseData <float>(outputVariable)[0] as List <float>; charIndex = GetSuggestedIndex(outputData, i); UpdateAndEvaluateModel(); // add the suggest char to the result string result[i] = Vocab.Decode(charIndex); } void UpdateAndEvaluateModel() { // map the used char to the sequence // - using a List decreases the round trip times float[] input = new float[Vocab.CharCount]; input[charIndex] = 1; sequence.AddRange(input); // re-evaluate the model inputs[inputVariable] = Value.CreateSequence(inputVariable.Shape, sequence, Device); outputs[outputVariable] = null; Model.Evaluate(inputs, outputs, Device); } return(new string(result)); }