跳過到頁腳內容
PYTHON 幫助

Keras python(開發人員工作原理)

Keras 是一個強大、易用的 Python 函式庫,用於開發和評估深度學習模型。 Keras 最初由貝朗科·喬列特(François Chollet)開發,因其簡單性和用戶友好的介面而廣受歡迎,是機器學習領域新手和專家們的優良選擇。

此外,我們將探討 IronPDF PDF 生成函式庫,以及如何將這兩個函式庫結合,生成結果並將其導出為 PDF。

Keras 的主要特點

1. 用戶友好和模組化

Keras 的標語是「深度學習為人」,旨在設計為易於使用、模組化且可擴展。 Keras 模型在出錯時提供清晰且可行的反饋,有助於開發人員有效地調試和優化其模型。

2. 支援多重後端

Keras 可以運行在多種深度學習框架之上,例如 TensorFlow、Theano 和 Microsoft Cognitive Toolkit (CNTK)。 這種靈活性允許開發人員選擇最符合其需求的後端。

3. 廣泛支援神經網絡

Keras 支援多種神經網絡層,包括卷積層、循環層和全連接層。 它還支援複雜架構,如多輸入和多輸出模型、層共享和模型共享。

4. 預處理工具

Keras 包括數據預處理的工具,例如圖像和文本處理,簡化了用於訓練模型的數據集的準備工作。

5. 模型可視化和調試工具

Keras 提供工具,用於可視化神經網絡的結構和監控訓練過程。 這對於理解模型的行為並進行必要的調整至關重要。 最簡單的模型類型是順序 Keras 模型,僅僅是層的線性堆疊。

安裝

安裝 Keras 很簡單。 可以使用 pip 來安裝它:

pip install keras
pip install tensorflow
pip install keras
pip install tensorflow
SHELL

使用 Keras 構建簡單的神經網絡

以下是如何使用 Keras 構建簡單前饋神經網絡的示例:

import keras
import numpy as np
import matplotlib.pyplot as plt

# Function to read UCR formatted data
def readucr(filename):
    """
    Reads a UCR format file and returns the features and labels.

    Args:
    filename (str): Path to the data file

    Returns:
    x, y: Features and labels as numpy arrays
    """
    data = np.loadtxt(filename, delimiter="\t")
    y = data[:, 0]
    x = data[:, 1:]
    return x, y.astype(int)

# Define the root URL for the dataset
root_url = "https://raw.githubusercontent.com/hfawaz/cd-diagram/master/FordA/"
x_train, y_train = readucr(root_url + "FordA_TRAIN.tsv")
x_test, y_test = readucr(root_url + "FordA_TEST.tsv")

# Get unique classes from the dataset
classes = np.unique(np.concatenate((y_train, y_test), axis=0))

# Plot an example from each class
plt.figure()
for c in classes:
    c_x_train = x_train[y_train == c]
    plt.plot(c_x_train[0], label="class " + str(c))
plt.legend(loc="best")
plt.show()
plt.close()
import keras
import numpy as np
import matplotlib.pyplot as plt

# Function to read UCR formatted data
def readucr(filename):
    """
    Reads a UCR format file and returns the features and labels.

    Args:
    filename (str): Path to the data file

    Returns:
    x, y: Features and labels as numpy arrays
    """
    data = np.loadtxt(filename, delimiter="\t")
    y = data[:, 0]
    x = data[:, 1:]
    return x, y.astype(int)

# Define the root URL for the dataset
root_url = "https://raw.githubusercontent.com/hfawaz/cd-diagram/master/FordA/"
x_train, y_train = readucr(root_url + "FordA_TRAIN.tsv")
x_test, y_test = readucr(root_url + "FordA_TEST.tsv")

# Get unique classes from the dataset
classes = np.unique(np.concatenate((y_train, y_test), axis=0))

# Plot an example from each class
plt.figure()
for c in classes:
    c_x_train = x_train[y_train == c]
    plt.plot(c_x_train[0], label="class " + str(c))
