多层感知机(Multilayer Perceptron,MLP)构建步骤
使用tf.keras.datasets获得数据集并预处理
使用tf.keras.Model和tf.keras.layers构建模型
构建模型训练流程,使用tf.keras.losses计算损失函数,并使用tf.keras.optimizer优化模型
构建模型评估流程,使用tf.keras.metrics计算评估指标
多层感知机MNIST手写体数字图片案例
import tensorflow as tf
import numpy as np
class MNISTLoader():
def __init__(self):
mnist = tf.keras.datasets.mnist
(self.train_data,self.train_label),(self.test_data,self.test_label) = mnist.load_data()
self.train_data = np.expand_dims(self.train_data.astype(np.float32)/255.0,axis=-1)
self.train_data = np.expand_dims(self.test_data.astype(np.float32)/255.0,axis=-1)
self.train_label = self.train_label.astype(np.int32)
self.test_label = self.test_label.astype(np.int32)
self.num_train_data,self.num_test_data = self.train_data.shape[0],self.test_data.shape[0]
def get_batch(self,batch_size):
index = np.random.randint(0,np.shape(self.train_data)[0],batch_size)
return self.train_data[index,:],self.train_label[index]
class MLP(tf.keras.Model):
def __init__(self):
super().__init__()
self.flatten = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(units=100,activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(units=10)
def call(self,inputs):
x = self.flatten(inputs)
x = self.dense1(x)
x = self.dense2(x)
output = tf.nn.softmax(x)
return output
num_epochs = 5
batch_size = 50
learning_rate = 0.001
model = MLP()
data_loader = MNISTLoader()
optimizer = tf.keras.optimizers.Adam(learning_rate = learning_rate)
num_batches = int(data_loader.num_train_data // batch_size*num_epochs)
for batch_index in range(num_batches):
X, y = data_loader.get_batch(batch_size)
with tf.GradientTape() as tape:
y_pred = model(X)
loss = tf.keras.losses.sparse_categorical_crossentropy(y_true=y,y_pred=y_pred)
loss = tf.reduce_mean(loss)
print("batch %d: loss %f" % (batch_index,loss.numpy()))
grads = tape.gradient(loss,model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads,model.variables))
输出:
batch 0: loss 2.384565
batch 1: loss 2.364758
batch 2: loss 2.437717
batch 3: loss 2.328029
batch 4: loss 2.320627
........
batch 999: loss 2.160970
softmax函数:为了使得模型的输出能始终满足这两个条件,
该向量中的每个元素均在[0,1]之间
该向量的所有元素之和为1