关键点:
STEP 1:读取数据
'''读取数据'''
from tensorflow.keras.datasets import mnist
from __future__ import division
import tensorflow as tf
import numpy as np
data = mnist.load_data()
(x_train, y_train), (x_test,y_test) = data# x_train.shape=(60000, 28, 28)
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
# reshape中值等于-1的话,那么Numpy会根据剩下的维度计算出数组的另外一个shape属性值。
x_train, x_test = x_train.reshape([-1,28*28]),x_test.reshape([-1,28*28]) # x_train.shape=(60000, 784)
STEP 2:特征缩放
该步骤可以让数据处于同一数值量级,也可以加快算法的收敛速度
from sklearn import preprocessing
x_train = preprocessing.scale(x_train)
STEP 3:初始化参数
import numpy as np
w = tf.Variable(tf.cast(np.random.randn(784,10)*0.001,tf.float32),name="weight")
b = tf.Variable(tf.cast(np.random.randn(10),tf.float32),name="bias")
learning_rate = 0.1
step = 2000
batch_size = 256
STEP 4:数据配对并洗牌
该步骤打乱数据,这样可以保证每批次训练的时候所用到的数据集是不一样的,可以提高模型训练效果
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
STEP 5:定义逻辑方程
def logistic_f(x):
z = x@w+b
y = tf.nn.softmax(z)
return y
STEP 5:交叉熵损失
def loss_f(y_pred, y_true):
y_true = tf.one_hot(y_true, depth=10)
term1 = -y_true*tf.math.log(y_pred)
loss = tf.reduce_mean(tf.reduce_sum(term1,1))
return loss
STEP 6:梯度下降
'''定义随机梯度下降优化器'''
optimizer = tf.optimizers.SGD(learning_rate)
def run_optimizer(x,y):
with tf.GradientTape() as g:
pred = logistic_f(x)
loss = loss_f(pred,y)
gradients = g.gradient(loss, [w, b])
optimizer.apply_gradients(zip(gradients,[w,b]))
STEP 7:计算准确度
def accuracy(y_pred,y_true):
y_pred = tf.argmax(y_pred,1)
y_true = tf.cast(y_true, y_pred.dtype)
correct = tf.equal(y_pred, y_true)
correct = tf.cast(correct, tf.float32)
accuracy = tf.reduce_mean(correct)
return accuracy
STEP 8:模型训练
'''迭代执行优化器计算出w,b'''
all_loss = []
all_acc = []
for i, (batch_x, batch_y) in enumerate(train_data.take(step), 1):
run_optimizer(batch_x, batch_y)
if i % 50 == 0:
pred = logistic_f(batch_x)
loss = loss_f(pred, batch_y)
all_loss.append(loss)
acc = accuracy(pred, batch_y)
all_acc.append(acc)
print("step: %i, loss: %f, accuracy: %f" % (i, loss, acc))
第2000次迭代结果
step: 2000, loss: 0.308024, accuracy: 0.929688
STEP 9:可视化损失函数
'''可视化损失函数'''
import numpy as np
import matplotlib.pyplot as plt
step_list = np.linspace(0,2000,40)
plt.plot(step_list,all_loss,label="loss")
plt.plot(step_list,all_acc,label="acc")
plt.legend()
plt.show()
import matplotlib.pyplot as plt
n_images = 5
test = x_test[:n_images]
predictions = logistic_f(test_images)
for i in range(n_images):
plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
plt.show()
print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))