本节介绍如何使用keras做深度学习训练。摘自tensorflow.keras官方教程,是手写数字识别的升级版,给定10种衣服类型进行图像识别。原网址见https://www.tensorflow.org/tutorials/keras/classification
直接上代码,配上注释。
# 0. load libs import tensorflow as tf from tensorflow import keras import numpy as np #import matplotlib.pyplot as plt # 1. load dataset and preprocess fashion_mnist = keras.datasets.fashion_mnist #Fashion MNIST是一个预定义好的训练例子,可以直接下载读取 (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() #已经区分好了60000训练10000验证 #图片是28x28像素,灰度表示,每个像素的值是0-255,标签是0-9,代表9种不同的衣服种类 #train_images.shape -> (60000, 28, 28) #len(train_labels) -> 60000 #print(train_labels) train_images = train_images / 255.0 #Scale values to a range of 0 to 1 before feeding them to the neural network model test_images = test_images / 255.0 # 2. define model model = keras.Sequential([ #使用构造函数一次性建立神经网络结构 keras.layers.Flatten(input_shape=(28, 28)), #Flatten较为特殊,没有参数,起到的作用仅仅是将28x28矩阵转成一维数组(flatten含义),即将图像输入转为合理格式 keras.layers.Dense(128, activation='relu'), keras.layers.Dense(10, activation='softmax') #softmax用于多分类过程中,将多个神经元的输出映射到(0,1)区间内,即最后结果的概率 ]) # 3. compile model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) #loss - 计算预测值与真值的多类交叉熵(输入值为二值矩阵,而不是向量) # 4. train model model.fit(train_images, train_labels, epochs=10) #The model learns to associate images and labels # 5. evaluate model test_loss, test_acc = model.evaluate(test_images, test_labels) print('\nTest accuracy:', test_acc) # 6. make predictions - single img = test_images[678] imgA = (np.expand_dims(img,0)) #model做预测都是batch,即使只有一个元素也要变成list predictions_single = model.predict(imgA) print(predictions_single) #会给出10个标签各自的概率 #print("Prediction: " + str(np.argmax(predictions_single[0]) + "Correct answer: " + str(test_labels[678]))) print(np.argmax(predictions_single[0])) print(test_labels[678])