讨论如何使用 Python 中的 Keras 函数式 API 创建层
Tensorflow 是 Google 提供的一个机器学习框架。它是一个开源框架,与 Python 结合使用以实现算法、深度学习应用程序等等。它用于研究和生产目的。它具有优化技术,有助于快速执行复杂的数学运算。
可以使用以下代码行在 Windows 上安装“tensorflow”包:
pip install tensorflow
Keras 在希腊语中意为“角”。Keras 是作为 ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分开发的。Keras 是一个用 Python 编写的深度学习 API。它是一个高级 API,具有高效的界面,有助于解决机器学习问题。它运行在 Tensorflow 框架之上。它是为了帮助快速进行实验而构建的。它提供了开发和封装机器学习解决方案所需的必要抽象和构建块。
Keras 已经存在于 Tensorflow 包中。可以使用以下代码行访问它。
import tensorflow from tensorflow import keras
Keras 函数式 API 有助于创建比使用顺序 API 创建的模型更灵活的模型。函数式 API 可以处理具有非线性拓扑的模型,可以共享层,并处理多个输入和输出。深度学习模型通常是一个包含多个层的 有向无环图 (DAG)。函数式 API 有助于构建图层图。
我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,并且无需任何配置即可免费访问 GPU(图形处理单元)。Colaboratory 是构建在 Jupyter Notebook 之上的。以下是代码片段,其中我们将看到如何使用 Keras 函数式 API 使用 Python 创建层:
示例
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
inputs = keras.Input(shape=(784,))
print("Demonstration")
img_inputs = keras.Input(shape=(32, 32, 3))
print("Dimensions of input")
print(inputs.shape)
print("The type of input")
print(inputs.dtype)
print("Layers in the model")
dense = layers.Dense(64, activation="relu")
x = dense(inputs)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(10)(x)
print("Model is being built")
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
print("More information about the model")
model.summary()代码来源 - https://tensorflowcn.cn/guide/keras/functional
输出

解释
创建一个输入节点,并将数据形状设置为 784 维向量。
返回的输入包含有关先前馈送到模型的输入数据的“形状”和“数据类型”的信息。
通过指定输入和输出创建模型。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP