跳至頁尾內容
.NET 幫助

TensorFlow .NET(開發者使用指南)

機器學習(ML)透過實現智慧決策和自動化,徹底改變了從醫療保健到金融等各個產業。 谷歌的開源機器學習和深度學習框架 TensorFlow 一直是這場變革的前沿。 借助TensorFlow.NET ,.NET 開發人員可以在 C# 生態系統中利用 TensorFlow 的強大功能。 在本文中,我們將探討TensorFlow.NET ,它的特性、優勢以及在 C# 開發中的實際應用。 此外,我們還將透過一個實際例子來學習Iron Software的名為IronPDF的PDF生成庫。

了解 TensorFlow.NET

TensorFlow.NET是 TensorFlow 的 .NET 綁定,可讓開發人員直接在 C# 和 .NET 應用程式層中使用 TensorFlow 功能。 TensorFlow.NET由社群開發,由 SciSharp 組織維護,它將 TensorFlow 的機器學習和神經網路功能與 .NET 平台的多功能性無縫整合。 它使 C# 開發人員能夠使用 TensorFlow 的廣泛系統 API 和工具建立神經網路、訓練模型和部署 ML 模型。

TensorFlow.NET 的主要特性

  1. TensorFlow 相容性: TensorFlow.NET完全相容於 TensorFlow 的 API 和操作,包括張量操作、神經網路層、損失函數、最佳化器以及用於資料預處理和評估的實用程式。 2.高效能: TensorFlow.NET利用 TensorFlow 高效的運算圖執行引擎和最佳化的內核,在 CPU 和 GPU 上提供高效能的機器學習推理和訓練。 3.輕鬆集成: TensorFlow.NET可與現有的 .NET 應用程式和程式庫無縫集成,使開發人員能夠在不離開熟悉的 C# 開發環境的情況下利用 TensorFlow 的功能。 4.模型可移植性: TensorFlow.NET允許開發人員匯入預先訓練的 TensorFlow 模型,並將訓練好的模型匯出到其他基於 TensorFlow 的環境(例如 Python 或行動裝置)中進行推理。 5.靈活性和可擴展性: TensorFlow.NET提供了靈活性,可以使用 C# 語言特性(例如用於資料操作的 LINQ(語言整合查詢)和用於模型組合的函數式程式設計範式)來客製化和擴展機器學習模型。 6.社區支援和文件: TensorFlow.NET受益於活躍的貢獻者社區,他們提供文件、教程和範例,幫助開發人員使用 TensorFlow 在 C# 世界中開始機器學習。

TensorFlow.NET 的實際範例

讓我們來探討一些可以使用TensorFlow.NET在 C# 中建構和部署機器學習模型的實際場景:

1.載入和使用預訓練模型:

```csharp
// Load a pre-trained TensorFlow model
var model = TensorFlowModel.LoadModel("model.pb");
// Perform inference on input data
var input = new float[,] { { 1.0f, 2.0f, 3.0f } };
var output = model.Predict(input);
```

2.訓練自訂模型:

```csharp
// Create a neural network model using TensorFlow.NET APIs
var input = new Input(Shape.Scalar);
var output = new Dense(1, Activation.ReLU).Forward(input);
// Compile the model with loss function and optimizer algorithms
var model = new Model(input, output);
model.Compile(optimizer: new SGD(), loss: Losses.MeanSquaredError);
// Train the model with training data
model.Fit(x_train, y_train, epochs: 10, batchSize: 32);
```

3.評估與部署:

```csharp
// Evaluate the trained model on test data
var evaluation = model.Evaluate(x_test, y_test);
// Export the trained model for deployment
model.SaveModel("trained_model.pb");
```

更多 TensorFlow 範例請參閱TensorFlow.NET 範例頁面。

// Use static TensorFlow
using static Tensorflow.Binding;

namespace IronPdfDemos
{
    public class TensorFlow
    {
        public static void Execute()
        {
            // Create a TensorFlow constant
            var hello = tf.constant("Hello, TensorFlow!");
            Console.WriteLine(hello);
        }
    }
}
// Use static TensorFlow
using static Tensorflow.Binding;

