最近课程需要,将lenet-5模型进行还原,但是网上博客大都根据原论文进行了自己的改进,不能满足还原的这个要求,所以按照论文结合TensorFlow2.0的API,将其还原。
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
The default version of TensorFlow in Colab will soon switch to TensorFlow 2.x.
We recommend you upgrade now or ensure your notebook will continue to use TensorFlow 1.x via the %tensorflow_version 1.x
magic: more info.
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 16313844761984461564, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 17583002155353847962
physical_device_desc: "device: XLA_CPU device", name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 188667365564072257
physical_device_desc: "device: XLA_GPU device", name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 15956161332
locality {
bus_id: 1
links {
}
}
incarnation: 540513892452511862
physical_device_desc: "device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0"]
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
%matplotlib inline
print(tf.__version__)
1.15.0
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# np.newaxis增加一维数据
X_val, y_val = X_train[55000:, ..., np.newaxis], y_train[55000:]
X_train, y_train = X_train[:55000, ..., np.newaxis], y_train[:55000]
X_test = X_test[..., np.newaxis]
print("图片形状:{}".format(X_train.shape), end = '\n\n')
print("训练集合:{}".format(len(X_train)))
print("验证集合:{}".format(len(X_val)))
print("测试集合:{}".format(len(X_test)))
图片形状:(55000, 28, 28, 1)
训练集合:55000
验证集合:5000
测试集合:10000
# 将图片大小reshape为32*32,第1维=数量前面扩充0列,最后面扩充0列;第2维==行,前面扩充2列,最后面扩充2列;依次第3维=列、第4维...constant_values=未指定,扩充全为0
X_train = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
X_val = np.pad(X_val, ((0,0),(2,