TensorFlow .NET(對於開發者的運行原理)
機器學習 (ML) 通過啟用智能決策和自動化,從醫療保健到金融等各種行業帶來了革命性的變革。 TensorFlow,Google 的開源 ML 和深度學習框架,一直處於這場革命的前沿。 使用 TensorFlow.NET,.NET 開發者可以在 C# 生態系統中利用 TensorFlow 的強大功能。 在本文中,我們將探索 TensorFlow.NET,其功能、優勢以及在 C# 開發中的實際應用。 另外,我們將透過一個實際範例了解 IronPDF 來自 Iron Software 名為的 PDF 生成程式庫。
了解 TensorFlow.NET
TensorFlow.NET 是 TensorFlow 的 .NET 綁定,允許開發者在 C# 和 .NET 應用層中直接使用 TensorFlow 的功能。 由社群開發並由 SciSharp 組織維護,TensorFlow.NET 提供了 TensorFlow 機器學習和神經網路的能力與 .NET 平台多功能性的無縫整合。 它使 C# 開發者能夠使用 TensorFlow 的廣泛系統 API 和工具來構建神經網路、訓練模型和部署 ML 模型。
TensorFlow.NET 的關鍵功能
- TensorFlow 相容性:
TensorFlow.NET提供了對 TensorFlow API 和操作的完全相容,包括張量操作、神經網路層、損失函式、優化器和資料預處理及評估的工具。 - 高效能:
TensorFlow.NET利用 TensorFlow 的高效計算圖執行引擎和優化內核,在 CPU 和 GPU 上提供高效能的機器學習推理和訓練。 - 簡易整合:
TensorFlow.NET可與現有的 .NET 應用和程式庫無縫整合,使開發者能夠在不離開熟悉的 C# 開發環境的情況下利用 TensorFlow 的功能。 - 模型可移植性:
TensorFlow.NET允許開發者導入預訓練的 TensorFlow 模型,並將訓練模型導出以便在其他基於 TensorFlow 的環境中進行推理,例如 Python 或移動裝置。 - 靈活性和可擴展性:
TensorFlow.NET提供了使用 C# 語言特性自訂和擴展機器學習模型的靈活性,例如使用 LINQ(語言整合查詢)進行資料操作和使用函式式編程範式進行模型組合。 - 社群支援和文件:
TensorFlow.NET受益於活躍的貢獻者社群,他們提供文件、教程和範例,幫助開發者在 C# 世界中使用 TensorFlow 開始機器學習。
使用 TensorFlow.NET 的實際範例
讓我們探索一些可用於使用 TensorFlow.NET 在 C# 中構建和部署機器學習模型的實際場景:
-
載入和使用預訓練模型:
// 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);// 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);' Load a pre-trained TensorFlow model Dim model = TensorFlowModel.LoadModel("model.pb") ' Perform inference on input data Dim input = New Single(, ) { { 1.0F, 2.0F, 3.0F } } Dim output = model.Predict(input)$vbLabelText $csharpLabel -
訓練自訂模型:
// 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);// 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);' Create a neural network model using TensorFlow.NET APIs Dim input As New Input(Shape.Scalar) Dim output = (New Dense(1, Activation.ReLU)).Forward(input) ' Compile the model with loss function and optimizer algorithms Dim model As 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)$vbLabelText $csharpLabel -
評估和部署:
// 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");// 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");' Evaluate the trained model on test data Dim evaluation = model.Evaluate(x_test, y_test) ' Export the trained model for deployment model.SaveModel("trained_model.pb")$vbLabelText $csharpLabel
更多 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);
}
}
}
' Use static TensorFlow
Imports Tensorflow.Binding
Namespace IronPdfDemos
Public Class TensorFlow
Public Shared Sub Execute()
' Create a TensorFlow constant
Dim hello = tf.constant("Hello, TensorFlow!")
Console.WriteLine(hello)
End Sub
End Class
End Namespace
範例 Hello TensorFlow 輸出