namespace IronPdfDemos
{
    public class TensorFlow
    {
        public static void Execute()
        {
            // Create a TensorFlow constant
            var hello = tf.constant("Hello, TensorFlow!");
            Console.WriteLine(hello);
        }
    }
}
$vbLabelText   $csharpLabel

TensorFlow Hello 範例輸出

TensorFlow .NET(開發者使用指南):圖 1 - 控制台應用程式輸出

使用 TensorFlow.NET 的好處

1.無縫整合: TensorFlow.NET將 TensorFlow 的強大功能引入 .NET 生態系統,使 C# 開發人員能夠在他們的應用程式中利用最先進的機器學習技術和演算法。 2.效能和可擴充性: TensorFlow.NET利用 TensorFlow 的最佳化執行引擎提供高效能的機器學習運算,使其適合處理大規模資料集和複雜模型。 3.熟悉的開發環境: TensorFlow.NET API 允許開發人員使用熟悉的 C# 語言特性和開發工具來建立和部署機器學習模型,從而降低在 C# 應用程式中採用機器學習的學習曲線。 4.互通性和可移植性: TensorFlow.NET促進了與其他基於 TensorFlow 的環境的互通性,從而實現了基於 C# 的機器學習模型與 Python、TensorFlow Serving 和 TensorFlow Lite 的無縫整合。 5.社區驅動開發: TensorFlow.NET受惠於活躍的貢獻者和使用者社區,他們為專案提供支援、回饋和貢獻,確保專案的持續成長和改進。

TensorFlow.NET 許可證

這是一個開源的 Apache 許可軟體包,可以免費使用。 有關許可的更多信息,請參閱TensorFlow.NET 許可頁面。

IronPDF簡介

TensorFlow .NET(開發者使用指南):圖 2 - IronPDF

IronPDF是一個功能強大的 C# PDF 庫,可讓開發人員直接從 HTML、CSS、圖像和 JavaScript 輸入建立、編輯和簽署 PDF。 它是一款商用級解決方案,具有高效能和低記憶體佔用等優點。 以下是一些主要特點:

  1. HTML 轉 PDF 轉換: IronPDF可以將 HTML 檔案、HTML 字串和 URL 轉換為 PDF。 例如,您可以使用 Chrome PDF 渲染器將網頁渲染為 PDF。 2.跨平台支援: IronPDF可在各種 .NET 平台上運行,包括 .NET Core、.NET Standard 和 .NET Framework。 它相容於Windows、Linux和macOS系統。 3.編輯和簽名:您可以設定屬性、新增安全措施(密碼和權限),甚至可以對 PDF 應用數位簽章。 4.頁面範本和設定:透過新增頁首、頁尾和頁碼以及調整頁邊距來自訂您的 PDF。 IronPDF也支援響應式佈局和自訂紙張尺寸。 5.標準合規性: IronPDF遵循 PDF/A 和 PDF/UA 等 PDF 標準。 它支援 UTF-8 字元編碼,並可處理圖像、CSS 和字體等資源。

使用 TensorFlow.NET 和 IronPDF 產生 PDF 文檔

首先,建立一個 Visual Studio 項目,然後選擇下面的控制台應用程式範本。

TensorFlow .NET(開發者使用指南):圖 3 - Visual Studio 項目

請提供項目名稱和地點。

TensorFlow .NET(開發者使用指南):圖 4 - 專案配置

在下一步中選擇所需的 .NET 版本,然後按一下"建立"按鈕。

從 Visual Studio 套件管理員中的 NuGet 套件安裝IronPDF

TensorFlow .NET(開發者使用指南):圖 5 - TensorFlow.NET

安裝TensorFlow.NETTensorFlow.Keras軟體包,TensorFlow.Keras 是一個用於運行模型的獨立軟體包。

TensorFlow .NET(開發者使用指南):圖 6 - 安裝 TensorFlow.Keras 套件

