tf.keras 搭建神经网络
keras介绍
tf.keras 是 tensorflow2 引入的高封装度的框架, 可以用于快速搭建神经网络模型, keras 为支持快速实验而生,能够把想法迅速转换为结果, 是深度学习框架之中最终易上手的一个,它提供了一致而简洁的 API,能够极大地减少一般应用下的工作量,提高代码地封装程度和复用性。
搭建神经网络六步法
第一步:import 相关模块,如 import tensorflow as tf。第二步:指定输入网络的训练集和测试集,如指定训练集的输入 x_train 和标签y_train,测试集的输入 x_test 和标签 y_test。第三步:逐层搭建网络结构, model = tf.keras.models.Sequential()。第四步:在 model.compile()中配置训练方法,选择训练时使用的优化器、损失函数和最终评价指标。第五步:在 model.fit()中执行训练过程,告知训练集和测试集的输入值和标签、每个 batch 的大小(batchsize)和数据集的迭代次数(epoch)。第六步:使用 model.summary()打印网络结构,统计参数数目。
函数用法介绍
tf.keras.models.Sequential():Sequential 函数是一个容器, 描述了神经网络的网络结构,在 Sequential函数的输入参数中描述从输入层到输出层的网络结构。拉直层:tf.keras.layers.Flatten()全连接层:tf.keras.layers.Dense()卷积层:tf.keras.layers.Conv2D()LSTM 层:tf.keras.layers.LSTM()Model.compile():Compile 用于配置神经网络的训练方法,告知训练时使用的优化器、损失函数和准确率评测标准。Loss 可以是字符串形式给出的损失函数的名字,也可以是函数形式。损失函数常需要经过 softmax 等函数将输出转化为概率分布的形式。from_logits 则用来标注该损失函数是否需要转换为概率的形式, 取 False 时表示转化为概率分布,取 True 时表示没有转化为概率分布,直接输出。model.fit():fit 函数用于执行训练过程。model.summary():summary 函数用于打印网络结构和参数统计。
鸢尾花数据集代码复现
import tensorflow as tffrom sklearn import datasetsimport numpy as npx_train = datasets.load_iris().datay_train = datasets.load_iris().targetnp.random.seed(116)np.random.shuffle(x_train)np.random.seed(116)np.random.shuffle(y_train)tf.random.set_seed(116)model = tf.keras.models.Sequential([ tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())])model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)model.summary()
Epoch 499/500120/120 [==============================] - 0s 208us/sample - loss: 0.3486 - sparse_categorical_accuracy: 0.9583Epoch 500/500120/120 [==============================] - 0s 357us/sample - loss: 0.3333 - sparse_categorical_accuracy: 0.9667 - val_loss: 0.4002 - val_sparse_categorical_accuracy: 1.0000Model: "iris_model"_________________________________________________________________Layer (type) Output Shape Param # =================================================================dense_1 (Dense) multiple 15 =================================================================Total params: 15Trainable params: 15Non-trainable params: 0_________________________________________________________________使用 Sequential 实现手写数字识别。
import tensorflow as tffrom matplotlib import pyplot as pltmnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()# 可视化训练集输入特征的第一个元素plt.imshow(x_train[0], cmap='gray') # 绘制灰度图plt.show()# 打印出训练集输入特征的第一个元素print("x_train[0]:\n", x_train[0])# 打印出训练集标签的第一个元素print("y_train[0]:\n", y_train[0])# 打印出整个训练集输入特征形状print("x_train.shape:\n", x_train.shape)# 打印出整个训练集标签的形状print("y_train.shape:\n", y_train.shape)# 打印出整个测试集输入特征的形状print("x_test.shape:\n", x_test.shape)# 打印出整个测试集标签的形状print("y_test.shape:\n", y_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz11493376/11490434 [==============================] - 6s 1us/step
png
x_train[0]: [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136 175 26 166 255 247 127 0 0 0 0] [ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253 225 172 253 242 195 64 0 0 0 0] [ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251 93 82 82 56 39 0 0 0 0 0] [ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119 25 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253 150 27 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252 253 187 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249 253 249 64 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253 253 207 2 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253 250 182 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201 78 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]y_train[0]: 5x_train.shape: (60000, 28, 28)y_train.shape: (60000,)x_test.shape: (10000, 28, 28)y_test.shape: (10000,)
import tensorflow as tfmnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax')])model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)model.summary()
Train on 60000 samples, validate on 10000 samplesEpoch 5/560000/60000 [==============================] - 5s 79us/sample - loss: 0.0441 - sparse_categorical_accuracy: 0.9868 - val_loss: 0.0722 - val_sparse_categorical_accuracy: 0.9780Model: "sequential_1"_________________________________________________________________Layer (type) Output Shape Param # =================================================================flatten (Flatten) multiple 0 _________________________________________________________________dense_2 (Dense) multiple 100480 _________________________________________________________________dense_3 (Dense) multiple 1290 =================================================================Total params: 101,770Trainable params: 101,770Non-trainable params: 0_________________________________________________________________
import tensorflow as tffrom tensorflow.keras.layers import Dense, Flattenfrom tensorflow.keras import Modelmnist = tf.keras.datasets.mnist(x_train, y_train), (x_test, y_test) = mnist.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0class MnistModel(Model): def __init__(self): super(MnistModel, self).__init__() self.flatten = Flatten() self.d1 = Dense(128, activation='relu') self.d2 = Dense(10, activation='softmax') def call(self, x): x = self.flatten(x) x = self.d1(x) y = self.d2(x) return ymodel = MnistModel()model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)model.summary()
Train on 60000 samples, validate on 10000 samplesEpoch 1/560000/60000 [==============================] - 6s 102us/sample - loss: 0.2598 - sparse_categorical_accuracy: 0.9253 - val_loss: 0.1344 - val_sparse_categorical_accuracy: 0.9603Epoch 2/560000/60000 [==============================] - 5s 88us/sample - loss: 0.1121 - sparse_categorical_accuracy: 0.9664 - val_loss: 0.0942 - val_sparse_categorical_accuracy: 0.9713Epoch 3/560000/60000 [==============================] - 5s 82us/sample - loss: 0.0777 - sparse_categorical_accuracy: 0.9768 - val_loss: 0.0900 - val_sparse_categorical_accuracy: 0.9731Epoch 4/560000/60000 [==============================] - 5s 81us/sample - loss: 0.0577 - sparse_categorical_accuracy: 0.9830 - val_loss: 0.0833 - val_sparse_categorical_accuracy: 0.9739Epoch 5/560000/60000 [==============================] - 5s 80us/sample - loss: 0.0454 - sparse_categorical_accuracy: 0.9861 - val_loss: 0.0757 - val_sparse_categorical_accuracy: 0.9768Model: "mnist_model"_________________________________________________________________Layer (type) Output Shape Param # =================================================================flatten_1 (Flatten) multiple 0 _________________________________________________________________dense_4 (Dense) multiple 100480 _________________________________________________________________dense_5 (Dense) multiple 1290 =================================================================Total params: 101,770Trainable params: 101,770Non-trainable params: 0_________________________________________________________________Fashion_mnist 数据集具有 mnist 近乎所有的特征,包括 60000 张训练图片和 10000 张测试图片,图片被分为十类,每张图像为 28×28 的分辨率。由于 Fashion_mnist 数据集和 mnist 数据集具有相似的属性,所以对于 mnist只需讲 mnist 数据集的加载换成 Fashion_mnist 就可以训练 Fashion 数据集了。
import tensorflow as tffashion = tf.keras.datasets.fashion_mnist(x_train, y_train),(x_test, y_test) = fashion.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax')])model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)model.summary()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz32768/29515 [=================================] - 0s 8us/stepDownloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz26427392/26421880 [==============================] - 388s 15us/stepDownloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz8192/5148 [===============================================] - 0s 3us/stepDownloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz4423680/4422102 [==============================] - 27s 6us/stepTrain on 60000 samples, validate on 10000 samplesEpoch 1/560000/60000 [==============================] - 5s 88us/sample - loss: 0.5017 - sparse_categorical_accuracy: 0.8230 - val_loss: 0.4468 - val_sparse_categorical_accuracy: 0.8440Epoch 2/560000/60000 [==============================] - 6s 93us/sample - loss: 0.3783 - sparse_categorical_accuracy: 0.8636 - val_loss: 0.3920 - val_sparse_categorical_accuracy: 0.8611Epoch 3/560000/60000 [==============================] - 6s 92us/sample - loss: 0.3410 - sparse_categorical_accuracy: 0.8756 - val_loss: 0.3626 - val_sparse_categorical_accuracy: 0.8668Epoch 4/560000/60000 [==============================] - 6s 92us/sample - loss: 0.3141 - sparse_categorical_accuracy: 0.8850 - val_loss: 0.3616 - val_sparse_categorical_accuracy: 0.8726Epoch 5/560000/60000 [==============================] - 7s 117us/sample - loss: 0.2950 - sparse_categorical_accuracy: 0.8909 - val_loss: 0.3635 - val_sparse_categorical_accuracy: 0.8708Model: "sequential_2"_________________________________________________________________Layer (type) Output Shape Param # =================================================================flatten_2 (Flatten) multiple 0 _________________________________________________________________dense_6 (Dense) multiple 100480 _________________________________________________________________dense_7 (Dense) multiple 1290 =================================================================Total params: 101,770Trainable params: 101,770Non-trainable params: 0_________________________________________________________________
import tensorflow as tffrom tensorflow.keras.layers import Dense, Flattenfrom tensorflow.keras import Modelfashion = tf.keras.datasets.fashion_mnist(x_train, y_train),(x_test, y_test) = fashion.load_data()x_train, x_test = x_train / 255.0, x_test / 255.0class MnistModel(Model): def __init__(self): super(MnistModel, self).__init__() self.flatten = Flatten() self.d1 = Dense(128, activation='relu') self.d2 = Dense(10, activation='softmax') def call(self, x): x = self.flatten(x) x = self.d1(x) y = self.d2(x) return ymodel = MnistModel()model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False), metrics=['sparse_categorical_accuracy'])model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)model.summary()
Train on 60000 samples, validate on 10000 samplesEpoch 1/560000/60000 [==============================] - 5s 79us/sample - loss: 0.5010 - sparse_categorical_accuracy: 0.8232 - val_loss: 0.4308 - val_sparse_categorical_accuracy: 0.8483Epoch 2/560000/60000 [==============================] - 4s 68us/sample - loss: 0.3740 - sparse_categorical_accuracy: 0.8649 - val_loss: 0.3998 - val_sparse_categorical_accuracy: 0.8589Epoch 3/560000/60000 [==============================] - 4s 68us/sample - loss: 0.3395 - sparse_categorical_accuracy: 0.8752 - val_loss: 0.3714 - val_sparse_categorical_accuracy: 0.8651Epoch 4/560000/60000 [==============================] - 4s 67us/sample - loss: 0.3145 - sparse_categorical_accuracy: 0.8846 - val_loss: 0.3905 - val_sparse_categorical_accuracy: 0.8555Epoch 5/560000/60000 [==============================] - 4s 66us/sample - loss: 0.2969 - sparse_categorical_accuracy: 0.8901 - val_loss: 0.3584 - val_sparse_categorical_accuracy: 0.8702Model: "mnist_model"_________________________________________________________________Layer (type) Output Shape Param # =================================================================flatten (Flatten) multiple 0 _________________________________________________________________dense (Dense) multiple 100480 _________________________________________________________________dense_1 (Dense) multiple 1290 =================================================================Total params: 101,770Trainable params: 101,770Non-trainable params: 0_________________________________________________________________
---------------------------------
南京欧帕提亚信息科技有限公司
地址:南京市江宁开发区将军大道南佑路千人大厦
电话:15952010959(微信同)
邮箱:[email protected]
---------------------------------