TensorFlow Keras 是 TensorFlow 库的高级 API,它提供了构建和训练神经网络所需的所有工具。本指南将帮助您开始使用 Keras 进行实践。
快速开始
安装 TensorFlow
- 确保您的系统中已安装 TensorFlow。您可以从 TensorFlow 官方网站 获取安装指南。
导入必要的库
import tensorflow as tf from tensorflow.keras import layers, models
创建一个简单的模型
model = models.Sequential() model.add(layers.Dense(64, activation='relu', input_shape=(784,))) model.add(layers.Dense(10, activation='softmax'))
编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
训练模型
model.fit(x_train, y_train, epochs=5)
数据预处理
在训练模型之前,您需要准备和预处理数据。
加载数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
数据标准化
x_train, x_test = x_train / 255.0, x_test / 255.0
扩展阅读
图片示例
Neural Network
Dense Layer