using IronPdf;
using static Tensorflow.Binding;

namespace IronPdfDemos
{
    public class Program
    {
        public static void Main()
        {
            // Instantiate Cache and ChromePdfRenderer
            var renderer = new ChromePdfRenderer();

            // Prepare HTML content for the PDF
            var content = "<h1>Demonstrate TensorFlow with IronPDF</h1>";
            content += "<h2>Enable Eager Execution</h2>";
            content += "<p>tf.enable_eager_execution();</p>";

            // Enable eager execution mode in TensorFlow
            tf.enable_eager_execution();

            // Define tensor constants
            content += "<h2>Define Tensor Constants</h2>";
            var a = tf.constant(5);
            var b = tf.constant(6);
            var c = tf.constant(7);

            // Perform various tensor operations
            content += "<h2>Various Tensor Operations</h2>";
            var add = tf.add(a, b);
            var sub = tf.subtract(a, b);
            var mul = tf.multiply(a, b);
            var div = tf.divide(a, b);

            content += $"<p>var add = tf.add(a, b);</p>";
            content += $"<p>var sub = tf.subtract(a, b);</p>";
            content += $"<p>var mul = tf.multiply(a, b);</p>";
            content += $"<p>var div = tf.divide(a, b);</p>";

            // Output tensor values to HTML content
            content += "<h2>Access Tensor Values</h2>";
            content += $"<p>{a.numpy()} + {b.numpy()} = {add.numpy()}</p>";
            content += $"<p>{a.numpy()} - {b.numpy()} = {sub.numpy()}</p>";
            content += $"<p>{a.numpy()} * {b.numpy()} = {mul.numpy()}</p>";
            content += $"<p>{a.numpy()} / {b.numpy()} = {div.numpy()}</p>";

            // Perform additional operations
            var mean = tf.reduce_mean(tf.constant(new[] { a, b, c }));
            var sum = tf.reduce_sum(tf.constant(new[] { a, b, c }));

            content += "<h2>Additional Operations</h2>";
            content += $"<p>mean = {mean.numpy()}</p>";
            content += $"<p>sum = {sum.numpy()}</p>";

            // Perform matrix multiplication
            var matrix1 = tf.constant(new float[,] { { 1, 2 }, { 3, 4 } });
            var matrix2 = tf.constant(new float[,] { { 5, 6 }, { 7, 8 } });
            var product = tf.matmul(matrix1, matrix2);

            content += "<h2>Matrix Multiplications</h2>";
            content += "<p>Multiplication Result:</p>";
            content += $"<p>product = {product.numpy()}</p>";

            // Render HTML content to PDF
            var pdf = renderer.RenderHtmlAsPdf(content);
            // Save PDF to file
            pdf.SaveAs("tensorflow.pdf");
        }
    }
}
using IronPdf;
using static Tensorflow.Binding;

