tensorflow框架学习-第一课 Train your first neural network: basic classification

Train your first neural network: basic classification

This guide trains a neural network model to classify images of clothing, like sneakers and shirts. It's okay if you don't understand all the details, this is a fast-paced overview of a complete TensorFlow program with the details explained as we go.

This guide uses tf.keras, a high-level API to build and train models in TensorFlow.

from __future__ import absolute_import, division, print_function

# Tensorflow and tf.keras
import tensorflow as tf
from tensorflow import keras

# helper libraries
import numpy as np
import matplotlib.pyplot as plt

Import the Fashion MNIST dataset

This guide uses the Fashion MNIST dataset which contains 70,000 grayscale images in 10 categories. The images show individual articles of clothing at low resolution (28 by 28 pixels), as seen here:

Fashion MNIST is intended as a drop-in replacement for the classic MNIST dataset—often used as the "Hello, World" of machine learning programs for computer vision. The MNIST dataset contains images of handwritten digits (0, 1, 2, etc) in an identical format to the articles of clothing we'll use here.

This guide uses Fashion MNIST for variety, and because it's a slightly more challenging problem than regular MNIST. Both datasets are relatively small and are used to verify that an algorithm works as expected. They're good starting points to test and debug code.

We will use 60,000 images to train the network and 10,000 images to evaluate how accurately the network learned to classify images. You can access the Fashion MNIST directly from TensorFlow, just import and load the data:

# 引入必要的库函数
from tensorflow.python.keras.utils.data_utils import get_file
from tensorflow.python.util.tf_export import tf_export
import gzip

# 读取本地gz文档,并转换为numpy矩阵的函数
def load_localData():
    
    path = '/home/brian/Documents/tensorflow-gpu/tensorflow-learning/data/fashion/'
    
    files = [
          'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
            't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz']
    
    paths = []
    for fname in files:
        paths.append(get_file(fname, origin=None, cache_dir=path + fname, cache_subdir=path))

    with gzip.open(paths[0], 'rb') as lbpath:
        y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[1], 'rb') as imgpath:
        x_train = np.frombuffer(\
            imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

    with gzip.open(paths[2], 'rb') as lbpath:
        y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

    with gzip.open(paths[3], 'rb') as imgpath:
        x_test = np.frombuffer(\
    imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
    
    return x_train, y_train, x_test, y_test
x_train, y_train, x_test, y_test = load_localData()
# 每个分类的真实标签
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# 查看数据集的情况
print("x_train.shape: ", x_train.shape)
print("y_train.shape: ", y_train.shape)
print("x_test.shape: ", x_test.shape)
print("y_test.shape: ", y_test.shape)
print("number of train: ", x_train.shape[0])
print("number of features: ", x_train.shape[1]*x_train.shape[2])
print("number of test: ", x_test.shape[0])
print("number of classes: ", np.unique(y_train).shape[0])
x_train.shape:  (60000, 28, 28)
y_train.shape:  (60000,)
x_test.shape:  (10000, 28, 28)
y_test.shape:  (10000,)
number of train:  60000
number of features:  784
number of test:  10000
number of classes:  10
# 查看数据集中图像
show_image = x_train[0,:,:]
plt.figure()
plt.imshow(show_image)
plt.colorbar()
plt.grid(False)
plt.show()

# 数据预数据
x_train = x_train/255.
x_test = x_test/255.
# 可视化前25个训练数据图像
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(x_train[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[y_train[i]])
plt.show()

1 构建模型

配置全连接神经网络层,构建简单的神经网络

keras.layers.Flatten(input_shape=(,)) 将图的2D铺成1D向量(本例中28*28 = 784)
keras.layers.Dense(units, activation=...) Dense:全连接网络层

激活函数activation:
'relu': tf.nn.relu
'sigmoid': tf.nn.sigmoid
‘softmax’: tf.nn.softmax
'tanh': tf.nn.tanh

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(units=128, activation=tf.nn.relu),
    keras.layers.Dense(units=10, activation=tf.nn.softmax)
])

2 编译模型

编译模型需要指定优化算法、损失函数、性能指标

tf.train. 各类优化算法在此文件中找到,可以取其为字符输入
tf.losses. 损失函数可以在此文件中找到,可以取其字符输入

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

3 训练模型

训练模型需要输入训练数据、数据标签、迭代次数

model.fit(x_train, y_train, epochs=5)
Epoch 1/5
60000/60000 [==============================] - 2s 39us/sample - loss: 0.3042 - acc: 0.9031
Epoch 2/5
60000/60000 [==============================] - 2s 39us/sample - loss: 0.2626 - acc: 0.9074
Epoch 3/5
60000/60000 [==============================] - 2s 38us/sample - loss: 0.2506 - acc: 0.9103
Epoch 4/5
60000/60000 [==============================] - 2s 38us/sample - loss: 0.2387 - acc: 0.9141
Epoch 5/5
60000/60000 [==============================] - 2s 38us/sample - loss: 0.2305 - acc: 0.9156

4 评估模型

在测试数据上的准确率 Evaluate accuracy

test_loss, test_acc = model.evaluate(x=x_test, y=y_test)
print("Test accuracy: ", test_acc)
10000/10000 [==============================] - 0s 24us/sample - loss: 0.3592 - acc: 0.8770
Test accuracy:  0.877

6 可视化模型预测输出的列表情况

def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(img, cmap=plt.cm.binary) # 显示灰度图像
    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'
    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)
    
def plot_value_array(i, predictions_array, true_label):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777") # 绘制柱状图
    plt.ylim([0, 1]) 
    predicted_label = np.argmax(predictions_array)
    thisplot[predicted_label].set_color('red') # 设定预测所在的柱状为红色
    thisplot[true_label].set_color('blue') # 设定真实标签的柱状为蓝色,若预测和真实相同,则只会显示蓝色
predictions = model.predict(x_test)

i = 45
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, y_test, x_test)
plt.subplot(1,2,2)
plot_value_array(i, predictions, y_test)
plt.show()

# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, y_test, x_test)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, y_test)
plt.show()

7 利用模型预测单个样本

# 需要首先对单个样本进行维度处理为(2,height,width)
img = x_test[0]
print("pre img.shape: ", img.shape)
img = (np.expand_dims(img, 0))
print("transfer to img.shape: ", img.shape)
pre img.shape:  (28, 28)
transfer to img.shape:  (1, 28, 28)
prediction = model.predict(img)
print(prediction)
[[1.6515060e-05 1.6245197e-10 2.0251418e-08 2.3881241e-10 2.2491010e-08
  1.5393030e-04 5.3086076e-08 2.9399262e-03 6.2701432e-08 9.9688941e-01]]
plot_value_array(0, prediction, y_test)
_ = plt.xticks(range(10), class_names, rotation=45) # 添加x轴信息

np.argmax(prediction[0])

9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值