下面是一个使用TensorFlow实现CIFAR-10图像识别的示例代码:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
# 加载CIFAR-10数据集,会自动下载(如果之前没下载过)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# 查看训练集信息
print("训练集图像形状:", x_train.shape)
print("训练集标签形状:", y_train.shape)
# 查看测试集信息
print("测试集图像形状:", x_test.shape)
print("测试集标签形状:", y_test.shape)
plt.mishow(x_train[6])
y_train
label_dict = {0:"airplane", 1:"automobile", 2:"bird", 3:"cat", 4:"deer",
5:"dog", 6:"frog", 7:"horse", 8:"ship", 9:"truck"}
label_dict[y_train[6][0]]
x_train[0][0][0]
x_train = x_train.astype("float32")/255.0
x_test = x_test.astype("float32")/255.0
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(filters=32,
kernel_size