namespace IronPdfDemos
{
    public class Program
    {
        public static void Main()
        {
            // Instantiate Cache and ChromePdfRenderer
            var renderer = new ChromePdfRenderer();

            // Prepare HTML content for the PDF
            var content = "<h1>Demonstrate TensorFlow with IronPDF</h1>";
            content += "<h2>Enable Eager Execution</h2>";
            content += "<p>tf.enable_eager_execution();</p>";

            // Enable eager execution mode in TensorFlow
            tf.enable_eager_execution();

            // Define tensor constants
            content += "<h2>Define Tensor Constants</h2>";
            var a = tf.constant(5);
            var b = tf.constant(6);
            var c = tf.constant(7);

            // Perform various tensor operations
            content += "<h2>Various Tensor Operations</h2>";
            var add = tf.add(a, b);
            var sub = tf.subtract(a, b);
            var mul = tf.multiply(a, b);
            var div = tf.divide(a, b);

            content += $"<p>var add = tf.add(a, b);</p>";
            content += $"<p>var sub = tf.subtract(a, b);</p>";
            content += $"<p>var mul = tf.multiply(a, b);</p>";
            content += $"<p>var div = tf.divide(a, b);</p>";

            // Output tensor values to HTML content
            content += "<h2>Access Tensor Values</h2>";
            content += $"<p>{a.numpy()} + {b.numpy()} = {add.numpy()}</p>";
            content += $"<p>{a.numpy()} - {b.numpy()} = {sub.numpy()}</p>";
            content += $"<p>{a.numpy()} * {b.numpy()} = {mul.numpy()}</p>";
            content += $"<p>{a.numpy()} / {b.numpy()} = {div.numpy()}</p>";

            // Perform additional operations
            var mean = tf.reduce_mean(tf.constant(new[] { a, b, c }));
            var sum = tf.reduce_sum(tf.constant(new[] { a, b, c }));

            content += "<h2>Additional Operations</h2>";
            content += $"<p>mean = {mean.numpy()}</p>";
            content += $"<p>sum = {sum.numpy()}</p>";

            // Perform matrix multiplication
            var matrix1 = tf.constant(new float[,] { { 1, 2 }, { 3, 4 } });
            var matrix2 = tf.constant(new float[,] { { 5, 6 }, { 7, 8 } });
            var product = tf.matmul(matrix1, matrix2);

            content += "<h2>Matrix Multiplications</h2>";
            content += "<p>Multiplication Result:</p>";
            content += $"<p>product = {product.numpy()}</p>";

            // Render HTML content to PDF
            var pdf = renderer.RenderHtmlAsPdf(content);
            // Save PDF to file
            pdf.SaveAs("tensorflow.pdf");
        }
    }
}
$vbLabelText   $csharpLabel

程式碼解釋

讓我們來分析一下這段程式碼:

1.進口聲明:

程式碼首先導入必要的庫。 具體來說:

```csharp
using IronPdf; // This imports the IronPDF package, which is used for working with PDF files.
using static Tensorflow.Binding; // This imports the TensorFlow library, specifically the .NET standard binding.
```

2.急於執行:

`tf.enable_eager_execution();`這行程式碼啟用了 TensorFlow 的 eager execution 模式。 在立即執行模式下,操作會立即進行評估,更容易調試和與張量互動。

3.定義張量常數:

此程式碼定義了三個張量常數: `a` 、 `b`和`c` 。 這些值分別初始化為 5、6 和 7。

4.各種張量運算:

執行以下張量運算:

```csharp
var add = tf.add(a, b); // Adds a and b.
var sub = tf.subtract(a, b); // Subtracts b from a.
var mul = tf.multiply(a, b); // Multiplies a and b.
var div = tf.divide(a, b); // Divides a by b.
```

5.訪問張量值:

張量運算的結果包含在 HTML 內容中:

```csharp
content += $"<p>{a.numpy()} + {b.numpy()} = {add.numpy()}</p>";
content += $"<p>{a.numpy()} - {b.numpy()} = {sub.numpy()}</p>";
content += $"<p>{a.numpy()} * {b.numpy()} = {mul.numpy()}</p>";
content += $"<p>{a.numpy()} / {b.numpy()} = {div.numpy()}</p>";
```

6.其他操作:

程式碼計算常數`[a, b, c]`的平均值和總和。

7.矩陣乘法:

它執行`matrix1`和`matrix2`之間的矩陣乘法,並顯示結果。
  1. PDF 生成:

    IronPDF中的ChromePdfRendererRenderHtmlAsPdf用於將 HTML 字串渲染成 PDF 文件。

輸出

TensorFlow .NET(開發者使用指南):圖 7 - PDF 輸出

IronPDF 許可

IronPDF需要許可證才能運作。 有關許可的更多信息,請訪問IronPDF 許可頁面。 將金鑰放入appSettings.json檔案中,如下所示。

{
  "IronPdf.License.LicenseKey": "The Key Here"
}

結論

