**
一、权重参数的保存与加载
**
network.save_weights('weights.ckpt')
network.load_weights('weights.ckpt')
权重参数的保存与加载可以针对任何模型,包括自定义的。
但是在加载权重参数时,其模型的结构需要与原来的完全一致。
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,Sequential,optimizers,datasets,metrics
def preprocess(x,y):
x = tf.cast(tf.reshape(x,[-1]),dtype=tf.float32)/255.
y = tf.cast(tf.one_hot(y,depth=10),dtype=tf.int32)
return x,y
#load_data
(x_train,y_train),(x_val,y_val) = datasets.mnist.load_data()
print('data: ',x_train.shape,y_train.shape,x_val.shape,y_val.shape)
db = tf.data.Dataset.from_tensor_slices((x_train,y_train))
db = db.map(preprocess).shuffle(60000).batch(128)
db_val = tf.data.Dataset.from_tensor_slices((x_val,y_val))
db_val = db_val.map(preprocess).batch(128)
#self def layer
class MyDense(layers.Layer):#inhe