plt.legend(loc="best")
plt.show()
plt.close()
PYTHON

輸出

Keras Python(它的作用於開發者): 圖 1 - 輸出的神經網絡模型

實際應用

1. 圖像分類

Keras 廣泛用於圖像分類任務。 例如,用 Keras 構建的卷積神經網絡(CNNs)可以在圖像物體識別中達到高準確度。

2. 自然語言處理

Keras 提供工具,用於構建可以處理和理解人類語言的模型。 在 Keras 中,循環神經網絡(RNNs)和長短期記憶(LSTM)網絡通常用於情感分析和機器翻譯等任務。

3. 生成模型

Keras 可以用來開發生成模型,例如生成對抗網絡(GANs),這些模型用於生成類似訓練數據的新數據樣本。

介紹 IronPDF

Keras Python(它的作用於開發者): 圖 2 - IronPDF for Python 網頁

IronPDF 是由 Iron Software 開發和維護的強大 Python 函式庫。 它允許開發人員在 Python 專案中創建、編輯和提取 PDF 內容。 以下是 IronPDF 的一些主要特點:

  1. PDF 生成

    • 您可以從多種來源生成 PDF,包括 HTML、URL、JavaScript、CSS 和圖像格式。

    • 可以向生成的 PDF 添加頁眉、頁腳、簽名、附件和安全功能。
  2. 性能優化

    • IronPDF 支援完整的多執行緒和異步操作。
  3. 跨平台相容性

    • 它在 Windows、macOS、Linux、Docker、Azure 和 AWS 上與 Python 3.7+ 一起工作。

要開始使用,請使用 pip 安裝 IronPDF:

pip install ironpdf
pip install ironpdf
SHELL

安裝後,您可以使用 HTML 內容或 URL 創建 PDF。 以下是一些範例:

  1. HTML 到 PDF
from ironpdf import ChromePdfRenderer

# Create a PDF renderer
renderer = ChromePdfRenderer()

