当前位置:网站首页>5 model saving and loading
5 model saving and loading
2022-06-26 15:58:00 【X1996_】
One keras Model saving and loading
Build models and train
import numpy as np
import tensorflow as tf
x_train = np.random.random((1000, 32))
y_train = np.random.randint(10, size=(1000, ))
x_val = np.random.random((200, 32))
y_val = np.random.randint(10, size=(200, ))
x_test = np.random.random((200, 32))
y_test = np.random.randint(10, size=(200, ))
# Functional expression
def get_uncompiled_model():
inputs = tf.keras.Input(shape=(32,), name='digits')
x = tf.keras.layers.Dense(64, activation='relu', name='dense_1')(inputs)
x = tf.keras.layers.Dense(64, activation='relu', name='dense_2')(x)
outputs = tf.keras.layers.Dense(10, name='predictions')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
def get_compiled_model():
model = get_uncompiled_model()
model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=1e-3),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['sparse_categorical_accuracy'])
return model
model = get_compiled_model()
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_val, y_val))
There are four ways to save and load
There are two methods for method 1
# Save and load , Only the weights are saved , Before loading weights, you need to build the model
model.save_weights("adasd.h5")
model.load_weights("adasd.h5")
model.predict(x_test)
model.save_weights('./checkpoints/mannul_checkpoint')
model.load_weights('./checkpoints/mannul_checkpoint')
model.predict(x_test)
Method 2
# There is no need to build a model when loading a model
# Export the model to a SavedModel
model.save('keras_model_tf_version', save_format='tf')
# Recreate the exact same model
new_model = tf.keras.models.load_model('keras_model_tf_version')
new_model.predict(x_test)
Method 3
# preservation hdf5 Format
model.save('keras_model_hdf5_version.h5')
new_model = tf.keras.models.load_model('keras_model_hdf5_version.h5')
new_model.predict(x_test)
Method 4 is often used in model deployment
# Save the format of model deployment
tf.saved_model.save(model,'tf_saved_model_version')
restored_saved_model = tf.saved_model.load('tf_saved_model_version')
f = restored_saved_model.signatures["serving_default"]
f(digits = tf.constant(x_test.tolist()) )
# Display the required information
!saved_model_cli show --dir tf_saved_model_version --all
2 Custom version model saving and loading
Save and load methods cannot be used except method 3 , Everything else is the same
import tensorflow as tf
import numpy as np
x_train = np.random.random((1000, 32))
y_train = np.random.random((1000, 10))
x_val = np.random.random((200, 32))
y_val = np.random.random((200, 10))
x_test = np.random.random((200, 32))
y_test = np.random.random((200, 10))
class MyModel(tf.keras.Model):
def __init__(self, num_classes=10):
super(MyModel, self).__init__(name='my_model')
self.num_classes = num_classes
# Define the layers you need
self.dense_1 = tf.keras.layers.Dense(32, activation='relu')
self.dense_2 = tf.keras.layers.Dense(num_classes)
@tf.function(input_signature=[tf.TensorSpec([None,32], tf.float32,name='digits')])
def call(self, inputs):
# Define forward propagation
# Use in (in `__init__`) Defined layer
x = self.dense_1(inputs)
return self.dense_2(x)
# Optimizer
optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3)
# Loss function
loss_fn = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
# Get ready metrics function
train_acc_metric = tf.keras.metrics.CategoricalAccuracy()
val_acc_metric = tf.keras.metrics.CategoricalAccuracy()
# Prepare the training data set
batch_size = 64
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)
# Prepare the test data set
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
val_dataset = val_dataset.batch(64)
model = MyModel(num_classes=10)
epochs = 3
for epoch in range(epochs):
print('Start of epoch %d' % (epoch,))
# Traversing the data set batch_size
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
with tf.GradientTape() as tape:
logits = model(x_batch_train)
loss_value = loss_fn(y_batch_train, logits)
grads = tape.gradient(loss_value, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
# Update the of the training set metrics
train_acc_metric(y_batch_train, logits)
# Every time 200 batches Print once .
if step % 200 == 0:
print('Training loss (for one batch) at step %s: %s' % (step, float(loss_value)))
print('Seen so far: %s samples' % ((step + 1) * 64))
# At every epoch Show at the end metrics.
train_acc = train_acc_metric.result()
print('Training acc over epoch: %s' % (float(train_acc),))
# At every epoch Reset the training indicator at the end
train_acc_metric.reset_states()
# At every epoch Run a validation set at the end .
for x_batch_val, y_batch_val in val_dataset:
val_logits = model(x_batch_val)
# Update validation set merics
val_acc_metric(y_batch_val, val_logits)
val_acc = val_acc_metric.result()
val_acc_metric.reset_states()
print('Validation acc: %s' % (float(val_acc),))
After the training, it is time to save the model
边栏推荐
- OpenSea上如何创建自己的NFT(Polygon)
- Analyse panoramique de la chaîne industrielle en amont, en aval et en aval de la NFT « Dry goods»
- 9 Tensorboard的使用
- 全面解析Discord安全问题
- Interview pit summary I
- NFT交易原理分析(2)
- Nanopi duo2 connection WiFi
- Unable to download Plug-in after idea local agent
- TweenMax+SVG切换颜色动画场景
- 【leetcode】331. Verifying the preorder serialization of a binary tree
猜你喜欢
随机推荐
效率超级加倍!pycharm十个小技巧就是这么神
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
音视频学习(三)——sip协议
Canvas three dot flashing animation
selenium将元素保存为图片
Beijing Fangshan District specialized special new small giant enterprise recognition conditions, with a subsidy of 500000 yuan
Solana capacity expansion mechanism analysis (2): an extreme attempt to sacrifice availability for efficiency | catchervc research
【leetcode】112. 路径总和 - 113. 路径总和 II
手写数字体识别,用保存的模型跑自己的图片
nanoPi Duo2连接wifi
有Cmake的工程交叉编译到链接时报错找不到.so动态库文件
IntelliJ idea -- Method for formatting SQL files
HW safety response
golang 临时对象池优化
手机上怎么开户?在线开户安全么?
9 use of tensorboard
Evaluate:huggingface detailed introduction to the evaluation index module
Ansible自动化的运用
4 自定义模型训练









