|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.Data.DataView; |
| 6 | +using Microsoft.ML.Transforms.TensorFlow; |
| 7 | + |
| 8 | +namespace Microsoft.ML.Transforms |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// This class holds the information related to TensorFlow model and session. |
| 12 | + /// It provides some convenient methods to query model schema as well as |
| 13 | + /// creation of <see cref="TensorFlowEstimator"/> object. |
| 14 | + /// </summary> |
| 15 | + public sealed class TensorFlowModel |
| 16 | + { |
| 17 | + internal TFSession Session { get; } |
| 18 | + internal string ModelPath { get; } |
| 19 | + |
| 20 | + private readonly IHostEnvironment _env; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Instantiates <see cref="TensorFlowModel"/>. |
| 24 | + /// </summary> |
| 25 | + /// <param name="env">An <see cref="IHostEnvironment"/> object.</param> |
| 26 | + /// <param name="session">TensorFlow session object.</param> |
| 27 | + /// <param name="modelLocation">Location of the model from where <paramref name="session"/> was loaded.</param> |
| 28 | + internal TensorFlowModel(IHostEnvironment env, TFSession session, string modelLocation) |
| 29 | + { |
| 30 | + Session = session; |
| 31 | + ModelPath = modelLocation; |
| 32 | + _env = env; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Get <see cref="DataViewSchema"/> for complete model. Every node in the TensorFlow model will be included in the <see cref="DataViewSchema"/> object. |
| 37 | + /// </summary> |
| 38 | + public DataViewSchema GetModelSchema() |
| 39 | + { |
| 40 | + return TensorFlowUtils.GetModelSchema(_env, Session.Graph); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Get <see cref="DataViewSchema"/> for only those nodes which are marked "Placeholder" in the TensorFlow model. |
| 45 | + /// This method is convenient for exploring the model input(s) in case TensorFlow graph is very large. |
| 46 | + /// </summary> |
| 47 | + public DataViewSchema GetInputSchema() |
| 48 | + { |
| 49 | + return TensorFlowUtils.GetModelSchema(_env, Session.Graph, "Placeholder"); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Scores a dataset using a pre-traiend <a href="https://www.tensorflow.org/">TensorFlow</a> model. |
| 54 | + /// </summary> |
| 55 | + /// <param name="inputColumnName"> The name of the model input.</param> |
| 56 | + /// <param name="outputColumnName">The name of the requested model output.</param> |
| 57 | + /// <example> |
| 58 | + /// <format type="text/markdown"> |
| 59 | + /// <] |
| 61 | + /// ]]> |
| 62 | + /// </format> |
| 63 | + /// </example> |
| 64 | + public TensorFlowEstimator ScoreTensorFlowModel(string outputColumnName, string inputColumnName) |
| 65 | + => new TensorFlowEstimator(_env, new[] { outputColumnName }, new[] { inputColumnName }, ModelPath); |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Scores a dataset using a pre-traiend TensorFlow model. |
| 69 | + /// </summary> |
| 70 | + /// <param name="inputColumnNames"> The names of the model inputs.</param> |
| 71 | + /// <param name="outputColumnNames">The names of the requested model outputs.</param> |
| 72 | + /// <example> |
| 73 | + /// <format type="text/markdown"> |
| 74 | + /// <] |
| 76 | + /// ]]> |
| 77 | + /// </format> |
| 78 | + /// </example> |
| 79 | + public TensorFlowEstimator ScoreTensorFlowModel(string[] outputColumnNames, string[] inputColumnNames) |
| 80 | + => new TensorFlowEstimator(_env, outputColumnNames, inputColumnNames, ModelPath); |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Retrain the TensorFlow model on new data. |
| 84 | + /// The model is not loaded again instead the information contained in <see cref="TensorFlowModel"/> class is reused |
| 85 | + /// (c.f. <see cref="TensorFlowModel.ModelPath"/> and <see cref="TensorFlowModel.Session"/>). |
| 86 | + /// </summary> |
| 87 | + /// <param name="inputColumnNames"> The names of the model inputs.</param> |
| 88 | + /// <param name="outputColumnNames">The names of the requested model outputs.</param> |
| 89 | + /// <param name="labelColumnName">Name of the label column.</param> |
| 90 | + /// <param name="tensorFlowLabel">Name of the node in TensorFlow graph that is used as label during training in TensorFlow. |
| 91 | + /// The value of <paramref name="labelColumnName"/> from <see cref="IDataView"/> is fed to this node.</param> |
| 92 | + /// <param name="optimizationOperation">The name of the optimization operation in the TensorFlow graph.</param> |
| 93 | + /// <param name="epoch">Number of training iterations.</param> |
| 94 | + /// <param name="batchSize">Number of samples to use for mini-batch training.</param> |
| 95 | + /// <param name="lossOperation">The name of the operation in the TensorFlow graph to compute training loss (Optional).</param> |
| 96 | + /// <param name="metricOperation">The name of the operation in the TensorFlow graph to compute performance metric during training (Optional).</param> |
| 97 | + /// <param name="learningRateOperation">The name of the operation in the TensorFlow graph which sets optimizer learning rate (Optional).</param> |
| 98 | + /// <param name="learningRate">Learning rate to use during optimization (Optional).</param> |
| 99 | + /// <remarks> |
| 100 | + /// The support for retraining is experimental. |
| 101 | + /// </remarks> |
| 102 | + public TensorFlowEstimator RetrainTensorFlowModel( |
| 103 | + string[] outputColumnNames, |
| 104 | + string[] inputColumnNames, |
| 105 | + string labelColumnName, |
| 106 | + string tensorFlowLabel, |
| 107 | + string optimizationOperation, |
| 108 | + int epoch = 10, |
| 109 | + int batchSize = 20, |
| 110 | + string lossOperation= null, |
| 111 | + string metricOperation = null, |
| 112 | + string learningRateOperation = null, |
| 113 | + float learningRate = 0.01f) |
| 114 | + { |
| 115 | + var options = new TensorFlowEstimator.Options() |
| 116 | + { |
| 117 | + ModelLocation = ModelPath, |
| 118 | + InputColumns = inputColumnNames, |
| 119 | + OutputColumns = outputColumnNames, |
| 120 | + LabelColumn = labelColumnName, |
| 121 | + TensorFlowLabel = tensorFlowLabel, |
| 122 | + OptimizationOperation = optimizationOperation, |
| 123 | + LossOperation = lossOperation, |
| 124 | + MetricOperation = metricOperation, |
| 125 | + Epoch = epoch, |
| 126 | + LearningRateOperation = learningRateOperation, |
| 127 | + LearningRate = learningRate, |
| 128 | + BatchSize = batchSize, |
| 129 | + ReTrain = true |
| 130 | + }; |
| 131 | + return new TensorFlowEstimator(_env, options, this); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments