// </SnippetDeclareGlobalVariables> static void Main(string[] args) { // Create MLContext to be shared across the model creation workflow objects // <SnippetCreateMLContext> MLContext mlContext = new MLContext(); // </SnippetCreateMLContext> // Dictionary to encode words as integers. // <SnippetCreateLookupMap> var lookupMap = mlContext.Data.LoadFromTextFile(Path.Combine(_modelPath, "imdb_word_index.csv"), columns: new[] { new TextLoader.Column("Words", DataKind.String, 0), new TextLoader.Column("Ids", DataKind.Int32, 1), }, separatorChar: ',' ); // </SnippetCreateLookupMap> // The model expects the input feature vector to be a fixed length vector. // This action resizes the integer vector to a fixed length vector. If there // are less than 600 words in the sentence, the remaining indices will be filled // with zeros. If there are more than 600 words in the sentence, then the // array is truncated at 600. // <SnippetResizeFeatures> Action <MovieReview, FixedLengthFeatures> ResizeFeaturesAction = (s, f) => { var features = s.VariableLengthFeatures; Array.Resize(ref features, FeatureLength); f.Features = features; }; // </SnippetResizeFeatures> // Load the TensorFlow model. // <SnippetLoadTensorFlowModel> TensorFlowModel tensorFlowModel = mlContext.Model.LoadTensorFlowModel(_modelPath); // </SnippetLoadTensorFlowModel> // <SnippetGetModelSchema> DataViewSchema schema = tensorFlowModel.GetModelSchema(); Console.WriteLine(" =============== TensorFlow Model Schema =============== "); var featuresType = (VectorDataViewType)schema["Features"].Type; Console.WriteLine($"Name: Features, Type: {featuresType.ItemType.RawType}, Size: ({featuresType.Dimensions[0]})"); var predictionType = (VectorDataViewType)schema["Prediction/Softmax"].Type; Console.WriteLine($"Name: Prediction/Softmax, Type: {predictionType.ItemType.RawType}, Size: ({predictionType.Dimensions[0]})"); // </SnippetGetModelSchema> // <SnippetTokenizeIntoWords> IEstimator <ITransformer> pipeline = // Split the text into individual words mlContext.Transforms.Text.TokenizeIntoWords("TokenizedWords", "ReviewText") // </SnippetTokenizeIntoWords> // <SnippetMapValue> // Map each word to an integer value. The array of integer makes up the input features. .Append(mlContext.Transforms.Conversion.MapValue("VariableLengthFeatures", lookupMap, lookupMap.Schema["Words"], lookupMap.Schema["Ids"], "TokenizedWords")) // </SnippetMapValue> // <SnippetCustomMapping> // Resize variable length vector to fixed length vector. .Append(mlContext.Transforms.CustomMapping(ResizeFeaturesAction, "Resize")) // </SnippetCustomMapping> // <SnippetScoreTensorFlowModel> // Passes the data to TensorFlow for scoring .Append(tensorFlowModel.ScoreTensorFlowModel("Prediction/Softmax", "Features")) // </SnippetScoreTensorFlowModel> // <SnippetCopyColumns> // Retrieves the 'Prediction' from TensorFlow and and copies to a column .Append(mlContext.Transforms.CopyColumns("Prediction", "Prediction/Softmax")); // </SnippetCopyColumns> // <SnippetCreateModel> // Create an executable model from the estimator pipeline IDataView dataView = mlContext.Data.LoadFromEnumerable(new List <MovieReview>()); ITransformer model = pipeline.Fit(dataView); // </SnippetCreateModel> // <SnippetCallPredictSentiment> PredictSentiment(mlContext, model); // </SnippetCallPredictSentiment> }
/// <summary> /// Run a TensorFlow model provided through <paramref name="tensorFlowModel"/> on the input column and extract one output column. /// The inputs and outputs are matched to TensorFlow graph nodes by name. /// </summary> public static Vector <float> ApplyTensorFlowGraph(this Vector <float> input, TensorFlowModel tensorFlowModel) { Contracts.CheckValue(input, nameof(input)); Contracts.CheckValue(tensorFlowModel, nameof(tensorFlowModel)); return(new OutColumn(input, tensorFlowModel)); }
public Reconciler(string modelFile) { Contracts.AssertNonEmpty(modelFile); _modelFile = modelFile; _tensorFlowModel = null; }
public OutColumn(Vector <float> input, TensorFlowModel tensorFlowModel) : base(new Reconciler(tensorFlowModel), input) { Input = input; }
/// <summary> /// Run a TensorFlow model provided through <paramref name="tensorFlowModel"/> on the input column and extract one output column. /// The inputs and outputs are matched to TensorFlow graph nodes by name. /// </summary> public static Vector <float> ApplyTensorFlowGraph(this Vector <float> input, TensorFlowModel tensorFlowModel, bool addBatchDimensionInput = false) { Contracts.CheckValue(input, nameof(input)); Contracts.CheckValue(tensorFlowModel, nameof(tensorFlowModel)); return(new OutColumn(input, tensorFlowModel, addBatchDimensionInput)); }
public OutColumn(Vector <float> input, TensorFlowModel tensorFlowModel, bool addBatchDimensionInput) : base(new Reconciler(tensorFlowModel, addBatchDimensionInput), input) { Input = input; }