使用 TensorFlow.NET 的好處
- 無縫整合:
TensorFlow.NET將 TensorFlow 的功能帶入 .NET 生態系統,使 C# 開發者能夠在他們的應用中利用最先進的機器學習技術和算法。 - 效能和可擴展性:
TensorFlow.NET利用 TensorFlow 的優化執行引擎提供高效能的機器學習計算,適合處理大規模資料集和複雜的模型。 - 熟悉的開發環境:
TensorFlow.NETAPI 允許開發者使用熟悉的 C# 語言特性和開發工具構建和部署機器學習模型,降低在 C# 應用中採用機器學習的學習曲線。 - 互操作性和可移植性:
TensorFlow.NET促進與其他基於 TensorFlow 的環境的互操作性,使 C# 基於的機器學習模型與 Python、TensorFlow 伺服器和 TensorFlow Lite 的無縫整合。 - 社群驅動的開發:
TensorFlow.NET從活躍的貢獻者和使用者社群中受益,他們為專案提供支援、反饋和貢獻,確保其不斷成長和改進。
TensorFlow.NET 授權
這是一個開放源碼的 Apache 授權套件,可以免費使用。 可以在 TensorFlow.NET 授權 頁面閱讀更多有關授權的資訊。
介紹IronPDF

IronPDF 是一個強大的 C# PDF 程式庫,允許開發者直接從 HTML、CSS、圖像和 JavaScript 輸入建立、編輯和簽署 PDF 文件。 這是一個商業級解決方案,具有高效能和低記憶體佔用。 這裡有一些關鍵特點:
- HTML 到 PDF 轉換:
IronPDF可以將 HTML 文件、HTML 字串和網址轉換為 PDF。 例如,您可以使用 Chrome PDF 渲染器渲染網頁為 PDF。 - 跨平台支持:
IronPDF適用於各個 .NET 平台,包括 .NET Core、.NET Standard 和 .NET Framework。 它與 Windows、Linux 和 macOS 相容。 - 編輯和簽署: 您可以設置屬性,新增安全性(密碼和許可權),甚至為您的 PDF 應用數位簽名。
- 頁面模板和設置: 通過新增頭部、尾部和頁碼,以及調整邊距來自訂您的 PDF。
IronPDF還支持響應式佈局和自訂紙張尺寸。 - 標準合規:
IronPDF遵循 PDF 標準,例如 PDF/A 和 PDF/UA。 它支持 UTF-8 字元編碼並處理圖像、CSS 和字體等資產。
使用 TensorFlow.NET 和 IronPDF 生成 PDF 文件
首先,建立一個 Visual Studio 專案,並選擇下面的控制台應用模板。

提供專案名稱和位置。

在下一步中選擇所需的 .NET 版本並點擊"建立"按鈕。
從 Visual Studio 封裝管理器中安裝 IronPDF 從 NuGet 套件。

