Tensorflow学习笔记1----基础分类模型

神经网络模型简介

神经网络基础简介,复习这篇笔记,模型可以总结如下:基础的神经网络,可以视为以层为单位,前一层的输出是下一层的输入(这类似Linux的管道),每一层的输出需要经过一个激活函数。

代码简介

神经网络部分

Tensorflow神经网络训练模型的基本步骤,总结如下:
导入数据 => 构建神经网络模型 => 配置模型的训练方式 => 输入数据到网络 => 训练模型 => 评估模型 => 使用模型进行预测

学习的时候,要时刻记住自己所处的阶段;同时要明白,对初学者或者AI基础应用工程来说,核心时搭建正确的神经网络和输入正确的数据。

以下内容从官网学习:https://www.tensorflow.org/tutorials/keras/basic_classification

导入数据

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

这是个固定的模式,直接导入即可。之后构建自己的数据标签:

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

通过print函数,可以知道训练数据有60000组,每个图片是28*28的二维数据;测试数据有10000组

print(train_images.shape)  # (60000, 28, 28)
print(test_images.shape)  # (10000, 28, 28)

构建全连接神经网络模型
我们第一个模型是全连接的,以层为单位建模,使用tensorflowkeras高级API作为工具。

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

tf.keras.Sequential是一个最基础的方式,它把每一层从前到后进行自动连接。我们模型有3层,第一层是输入数据,第二层是使用relu的全连接层,第三层是使用softmax的结果层。
tf.keras.layers.Flatten()是把高维数据展开成一维的
tf.keras.layers.Dense()表示一个全连接层

这个model高度封装了从训练到测试的所有有用的数据。

配置模型的训练方式

model.compile(optimizer='adam',
			  loss='sparse_categorical_crossentropy',
			  metrics=['accuracy'])
  • optimizer:反向梯度计算的算法
  • loss:损失函数
  • metrics:用于监视寻来你和测试步骤,这里使用了精度的方式

训练模型
tf.keras的API高度封装了整个流程,因此只需要指定训练数据和训练的轮数即可。

model.fit(trains_images, train_labels, epochs=5)

评估模型
训练完成后,需要在测试集上评估模型准确率,使用

test_loss, test_acc = model.evaluate(test_images, test_labels)

返回损失值和精度,表示平均损失值和预测的准确率

使用训练好的模型进行预测

predictions = model.predict(test_image)

通过输入一组预测数据,得到一组预测值,注意predictions是个二维的。

predictions[0] = array([1.6707758e-06, 8.3274145e-08, 9.8423456e-08, 1.9251273e-07,
       1.4543222e-06, 2.4620399e-02, 8.9157339e-07, 4.9053874e-02,
       6.1236402e-05, 9.2625999e-01], dtype=float32)

通过np.argmax(predictions[0])可以索引出最大的下标。

第一部分的代码

import tensorflow as tf
import numpy as np

fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# 参数标准化到0-1
train_images = train_images / 255.0
test_images = test_images / 255.0

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

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

model.fit(train_images, train_labels, epochs=5)

test_err, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

predictions = model.predict(test_images)

print(np.argmax(predictions[0]))

绘图部分

掌握基本的绘图工具,应该是一项基本的技能,否则无法描述更复杂的网络,先学习使用最基本的matplotlib.pyplot这个最常用的库。

创建一个新的图片
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

一般都是用默认的即可,具体参考文档。

显示照片
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow

matplotlib.pyplot.imshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, *, data=None, **kwargs)

具体参照文档即可

绘制子图片

subplot(nrows, ncols, index, **kwargs)

nrows行,ncols列,第index个位置

显示图片

show()

调用该函数,才会有图片显示出来。

完整代码

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

print(tf.__version__)

fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

train_images = train_images / 255.0
test_images = test_images / 255.0

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

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

model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

predictions = model.predict(test_images)


def plot_image(i, prediction_array, true_label, imag):
    prediction_array, true_label, imag = prediction_array[i], true_label[i], imag[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(imag, cmap=plt.cm.binary)

    predicted_label = np.argmax(prediction_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                         100 * np.max(prediction_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')


num_rows = 5
num_cols = 2
num_images = num_rows * num_cols
plt.figure(figsize=(2 * 2 * num_cols, 2 * num_cols))
for i in range(num_images):
    plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)
    plot_image(i, predictions, test_labels, test_images)
    plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)
    plot_value_array(i, predictions, test_labels)

plt.show()

img = test_images[0]
print(img.shape)
img = (np.expand_dims(img, 0))
print(img.shape)

predictions_single = model.predict(img)
print(predictions_single)

plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

print(np.argmax(predictions_single[0]))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值