|
| 1 | +<!DOCTYPE html> |
| 2 | +<!--[if IE]><![endif]--> |
| 3 | +<html> |
| 4 | + |
| 5 | + <head> |
| 6 | + <meta charset="utf-8"> |
| 7 | + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 8 | + <title>Getting Started With TensorFlowSharp </title> |
| 9 | + <meta name="viewport" content="width=device-width"> |
| 10 | + <meta name="title" content="Getting Started With TensorFlowSharp "> |
| 11 | + <meta name="generator" content="docfx 2.18.2.0"> |
| 12 | + |
| 13 | + <link rel="shortcut icon" href="../favicon.ico"> |
| 14 | + <link rel="stylesheet" href="../styles/docfx.vendor.css"> |
| 15 | + <link rel="stylesheet" href="../styles/docfx.css"> |
| 16 | + <link rel="stylesheet" href="../styles/main.css"> |
| 17 | + <meta property="docfx:navrel" content="../toc.html"> |
| 18 | + <meta property="docfx:tocrel" content="toc.html"> |
| 19 | + |
| 20 | + |
| 21 | + </head> |
| 22 | + <body data-spy="scroll" data-target="#affix"> |
| 23 | + <div id="wrapper"> |
| 24 | + <header> |
| 25 | + |
| 26 | + <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation"> |
| 27 | + <div class="container"> |
| 28 | + <div class="navbar-header"> |
| 29 | + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar"> |
| 30 | + <span class="sr-only">Toggle navigation</span> |
| 31 | + <span class="icon-bar"></span> |
| 32 | + <span class="icon-bar"></span> |
| 33 | + <span class="icon-bar"></span> |
| 34 | + </button> |
| 35 | + |
| 36 | + <a class="navbar-brand" href="../index.html"> |
| 37 | + <img id="logo" class="svg" src="../logo.svg" alt=""> |
| 38 | + </a> |
| 39 | + </div> |
| 40 | + <div class="collapse navbar-collapse" id="navbar"> |
| 41 | + <form class="navbar-form navbar-right" role="search" id="search"> |
| 42 | + <div class="form-group"> |
| 43 | + <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off"> |
| 44 | + </div> |
| 45 | + </form> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + </nav> |
| 49 | + |
| 50 | + <div class="subnav navbar navbar-default"> |
| 51 | + <div class="container hide-when-search" id="breadcrumb"> |
| 52 | + <ul class="breadcrumb"> |
| 53 | + <li></li> |
| 54 | + </ul> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </header> |
| 58 | + <div role="main" class="container body-content hide-when-search"> |
| 59 | + |
| 60 | + <div class="sidenav hide-when-search"> |
| 61 | + <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a> |
| 62 | + <div class="sidetoggle collapse" id="sidetoggle"> |
| 63 | + <div id="sidetoc"></div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + <div class="article row grid-right"> |
| 67 | + <div class="col-md-10"> |
| 68 | + <article class="content wrap" id="_content" data-uid=""> |
| 69 | +<h1 id="getting-started-with-tensorflowsharp">Getting Started With TensorFlowSharp</h1> |
| 70 | + |
| 71 | +<p>TensorFlowSharp provides APIs for use in .NET programs, including C# |
| 72 | +and F#. These APIs are particularly well-suited to loading models |
| 73 | +created in Python and executing them within a .NET application. This |
| 74 | +guide explains how to install TensorFlow for .NET and use it in your application.</p> |
| 75 | +<p>TensorFlowSharp itself is a .NET API that calls into the native |
| 76 | +TensorFlow runtime. </p> |
| 77 | +<h1 id="supported-platforms">Supported Platforms</h1> |
| 78 | +<p>The NuGet package that you obtain from NuGet.org comes with the native |
| 79 | +TensorFlow runtime for Windows (x64), Mac (x64) and Linux (x64). </p> |
| 80 | +<p>If you desire to run TensorFlowSharp in other platforms, you can do so |
| 81 | +by downloading the appropriate TensorFlow dynamic library for your |
| 82 | +platform and placing this side-by-side the <code>TensorFlowSharp.dll</code> library.</p> |
| 83 | +<p>It is just not included by default as this would make the binary a lot larger.</p> |
| 84 | +<p>Additionally, up until version 1.5, TensorFlowSharp currently ships |
| 85 | +with .NET Desktop libraries that run on the .NET Desktop on Windows or |
| 86 | +on Linux and Mac using the Mono runtime.</p> |
| 87 | +<p>Support for running under .NET Core is waiting on the <a href="https://github.com/migueldeicaza/TensorFlowSharp/pull/188">migration of the |
| 88 | +package to the .NET Standard</a>.</p> |
| 89 | +<h1 id="using-tensorflowsharp-in-a-net-application">Using TensorFlowSharp in a .NET Application</h1> |
| 90 | +<p>To use TensorFlowSharp, you will need to create a .NET Desktop |
| 91 | +application on Windows or using Mono on Linux and Mac. </p> |
| 92 | +<p>To use it, make sure that you download the TensorFlowSharp package |
| 93 | +from NuGet, either using the command line (<code>nuget install |
| 94 | +TensorFlowSharp</code>) or from your favorite .NET IDE.</p> |
| 95 | +<h1 id="getting-started">Getting started</h1> |
| 96 | +<p>Your best source of information right now are the SampleTest that |
| 97 | +exercises various APIs of TensorFlowSharp, or the stand-alone samples |
| 98 | +located in "Examples".</p> |
| 99 | +<p>This API binding is closer design-wise to the Java and Go bindings |
| 100 | +which use explicit TensorFlow graphs and sessions. Your application |
| 101 | +will typically create a graph (TFGraph) and setup the operations |
| 102 | +there, then create a session from it (TFSession), then use the session |
| 103 | +runner to setup inputs and outputs and execute the pipeline.</p> |
| 104 | +<p>Something like this:</p> |
| 105 | +<pre><code class="lang-csharp">using(var graph = new TFGraph ()) |
| 106 | +{ |
| 107 | + graph.Import (File.ReadAllBytes ("MySavedModel")); |
| 108 | + var session = new TFSession (graph); |
| 109 | + var runner = session.GetRunner (); |
| 110 | + runner.AddInput (graph ["input"] [0], tensor); |
| 111 | + runner.Fetch (graph ["output"] [0]); |
| 112 | + |
| 113 | + var output = runner.Run (); |
| 114 | + |
| 115 | + // Fetch the results from output: |
| 116 | + TFTensor result = output [0]; |
| 117 | +} |
| 118 | +</code></pre><p>In scenarios where you do not need to setup the graph independently, |
| 119 | +the session will create one for you. The following example shows how |
| 120 | +to abuse TensorFlow to compute the addition of two numbers:</p> |
| 121 | +<pre><code class="lang-csharp">using (var session = new TFSession()) |
| 122 | +{ |
| 123 | + var graph = session.Graph; |
| 124 | + |
| 125 | + var a = graph.Const(2); |
| 126 | + var b = graph.Const(3); |
| 127 | + Console.WriteLine("a=2 b=3"); |
| 128 | + |
| 129 | + // Add two constants |
| 130 | + var addingResults = session.GetRunner().Run(graph.Add(a, b)); |
| 131 | + var addingResultValue = addingResults.GetValue(); |
| 132 | + Console.WriteLine("a+b={0}", addingResultValue); |
| 133 | + |
| 134 | + // Multiply two constants |
| 135 | + var multiplyResults = session.GetRunner().Run(graph.Mul(a, b)); |
| 136 | + var multiplyResultValue = multiplyResults.GetValue(); |
| 137 | + Console.WriteLine("a*b={0}", multiplyResultValue); |
| 138 | +} |
| 139 | +</code></pre><p>Here is an F# scripting version of the same example, you can use this in F# Interactive:</p> |
| 140 | +<pre><code>#r @"packages\TensorFlowSharp.1.4.0\lib\net461\TensorFlowSharp.dll" |
| 141 | + |
| 142 | +open System |
| 143 | +open System.IO |
| 144 | +open TensorFlow |
| 145 | + |
| 146 | +// set the path to find the native DLL |
| 147 | +Environment.SetEnvironmentVariable("Path", |
| 148 | + Environment.GetEnvironmentVariable("Path") + ";" + __SOURCE_DIRECTORY__ + @"/packages/TensorFlowSharp.1.2.2/native") |
| 149 | + |
| 150 | +module AddTwoNumbers = |
| 151 | + let session = new TFSession() |
| 152 | + let graph = session.Graph |
| 153 | + |
| 154 | + let a = graph.Const(new TFTensor(2)) |
| 155 | + let b = graph.Const(new TFTensor(3)) |
| 156 | + Console.WriteLine("a=2 b=3") |
| 157 | + |
| 158 | + // Add two constants |
| 159 | + let addingResults = session.GetRunner().Run(graph.Add(a, b)) |
| 160 | + let addingResultValue = addingResults.GetValue() |
| 161 | + Console.WriteLine("a+b={0}", addingResultValue) |
| 162 | + |
| 163 | + // Multiply two constants |
| 164 | + let multiplyResults = session.GetRunner().Run(graph.Mul(a, b)) |
| 165 | + let multiplyResultValue = multiplyResults.GetValue() |
| 166 | + Console.WriteLine("a*b={0}", multiplyResultValue) |
| 167 | +</code></pre></article> |
| 168 | + </div> |
| 169 | + |
| 170 | + <div class="hidden-sm col-md-2" role="complementary"> |
| 171 | + <div class="sideaffix"> |
| 172 | + <div class="contribution"> |
| 173 | + <ul class="nav"> |
| 174 | + <li> |
| 175 | + <a href="https://github.com/migueldeicaza/TensorFlowSharp/blob/master/docfx/articles/start.md/#L1" class="contribution-link">Improve this Doc</a> |
| 176 | + </li> |
| 177 | + </ul> |
| 178 | + </div> |
| 179 | + <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix"> |
| 180 | + <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> --> |
| 181 | + </nav> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + |
| 187 | + <footer> |
| 188 | + <div class="grad-bottom"></div> |
| 189 | + <div class="footer"> |
| 190 | + <div class="container"> |
| 191 | + <span class="pull-right"> |
| 192 | + <a href="#top">Back to top</a> |
| 193 | + </span> |
| 194 | + |
| 195 | + <span>Copyright © 2015-2017 Microsoft<br>Generated by <strong>DocFX</strong></span> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </footer> |
| 199 | + </div> |
| 200 | + |
| 201 | + <script type="text/javascript" src="../styles/docfx.vendor.js"></script> |
| 202 | + <script type="text/javascript" src="../styles/docfx.js"></script> |
| 203 | + <script type="text/javascript" src="../styles/main.js"></script> |
| 204 | + </body> |
| 205 | +</html> |
0 commit comments