跳至页脚内容
PYTHON 帮助

Keras Python(开发人员如何使用)

Keras 是一个功能强大、易于使用的 Python 库,用于开发和评估深度学习模型。 Keras 最初由 François Chollet 开发,由于其简洁且用户友好的界面而广受欢迎,使其成为机器学习领域中初学者和专家的理想选择。

此外,我们将研究 IronPDF PDF 生成库,以及如何将这两个库结合起来生成结果并导出到 PDF。

Keras 的关键特性

1. 用户友好和模块化

以 'Deep learning for humans' 为口号,Keras 被设计为易于使用、模块化和可扩展。 Keras 模型在出错时提供清晰且可操作的反馈,有助于开发者高效地调试和优化他们的模型。

2. 支持多种后台

Keras 可以在多种深度学习框架之上运行,如 TensorFlow、Theano 和微软认知工具包 (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 构建的卷积神经网络(CNN)在识别图像中的物体方面可以达到很高的精度。

2. 自然语言处理

Keras 提供工具来构建可以处理和理解人类语言的模型。 在 Keras 中,递归神经网络(RNN)和长短时记忆(LSTM)网络通常用于情感分析和机器翻译等任务。

3. 生成模型

Keras 可以用于开发生成模型,如生成对抗网络(GANs),用于生成新数据样本,这些样本类似于训练数据。

IronPDF 简介

Keras Python(其如何为开发者工作):图 2 - IronPDF 用于 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 (as np):用于数值运算。
      • matplotlib.pyplot (as plt):用于创建图表。
      • ironpdf:用于处理 PDF 的 IronPDF 库。
  2. 设置许可证密钥

    • License.LicenseKey = "your key" 设置 IronPDF 的许可证密钥。
  3. 读取数据

    • readucr 函数从特定格式(制表符分隔值)文件中读取数据。
    • 它从数据中提取标签 (y) 和特征 (x)。
  4. 加载训练和测试数据

    • 代码构建与“FordA”数据集相关的训练和测试数据文件的 URLs。
    • 使用 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。