Tensotflow API:tf.keras搭建网络八股
六步法:
import
train,test
model=tf.keras.models.Sequential
model.compile
model.fit
model.summary
Sequential()
model=tf.keras.cmodel.Sequential([网络结构]) #描述各层网络
网络结构举例:
拉直层:tf.keras.layers.Flatten() 这一层不含计算,只是形状转换,把输入特征拉直变成一维数组
全连接层:tf.keras.layers.Dense(神经元个数,activation="激活函数",kernel_regularizer=哪种正则化)
activation(字符串给出)可选:relu、softmax、sigmoid、tanh
kernel_regularizer可选:tf.keras.regularizers.l1()、tf.keras.regularizer.l2()
卷积层:tf.keras.layers.Conv2D(filters=卷积核个数,kernel_size=卷积核尺寸,strides=卷积步长,padding="vaild"or"same)
LSTM层:tf.keras.layers.LSTM()
model.compile()
model.compile(optimizer=优化器
loss=损失函数
metrics=["准确率"])
optimizer可选:
'sgd' or tf.keras.optimizers.SGD(lr=学习率,momentum=动量参选)
'adagrad' or tf.keras.optimizers.Adagrad(lr=学习率)
'adadelta' or tf.keras.optimizers.Adadelta(lr=学习率)
'adam' or tf.keras.optimizers.Adam(lr=学习率,beta_1=0.9,beta_2=0.99)
loss可选:
'mse' or tf.keras.losses.MeanSquaredError()
'sparse_categorical_crossentropy' or tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
#如果神经网络在预测结果在输出前经过了概率分布,这里的结果是False
#神经网络预测结果输出枝前没有经过概率分布,直接输出了,这里是True
Metrics可选:
'accuracy':y_和y都是数值,如y_=[1] y=[1]
'categorical_accuracy':y_和y都是独热码(概率分布),如y_=[0,1,0],y=[0.256,0.695,0.048]
'sparse_categorical_accuracy':y_是数值,y是独热码(概率分布),如y_=[1] y=0.256,0.695,0.048]
model.fit()
model.fit(训练的输入特征,训练集的标签,
batch_size= ,epochs= ,
validation_data(测试集的输入特征,测试集的标签),
validation_split=从训练集划分多少比例给测试集,与validation_data二者选择其一,
validation_freq=多少次epoch测试一次)
model.summary()
summary()可以打印出网络的结构和参数统计。
#import
import tensorflow as tf
from sklearn import datasets
import numpy as np
#train test
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
#model.Sequential
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
#model.compile
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
#由于神经网络末端使用了softmax函数,使得输出是概率分布而不是原始输出,所以from_logits=False
metrics=['sparse_categorical_accuracy'])
#由于鸢尾花数据集给的标签是0、1、2是数值,而神经网络的前向传播的输出是概率分布,所以这里选择sparse_categorical_accuracy作为评测指标
#model.fit
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
#从训练集中选择20%的数据作为测试集 每二十次输出一次准确率
#model.summary
model.summary()
.
.
.
Epoch 490/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3438 - sparse_categorical_accuracy: 0.9583
Epoch 491/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3397 - sparse_categorical_accuracy: 0.9667
Epoch 492/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3352 - sparse_categorical_accuracy: 0.9833
Epoch 493/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3451 - sparse_categorical_accuracy: 0.9333
Epoch 494/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3441 - sparse_categorical_accuracy: 0.9333
Epoch 495/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3362 - sparse_categorical_accuracy: 0.9583
Epoch 496/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3450 - sparse_categorical_accuracy: 0.9583
Epoch 497/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3436 - sparse_categorical_accuracy: 0.9333
Epoch 498/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3412 - sparse_categorical_accuracy: 0.9667
Epoch 499/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3652 - sparse_categorical_accuracy: 0.9333
Epoch 500/500
4/4 [==============================] - 0s 6ms/step - loss: 0.3896 - sparse_categorical_accuracy: 0.9250 - val_loss: 0.3515 - val_sparse_categorical_accuracy: 0.8667
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 3) 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
Sequential可以搭建出上层输出就是下层输入的顺序网络结构,但是无法写出一些有跳连的非顺序网络结构,这个时候我们可以选择用类class搭建神经网络结构。
六步法:
import
train,test
class MyModel(Model) model=MyModel
model.compile
model.fit
model.summary
class MyModel(Model):
def __init__(self):
super(MyModel,self).__init__()
定义网络结构块
def call(self,x):
调用网络模块,实现前向传播
return y
__init__() 定义所需网络结构块
call() 写出前向传播
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as np
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
class IrisModel(Model):
def __init__(self):
super(IrisModel, self).__init__()
self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
model = IrisModel()
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 490/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3438 - sparse_categorical_accuracy: 0.9583
Epoch 491/500
4/4 [==============================] - 0s 997us/step - loss: 0.3397 - sparse_categorical_accuracy: 0.9667
Epoch 492/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3352 - sparse_categorical_accuracy: 0.9833
Epoch 493/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3451 - sparse_categorical_accuracy: 0.9333
Epoch 494/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3441 - sparse_categorical_accuracy: 0.9333
Epoch 495/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3362 - sparse_categorical_accuracy: 0.9583
Epoch 496/500
4/4 [==============================] - 0s 2ms/step - loss: 0.3450 - sparse_categorical_accuracy: 0.9583
Epoch 497/500
4/4 [==============================] - 0s 2ms/step - loss: 0.3436 - sparse_categorical_accuracy: 0.9333
Epoch 498/500
4/4 [==============================] - 0s 2ms/step - loss: 0.3412 - sparse_categorical_accuracy: 0.9667
Epoch 499/500
4/4 [==============================] - 0s 1ms/step - loss: 0.3652 - sparse_categorical_accuracy: 0.9333
Epoch 500/500
4/4 [==============================] - 0s 7ms/step - loss: 0.3896 - sparse_categorical_accuracy: 0.9250 - val_loss: 0.3515 - val_sparse_categorical_accuracy: 0.8667
Model: "iris_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) multiple 15
=================================================================
Total params: 15
Trainable params: 15
Non-trainable params: 0
_________________________________________________________________
28*28
像素点的0~9手写数字图片和标签,用于训练。28*28
像素点的0~9手写数字图片和标签,用于测试。导入MNIST数据集:
mnist=tf.kreas.datasets.mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
作为输入特征,输入神经网络时,将数据拉伸为一维数组:
tf.keras.layers.Flatten()
[0 0 0 48 238 252 252 ... ... ...253 186 12 0 0 0]
import tensorflow as tf
from matplotlib import pyplot as plt
mnist = 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)
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]:
5
x_train.shape:
(60000, 28, 28)
y_train.shape:
(60000,)
x_test.shape:
(10000, 28, 28)
y_test.shape:
(10000,)
import tensorflow as tf
mnist = 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.0
model = 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()
Epoch 1/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.2633 - sparse_categorical_accuracy: 0.9238 - val_loss: 0.1477 - val_sparse_categorical_accuracy: 0.9556
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1178 - sparse_categorical_accuracy: 0.9648 - val_loss: 0.1059 - val_sparse_categorical_accuracy: 0.9678
Epoch 3/5
1875/1875 [==============================] - 3s 1ms/step - loss: 0.0826 - sparse_categorical_accuracy: 0.9753 - val_loss: 0.0934 - val_sparse_categorical_accuracy: 0.9710
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0608 - sparse_categorical_accuracy: 0.9812 - val_loss: 0.0789 - val_sparse_categorical_accuracy: 0.9754
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0470 - sparse_categorical_accuracy: 0.9850 - val_loss: 0.0773 - val_sparse_categorical_accuracy: 0.9770
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 128) 100480
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model
mnist = 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.0
class 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 y
model = 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()
Epoch 1/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2624 - sparse_categorical_accuracy: 0.9248 - val_loss: 0.1369 - val_sparse_categorical_accuracy: 0.9607
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.1136 - sparse_categorical_accuracy: 0.9664 - val_loss: 0.0986 - val_sparse_categorical_accuracy: 0.9699
Epoch 3/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0776 - sparse_categorical_accuracy: 0.9765 - val_loss: 0.0945 - val_sparse_categorical_accuracy: 0.9716
Epoch 4/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0583 - sparse_categorical_accuracy: 0.9820 - val_loss: 0.0842 - val_sparse_categorical_accuracy: 0.9761
Epoch 5/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.0453 - sparse_categorical_accuracy: 0.9864 - val_loss: 0.1040 - val_sparse_categorical_accuracy: 0.9687
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) multiple 0
_________________________________________________________________
dense (Dense) multiple 100480
_________________________________________________________________
dense_1 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
28*28
像素点的衣裤图片和标签,用于训练。28*28
像素点的衣裤图片和标签,用于测试。导入FASHION数据集
fashion=tf.kreas.datasets.fashion_mnist
(x_train,y_train),(x_test,y_test)=fashion.load_data()
import tensorflow as tf
fashion = 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.0
model = 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()
Epoch 1/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.4988 - sparse_categorical_accuracy: 0.8245 - val_loss: 0.4296 - val_sparse_categorical_accuracy: 0.8468
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3750 - sparse_categorical_accuracy: 0.8648 - val_loss: 0.3852 - val_sparse_categorical_accuracy: 0.8624
Epoch 3/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3382 - sparse_categorical_accuracy: 0.8766 - val_loss: 0.3602 - val_sparse_categorical_accuracy: 0.8714
Epoch 4/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3162 - sparse_categorical_accuracy: 0.8832 - val_loss: 0.3692 - val_sparse_categorical_accuracy: 0.8694
Epoch 5/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2967 - sparse_categorical_accuracy: 0.8908 - val_loss: 0.3504 - val_sparse_categorical_accuracy: 0.8742
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 128) 100480
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model
fashion = 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.0
class 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 y
model = 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()
Epoch 1/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.5003 - sparse_categorical_accuracy: 0.8252 - val_loss: 0.4209 - val_sparse_categorical_accuracy: 0.8525
Epoch 2/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3796 - sparse_categorical_accuracy: 0.8623 - val_loss: 0.3864 - val_sparse_categorical_accuracy: 0.8578
Epoch 3/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3390 - sparse_categorical_accuracy: 0.8763 - val_loss: 0.3685 - val_sparse_categorical_accuracy: 0.8665
Epoch 4/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.3156 - sparse_categorical_accuracy: 0.8849 - val_loss: 0.3552 - val_sparse_categorical_accuracy: 0.8713
Epoch 5/5
1875/1875 [==============================] - 2s 1ms/step - loss: 0.2972 - sparse_categorical_accuracy: 0.8897 - val_loss: 0.3530 - val_sparse_categorical_accuracy: 0.8711
Model: "mnist_model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) multiple 0
_________________________________________________________________
dense (Dense) multiple 100480
_________________________________________________________________
dense_1 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0benling
_________________________________________________________________