總而言之, TensorFlow.NET使 C# 開發人員能夠利用 .NET 生態系統的多功能性和生產力來探索機器學習和人工智慧的世界。 無論您是在建立智慧應用程式、預測分析工具或自動化決策系統, TensorFlow.NET都提供了一個強大且靈活的框架,可在 C# 中釋放機器學習的潛力。 結合Iron SoftwareIronPDF庫,開發人員可以獲得開發現代應用程式的高級技能。

常見問題解答

如何將機器學習整合到我的 C# 應用程式中?

您可以使用 TensorFlow.NET,它是 TensorFlow 的 .NET 綁定,可讓您在 C# 應用程式中建置、訓練和部署機器學習模型。它與 TensorFlow 強大的 API 完全相容。

TensorFlow.NET 的主要特性有哪些?

TensorFlow.NET 提供以下功能:與 TensorFlow API 完全相容、透過 TensorFlow 的運算引擎實現高效能、輕鬆與 .NET 系統整合、模型可移植性以及強大的社群支援。

如何在.NET應用程式中將HTML轉換為PDF?

您可以使用 IronPDF 在 .NET 應用程式中將 HTML 轉換為 PDF。 IronPDF 支援將 HTML、CSS 和 JavaScript 輸入轉換為 PDF 文檔,提供跨平台相容性和高級 PDF 操作功能。

TensorFlow.NET 能否用於從 Python 匯入模型?

是的,TensorFlow.NET 支援模型可移植性,可讓您匯入在 Python 等環境中建立的模型,並在您的 .NET 應用程式中使用它們。

TensorFlow.NET 和 IronPDF 結合使用有哪些潛力?

將 TensorFlow.NET 和 IronPDF 結合起來,開發人員可以建立智慧應用程序,執行複雜的機器學習計算,並將結果以格式良好的 PDF 文件形式呈現,從而增強文件和報告流程。

TensorFlow.NET 適合跨平台開發嗎?

是的,TensorFlow.NET 可以在跨平台的 .NET 環境中使用,使開發人員能夠建立與各種作業系統相容的應用程式。

如何在C#應用程式中編輯和簽署PDF文件?

IronPDF 提供在 C# 應用程式中編輯和簽署 PDF 文件的功能,從而實現強大的 PDF 操作和管理。

使用 TensorFlow.NET 的開發者可以獲得哪些支援?

TensorFlow.NET 擁有強大的社群支援和全面的文檔,讓開發人員更容易找到資源和範例來幫助他們進行開發。

TensorFlow.NET 如何增強 C# 開發環境?

TensorFlow.NET 透過整合 TensorFlow 的機器學習功能來增強 C# 開發環境,使開發人員能夠在不離開 .NET 生態系統的情況下充分利用 TensorFlow 的強大功能。

開發者可以在哪裡找到使用 IronPDF 的實際案例?

開發人員可以在 IronPDF 文件頁面以及各種線上資源和專門用於 .NET PDF 操作的社群論壇上找到使用 IronPDF 的實際範例。

Jacob Mellor,Team Iron 首席技術官
首席技術長

Jacob Mellor 是 Iron Software 的首席技術官,也是一位富有遠見的工程師,率先開發了 C# PDF 技術。作為 Iron Software 核心程式碼庫的最初開發者,他自公司成立之初便參與塑造了其產品架構,並與執行長 Cameron Rimington 一起將其發展成為一家擁有 50 多名員工、服務於 NASA、特斯拉和全球政府機構的公司。

Jacob 於 1998 年至 2001 年在曼徹斯特大學獲得土木工程一級榮譽學士學位。 1999 年,他在倫敦創辦了自己的第一家軟體公司;2005 年,他創建了自己的第一個 .NET 元件。此後,他專注於解決微軟生態系統中的複雜問題。

他的旗艦產品 IronPDF 和 IronSuite .NET 庫在全球 NuGet 上的安裝量已超過 3000 萬次,其基礎程式碼持續為全球開發者工具提供支援。憑藉 25 年的商業經驗和 41 年的程式設計專長,Jacob 始終致力於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代技術領導者。