# Render HTML content as a PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
pdf.SaveAs("ironAwesome.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer
renderer = ChromePdfRenderer()

# Render HTML content as a PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
pdf.SaveAs("ironAwesome.pdf")
PYTHON
  1. URL 到 PDF
from ironpdf import ChromePdfRenderer

# Create a PDF renderer
renderer = ChromePdfRenderer()

# Render a URL as a PDF
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")
pdf.SaveAs("ironAwesome.pdf")
from ironpdf import ChromePdfRenderer

# Create a PDF renderer
renderer = ChromePdfRenderer()

# Render a URL as a PDF
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")
pdf.SaveAs("ironAwesome.pdf")
PYTHON

IronPDF 與 Keras Python:生成模型的 PDF

安裝

pip install ironpdf
pip install keras
pip install tensorflow
pip install ironpdf
pip install keras
pip install tensorflow
SHELL

現在生成模型圖並使用以下代碼將其導出為 PDF:

import keras
import numpy as np
import matplotlib.pyplot as plt
from ironpdf import License, ImageToPdfConverter

# Apply your license key
License.LicenseKey = "your key goes here"

def readucr(filename):
    """Read and parse UCR formatted data."""
    data = np.loadtxt(filename, delimiter="\t")
    y = data[:, 0]
    x = data[:, 1:]
    return x, y.astype(int)

# Define the root URL for the dataset
root_url = "https://raw.githubusercontent.com/hfawaz/cd-diagram/master/FordA/"
x_train, y_train = readucr(root_url + "FordA_TRAIN.tsv")
x_test, y_test = readucr(root_url + "FordA_TEST.tsv")

# Extract unique classes from the dataset
classes = np.unique(np.concatenate((y_train, y_test), axis=0))

# Plot example data for each class
plt.figure()
for c in classes:
    c_x_train = x_train[y_train == c]
    plt.plot(c_x_train[0], label="class " + str(c))
plt.legend(loc="best")
plt.savefig('data.png') 
plt.show()
plt.close()

# Convert the saved image to a PDF
ImageToPdfConverter.ImageToPdf("data.png").SaveAs("plot.pdf")
import keras
import numpy as np
import matplotlib.pyplot as plt
from ironpdf import License, ImageToPdfConverter

# Apply your license key
License.LicenseKey = "your key goes here"

def readucr(filename):
    """Read and parse UCR formatted data."""
    data = np.loadtxt(filename, delimiter="\t")
    y = data[:, 0]
    x = data[:, 1:]
    return x, y.astype(int)

# Define the root URL for the dataset
root_url = "https://raw.githubusercontent.com/hfawaz/cd-diagram/master/FordA/"
x_train, y_train = readucr(root_url + "FordA_TRAIN.tsv")
x_test, y_test = readucr(root_url + "FordA_TEST.tsv")

# Extract unique classes from the dataset
classes = np.unique(np.concatenate((y_train, y_test), axis=0))

# Plot example data for each class
plt.figure()
for c in classes:
    c_x_train = x_train[y_train == c]
    plt.plot(c_x_train[0], label="class " + str(c))
plt.legend(loc="best")
plt.savefig('data.png') 
plt.show()
plt.close()

# Convert the saved image to a PDF
ImageToPdfConverter.ImageToPdf("data.png").SaveAs("plot.pdf")
PYTHON

代碼說明

  1. 導入函式庫

    • 代碼首先導入必要的函式庫:
      • Keras:流行的深度學習函式庫。
      • numpy(縮寫 np):用於數值運算。
      • matplotlib.pyplot(縮寫 plt):用於創建圖表。
      • ironpdf:用於處理 PDF 的 IronPDF 函式庫。
  2. 設置授權密鑰

    • License.LicenseKey = "your key" 此行設置 IronPDF 的授權密鑰。
  3. 讀取數據

    • readucr 函數用特定格式(制表符分隔值)從文件中讀取數據。
    • 它提取數據中的標籤(y)和特徵(x)。
  4. 載入訓練和測試數據

    • 代碼構建與 "FordA" 數據集相關的訓練和測試數據文件的 URL。
    • 使用readucr函數載入數據。
  5. 繪製數據

    • 代碼識別數據集中的唯一類別。
    • 對於每個類別,選擇第一個實例(c_x_train[0])並繪製之。
    • 圖例顯示類別標籤。
  6. 保存圖表

    • 圖表作為名為 "data.png" 的圖像文件保存。
  7. 將圖像轉換為 PDF

    • IronPDF 的 ImageToPdfConverter 將保存的圖像("data.png")轉換為 PDF 文件("plot.pdf")。

輸出ted PDF

Keras Python(它的作用於開發者): 圖 3 - 從前面的代碼導出的 PDF

IronPDF 授權

Keras Python(它的作用於開發者): 圖 4 - IronPDF for Python 授權頁面

IronPDF 需要授權才能運行,如上述代碼所示。 在腳本開頭設置授權密鑰如下:

# Apply your license key
License.LicenseKey = "your key goes here"
# Apply your license key
License.LicenseKey = "your key goes here"
PYTHON

如果您對 IronPDF 函式庫的試用授權感興趣,可以從此處獲得試用授權密鑰。

結論

Keras 人工智能 Python 函式庫因其簡單性和靈活性在深度學習社區中脫穎而出。 它抽象化了構建神經網絡的複雜性,使開發人員能專注於設計和實驗模型。 無論您是剛開始深度學習的初學者,還是經驗豐富的從業者,Keras 都提供了將想法付諸實現所需的工具,其模仿人類大腦的行為。

另一方面,IronPDF 是一個多功能的 PDF 生成和操作函式庫,使將結果導出為 PDF 變得容易。 擁有這兩項技能將幫助用戶撰寫現代數據科學模型,並將輸出導出為 PDF 以便結果文件化。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。