{"id":13430872,"url":"https://github.com/migueldeicaza/TensorFlowSharp","last_synced_at":"2025-03-16T06:31:38.060Z","repository":{"id":38837829,"uuid":"79082378","full_name":"migueldeicaza/TensorFlowSharp","owner":"migueldeicaza","description":"TensorFlow API for .NET languages","archived":false,"fork":false,"pushed_at":"2024-01-12T14:41:05.000Z","size":8583,"stargazers_count":3139,"open_issues_count":196,"forks_count":578,"subscribers_count":231,"default_branch":"main","last_synced_at":"2024-10-11T01:21:09.222Z","etag":null,"topics":["c-sharp","dot-net","f-sharp","machine-learning","mono","tensorflow","xamarin"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/migueldeicaza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-16T04:18:28.000Z","updated_at":"2024-10-07T22:58:13.000Z","dependencies_parsed_at":"2024-06-20T23:28:07.476Z","dependency_job_id":"29932803-4255-4eb8-9a9d-3af4964256dc","html_url":"https://github.com/migueldeicaza/TensorFlowSharp","commit_stats":{"total_commits":269,"total_committers":30,"mean_commits":8.966666666666667,"dds":0.4386617100371747,"last_synced_commit":"600d278ded9e7e723309cff27f69cd4597c2a077"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/migueldeicaza%2FTensorFlowSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/migueldeicaza%2FTensorFlowSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/migueldeicaza%2FTensorFlowSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/migueldeicaza%2FTensorFlowSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/migueldeicaza","download_url":"https://codeload.github.com/migueldeicaza/TensorFlowSharp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221656458,"owners_count":16858774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","dot-net","f-sharp","machine-learning","mono","tensorflow","xamarin"],"created_at":"2024-07-31T02:00:58.606Z","updated_at":"2024-10-27T09:31:01.963Z","avatar_url":"https://github.com/migueldeicaza.png","language":"C#","readme":"[![Build Status](https://travis-ci.org/migueldeicaza/TensorFlowSharp.svg?branch=master)](https://travis-ci.org/migueldeicaza/TensorFlowSharp)\n[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/TensorFlowSharp)\n\n# When to use TensorFlowSharp\n\nTensorFlowSharp is a good runtime to run your existing models, and is mostly\na straight binding to the underlying TensorFlow runtime.   Most people will\nwant to use a higher-level library for interfacing with TensorFlow.\n\nThe library was designed to blend in the .NET ecosystem and use the\n.NET naming conventions.\n\nI strongly recommend that you use\n[TensorFlow.NET](https://github.com/SciSharp/TensorFlow.NET) which\ntakes a different approach than TensorFlowSharp, it uses the Python\nnaming convention and has a much broader support for the higher level\noperations that you are likely to need - and is also actively maintained.\n\n\n# TensorFlowSharp\n\nTensorFlowSharp are .NET bindings to the TensorFlow library published here:\n\nhttps://github.com/tensorflow/tensorflow\n\nThis surfaces the C API as a strongly-typed .NET API for use from C# and F#.\n\nThe API surfaces the entire low-level TensorFlow API, it is on par with other\nlanguage bindings.  But currently does not include a high-level API like\nthe Python binding does, so it is more cumbersome to use for those high level\noperations.\n\nYou can prototype using TensorFlow or Keras in Python, then save your graphs\nor trained models and then load the result in .NET with TensorFlowSharp and\nfeed your own data to train or run.\n\nThe [current API\ndocumentation](https://migueldeicaza.github.io/TensorFlowSharp/) is here.\n\n# Using TensorFlowSharp\n\n## Installation \n\nThe easiest way to get started is to use the NuGet package for \nTensorFlowSharp which contains both the .NET API as well as the \nnative libraries for 64-bit Linux, Mac and Windows using the CPU backend.\n\nYou can install using NuGet like this:\n\n```cmd\nnuget install TensorFlowSharp\n```\n\nOr select it from the NuGet packages UI on Visual Studio.\n\nOn Visual Studio, make sure that you are targeting .NET 4.6.1 or\nlater, as this package uses some features of newer .NETs.  Otherwise,\nthe package will not be added. Once you do this, you can just use the\nTensorFlowSharp nuget\n\nAlternatively, you can [download it](https://www.nuget.org/packages/TensorFlowSharp/) directly.\n\n## Using TensorFlowSharp\n\nYour best source of information right now are the SampleTest that\nexercises various APIs of TensorFlowSharp, or the stand-alone samples\nlocated in \"Examples\".\n\nThis API binding is closer design-wise to the Java and Go bindings\nwhich use explicit TensorFlow graphs and sessions.  Your application\nwill typically create a graph (TFGraph) and setup the operations\nthere, then create a session from it (TFSession), then use the session\nrunner to setup inputs and outputs and execute the pipeline.\n\nSomething like this:\n\n```csharp\nusing (var graph = new TFGraph ())\n{\n    // Load the model\n    graph.Import (File.ReadAllBytes (\"MySavedModel\"));\n    using (var session = new TFSession (graph))\n    {\n        // Setup the runner\n        var runner = session.GetRunner ();\n        runner.AddInput (graph [\"input\"] [0], tensor);\n        runner.Fetch (graph [\"output\"] [0]);\n\n        // Run the model\n        var output = runner.Run ();\n\n        // Fetch the results from output:\n        TFTensor result = output [0];\n    }\n}\n```\n\nIf your application is sensitive to GC cycles, you can run your model as follows.\nThe `Run` method will then allocate managed memory only at the first call and reuse it later on.\nNote that this requires you to reuse the `Runner` instance and not to change the shape of the input data:\n\n```csharp\n// Some input matrices\nvar inputs = new float[][,] {\n    new float[,] { { 1, 2 }, { 3, 4 } },\n    new float[,] { { 2, 4 }, { 6, 8 } }\n};\n\n// Assumes all input matrices have identical shape\nvar shape = new long[] { inputs[0].GetLongLength(0), inputs[0].GetLongLength(1) };\nvar size = inputs[0].Length * sizeof(float);\n\n// Empty input and output tensors\nvar input = new TFTensor(TFDataType.Float, shape, size);\nvar output = new TFTensor[1];\n\n// Result array for a single run\nvar result = new float[1, 1];\n\nusing (var graph = new TFGraph())\n{\n    // Load the model\n    graph.Import(File.ReadAllBytes(\"MySavedModel\"));\n    using (var session = new TFSession(graph))\n    {\n        // Setup the runner\n        var runner = session.GetRunner();\n        runner.AddInput(graph[\"input\"][0], input);\n        runner.Fetch(graph[\"output\"][0]);\n\n        // Run the model on each input matrix\n        for (int i = 0; i \u003c inputs.Length; i++)\n        {\n            // Mutate the input tensor\n            input.SetValue(inputs[i]);\n\n            // Run the model\n            runner.Run(output);\n\n            // Fetch the result from output into `result`\n            output[0].GetValue(result);\n        }\n    }\n}\n```\n\nIn scenarios where you do not need to setup the graph independently,\nthe session will create one for you.  The following example shows how\nto abuse TensorFlow to compute the addition of two numbers:\n\n```csharp\nusing (var session = new TFSession())\n{\n    var graph = session.Graph;\n\n    var a = graph.Const(2);\n    var b = graph.Const(3);\n    Console.WriteLine(\"a=2 b=3\");\n\n    // Add two constants\n    var addingResults = session.GetRunner().Run(graph.Add(a, b));\n    var addingResultValue = addingResults.GetValue();\n    Console.WriteLine(\"a+b={0}\", addingResultValue);\n\n    // Multiply two constants\n    var multiplyResults = session.GetRunner().Run(graph.Mul(a, b));\n    var multiplyResultValue = multiplyResults.GetValue();\n    Console.WriteLine(\"a*b={0}\", multiplyResultValue);\n}\n```\n\nHere is an F# scripting version of the same example, you can use this in F# Interactive:\n\n```fsharp\n#r @\"packages\\TensorFlowSharp.1.4.0\\lib\\net471\\TensorFlowSharp.dll\"\n\nopen System\nopen System.IO\nopen TensorFlow\n\n// set the path to find the native DLL\nEnvironment.SetEnvironmentVariable(\"Path\", \n    Environment.GetEnvironmentVariable(\"Path\") + \";\" + __SOURCE_DIRECTORY__ + @\"/packages/TensorFlowSharp.1.2.2/native\")\n\nmodule AddTwoNumbers = \n    let session = new TFSession()\n    let graph = session.Graph\n\n    let a = graph.Const(new TFTensor(2))\n    let b = graph.Const(new TFTensor(3))\n    Console.WriteLine(\"a=2 b=3\")\n\n    // Add two constants\n    let addingResults = session.GetRunner().Run(graph.Add(a, b))\n    let addingResultValue = addingResults.GetValue()\n    Console.WriteLine(\"a+b={0}\", addingResultValue)\n\n    // Multiply two constants\n    let multiplyResults = session.GetRunner().Run(graph.Mul(a, b))\n    let multiplyResultValue = multiplyResults.GetValue()\n    Console.WriteLine(\"a*b={0}\", multiplyResultValue)\n```\n\n# Working on TensorFlowSharp \n\nIf you want to work on extending TensorFlowSharp or contribute to its development\nread the [CONTRIBUTING.md](CONTRIBUTING.md) file.\n\nPlease keep in mind that this requires a modern version of C# as this uses some\nnew capabilities there.   So you will want to use Visual Studio 2017.\n\n## Possible Contributions\n\n### Build More Tests\n\nWould love to have more tests to ensure the proper operation of the framework.\n\n### Samples\n\nThe binding is pretty much complete, and at this point, I want to improve the \nAPI to be easier and more pleasant to use from both C# and F#.   Creating\nsamples that use Tensorflow is a good way of finding easy wins on the usability\nof the API, there are some here:\n\nhttps://github.com/tensorflow/models\n\n### Packaging\n\nMobile: we need to package the library for consumption on Android and iOS.\n\n### Documentation Styling\n\nThe API documentation has not been styled, I am using the barebones template\nfor documentation, and it can use some work.\n\n### Issues\n\nI have logged some usability problems and bugs in Issues, feel free to take\non one of those tasks.\n\n## Documentation\n\nMuch of the online documentation comes from TensorFlow and is licensed under\nthe terms of Apache 2 License, in particular all the generated documentation\nfor the various operations that is generated by using the tensorflow reflection\nAPIs.\n\nLast API update: Release 1.9\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","C#","C\\#","C# #","xamarin","Machine Learning and Data Science","🗒️ Cheatsheets"],"sub_categories":["Machine Learning and Data Science","机器学习和科学研究","📦 Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmigueldeicaza%2FTensorFlowSharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmigueldeicaza%2FTensorFlowSharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmigueldeicaza%2FTensorFlowSharp/lists"}