如何使用Tensorflow和Python下载和准备CIFAR数据集?
可以使用“datasets”模块中的“load_data”方法下载CIFAR数据集。它被下载后,数据被分为训练集和验证集。
我们将使用Keras Sequential API,该API有助于构建顺序模型,用于与一整个堆栈层一起工作,其中每个层都有一个输入张量和一个输出张量。
至少包含一个层的神经网络被称为卷积层。卷积神经网络通常包括以下类型的层:
- 卷积层
- 池化层
- 密集层
卷积神经网络已被用于处理某种特定类型的问题,例如图像识别。
我们是使用Google Colaboratory来运行下面的代码。Google Colab或Colaboratory可以帮助在浏览器中运行Python代码,无需配置,并可使用免费的GPU(图形处理单元)。Colaboratory是建立在Jupyter Notebook之上的。
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
print("正在下载CIFAR数据集")
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
print("像素值被归一化到0到1之间")
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer','dog', 'frog', 'horse', 'ship', 'truck']
代码来源:https://www.tensorflow.org/tutorials/images/cnn
更多Python相关文章,请阅读:Python 教程
输出
正在下载CIFAR数据集
正在从https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz下载数据
170500096/170498071 [==============================] - 11秒 0us/step
像素值被归一化到0到1之间
说明
- CIFAR10数据集包含10个类别中的60,000张彩色图像,每个类别包含6,000张图像。
- 该数据集分为50,000个训练图像和10,000个测试图像。
- 这些类别是互斥的,并且它们之间没有重叠。
- 该数据集被下载并且数据被规范化以落在0和1之间。
极客教程