安裝套件 TensorFlow.NET 和 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");
}
}
}
Imports IronPdf
Imports Tensorflow.Binding
Namespace IronPdfDemos
Public Class Program
Public Shared Sub Main()
' Instantiate Cache and ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Prepare HTML content for the PDF
Dim 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>"
Dim a = tf.constant(5)
Dim b = tf.constant(6)
Dim c = tf.constant(7)
' Perform various tensor operations
content &= "<h2>Various Tensor Operations</h2>"
Dim add = tf.add(a, b)
Dim [sub] = tf.subtract(a, b)
Dim mul = tf.multiply(a, b)
Dim 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
Dim mean = tf.reduce_mean(tf.constant( { a, b, c }))
Dim sum = tf.reduce_sum(tf.constant( { a, b, c }))
content &= "<h2>Additional Operations</h2>"
content &= $"<p>mean = {mean.numpy()}</p>"
content &= $"<p>sum = {sum.numpy()}</p>"
' Perform matrix multiplication
Dim matrix1 = tf.constant(New Single(, ) {
{ 1, 2 },
{ 3, 4 }
})
Dim matrix2 = tf.constant(New Single(, ) {
{ 5, 6 },
{ 7, 8 }
})
Dim 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
Dim pdf = renderer.RenderHtmlAsPdf(content)
' Save PDF to file
pdf.SaveAs("tensorflow.pdf")
End Sub
End Class
End Namespace
程式碼解釋
讓我們分解這段程式碼片段:
-
導入聲明:
程式碼以導入必要的程式庫開始。 具體來說:
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.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.Imports IronPdf ' This imports the IronPDF package, which is used for working with PDF files. Imports Tensorflow.Binding ' This imports the TensorFlow library, specifically the .NET standard binding.$vbLabelText $csharpLabel -
急切執行:
這行
tf.enable_eager_execution();啟用了 TensorFlow 的急切執行模式。 在急切執行中,操作會立即被評估,使其更易於除錯和與張量互動。 -
定義張量常數:
程式碼定義了三個張量常數:
a,b和c。 這些分別初始化為值 5、6 和 7。 -
各種張量操作:
執行了以下張量操作:
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.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.Dim add = tf.add(a, b) ' Adds a and b. Dim [sub] = tf.subtract(a, b) ' Subtracts b from a. Dim mul = tf.multiply(a, b) ' Multiplies a and b. Dim div = tf.divide(a, b) ' Divides a by b.$vbLabelText $csharpLabel -
存取張量值:
張量操作的結果包含在 HTML 內容中:
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>";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>";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>"$vbLabelText $csharpLabel -
附加操作:
程式碼計算了常數
[a, b, c]的平均值和總和。 -
矩陣乘法:
它在
matrix1和matrix2之間執行矩陣乘法並顯示結果。 -
PDF 生成:
使用
ChromePdfRenderer和RenderHtmlAsPdf來自IronPDF將 HTML 字串渲染為 PDF 文件。
輸出

IronPDF 授權
IronPDF 需要授權才能運行。 更多關於授權的資訊可以在 IronPDF 授權 頁面找到。 將金鑰放置在如下所示的 appSettings.json 文件中。
{
"IronPdf.License.LicenseKey": "The Key Here"
}
結論
總之,TensorFlow.NET 使 C# 開發者能夠探索機器學習和人工智慧的世界,並利用 .NET 生態系統的多樣性和生產力。 無論您是在構建智能應用、預測分析工具,還是自動化決策系統,TensorFlow.NET 提供了一個強大而靈活的框架,讓 C# 中的機器學習潛力得以釋放。 結合來自 Iron Software 的 IronPDF 程式庫,開發者能夠獲得開發現代應用的高級技能。
常見問題
我如何將機器學習整合到我的 C# 應用程式中?
您可以使用 TensorFlow.NET,它是 TensorFlow 的 .NET 綁定,允許您在 C# 應用程式中構建、訓練和部署機器學習模型。它提供與 TensorFlow 強大 API 的完全相容性。
TensorFlow.NET 的關鍵特點是什麼?
TensorFlow.NET 提供的特點包括與 TensorFlow APIs 的完全相容性、通過 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 操作和管理。
using TensorFlow.NET 的開發者可獲得哪些支持?
TensorFlow.NET 由強大的社區和完整的文件支持,使開發者更易於找到資源和範例來幫助他們的開發過程。
TensorFlow.NET 如何增強 C# 開發環境?
TensorFlow.NET 通過整合 TensorFlow 的機器學習能力來增強 C# 開發環境,允許開發者在不離開 .NET 生態系統的情況下充分利用 TensorFlow 的強大功能。
開發者在哪裡可以找到使用 IronPDF 的實務範例?
開發者可以在 IronPDF 文件頁面以及專注於 .NET PDF 操作的各種線上資源和社區論壇中找到使用 IronPDF 的實務範例。




